package com.tofly.ghrq_route.module.emergencyHandle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.emergencyHandle.EmergencyTool;
import com.tofly.ghrq_route.module.emergencyHandle.controller.EmergencyCallBack;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.MyEmergencyEvent;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyAssignHandleHistoryModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyAssignHandleInfoModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyReportInfoModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyScheduleAssignReportModuleLayout;
import com.tofly.ghrq_route.common.util.EventBusUtil;
import com.tofly.ghrq_route.common.util.dialog.DialogUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zombiexq on 2016/7/4.
 */
public class UiEmergencyScheduleAssign extends BaseUi {
    @BindView(R.id.layout_collapse_toolbar)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.main_toolbar)
    Toolbar toolbar;
    @BindView(R.id.layout_module_schedule_assign_report)
    EmergencyScheduleAssignReportModuleLayout scheduleAssignModule;
    @BindView(R.id.layout_module_assign_handle_info)
    EmergencyAssignHandleInfoModuleLayout assignHandleInfoModule;
    @BindView(R.id.layout_module_assign_handle_history)
    EmergencyAssignHandleHistoryModuleLayout assignHandleHistoryModule;
    @BindView(R.id.layout_module_emergency_info)
    EmergencyReportInfoModuleLayout emergencyInfoModule;

    private EmergencyHandleData mEmergencyData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_emergency_schedule_assign);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

        EventBusUtil.register(this);
    }

    public void initView() {
        initToolBar();
        setDarkStatusBar();
    }

    private void initToolBar() {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(v -> {
            this.finish();
        });
    }

    public void initData() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            EmergencyHandleData handleData =
                    (EmergencyHandleData) extras.getSerializable(EmergencyHandleData.EMERGENCY_DATA_KEY);
            if (handleData != null) {
                mEmergencyData = handleData;

                assignHandleInfoModule.initEmergencyInfo(mEmergencyData);
                assignHandleHistoryModule.initEmergencyInfo(handleData);
                emergencyInfoModule.initEmergencyInfo(handleData);
            }
        }
    }

    public void initListener() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_commit, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_commit) {
            if (isValid()) {
                uploadAlert();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private boolean isValid() {
        return scheduleAssignModule.isValid();
    }

    private void uploadAlert() {
        String title = "上传数据";
        String content = "是否上传数据?";
        DialogUtil.showAlertWithConfirmCancelButton(this, title, content, true, (dialogInterface, i) -> {
            emergencyHandle();
        });
    }

    private EmergencyHandleData collectData() {
        EmergencyHandleData handleData = new EmergencyHandleData();
        handleData.setId(mEmergencyData.getId());

        scheduleAssignModule.collectData(handleData);
        return handleData;
    }

    private void emergencyHandle() {
        String strUploading = getString(R.string.uploading_data);
        ProgressManager.getInstance().showProgress(this, null, strUploading, false);

        EmergencyHandleData handleData = collectData();
        EmergencyTool emergencyTool = EmergencyTool.getInstance();
        emergencyTool.emergencyScheduleAssign(this, handleData, new EmergencyCallBack(){
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess) {
                    onEmergencyHandled();
                }
            }
        });
    }

    public void onEmergencyHandled() {
        EventBus.getDefault().post(new MyEmergencyEvent(MyEmergencyEvent.EMERGENCY_SCHEDULE_ASSIGN_UPLOADED, mEmergencyData));
        showUploadFinishedDialog();
    }

    private void showUploadFinishedDialog() {
        int title = R.string.emergency_schedule_assign_report_title;
        int msg = R.string.emergency_schedule_assign_report_ok;
        DialogUtil.showAlertWithConfirmButton(this, title, msg, false, (dialogInterface, i) -> {
            this.finish();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
