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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

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.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.controller.TroubleCallBack;
import com.tofly.ghrq_route.module.trouble.domain.MyTroubleEvent;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleProcessBean;
import com.tofly.ghrq_route.module.trouble.ui.view.TroubleExecuteReportModuleLayout;
import com.tofly.ghrq_route.module.trouble.ui.view.TroubleReportInfoModuleLayout;
import com.tofly.ghrq_route.module.trouble.ui.view.TroubleUndertakerVerifyModuleLayout;
import com.tofly.ghrq_route.module.trouble.ui.view.TroubleVerifyMakePlanModuleLayout;
import com.tofly.ghrq_route.module.trouble.ui.view.TroubleVerifyRouteModuleLayout;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UiTroubleDetail extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.layout_collapse_toolbar)
    CollapsingToolbarLayout layoutCollapseToolbar;
    @BindView(R.id.layout_module_report_info)
    TroubleReportInfoModuleLayout reportInfoModule;
    @BindView(R.id.layout_module_undertaker_verify)
    TroubleUndertakerVerifyModuleLayout undertakerVerifyModule;
    @BindView(R.id.layout_module_verify_route_report)
    TroubleVerifyRouteModuleLayout verifyRouteModule;
    @BindView(R.id.layout_module_verify_make_plan)
    TroubleVerifyMakePlanModuleLayout verifyMakePlanModule;
    @BindView(R.id.layout_module_execute_report_verify)
    TroubleExecuteReportModuleLayout executeVerifyLayout;

    private TroubleTool troubleTool = TroubleTool.getInstance();

    private TroubleBean mTroubleBean;

    private boolean isEditable = true;

    private int troubleSelectType = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_trouble_detail);
        ButterKnife.bind(this);

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

    protected void initView() {
        setDarkStatusBar();
        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            if (bundle.containsKey(TroubleBean.TROUBLE_BEAN_KEY)) {
                mTroubleBean = (TroubleBean) bundle.getSerializable(TroubleBean.TROUBLE_BEAN_KEY);
                isEditable = bundle.getBoolean(TroubleBean.TROUBLE_BEAN_EDITABLE_KEY, true);
                troubleSelectType = bundle.getInt(TroubleBean.TROUBLE_LIST_SELECT_TYPE, -1);
                if (mTroubleBean != null) {
                    getTroubleDataFromServer();

                    NotifyManageBean msgBean = troubleTool.getNotifyMsgBean(mTroubleBean.getId());
                    if (msgBean != null) {
                        troubleTool.setMsgBeanRead(msgBean);
                        if (bundle.containsKey(TroubleBean.TROUBLE_POSITION_KEY)) {
                            int position = (int) bundle.getSerializable(TroubleBean.TROUBLE_POSITION_KEY);
                            EventBusUtil.postEvent(new MyTroubleEvent(MyTroubleEvent.TROUBLE_TROUBLE_HAS_READ, position));
                        }
                    }
                }
            }
        }
    }

    private void getTroubleDataFromServer() {
        String message = getResources().getString(R.string.updating_data);
        ProgressManager.getInstance().showProgress(this, null,  message, true);
        String trouble_id = mTroubleBean.getId();
        troubleTool.loadTroubleFromServer(this, trouble_id, new TroubleCallBack(){
            @Override
            public void onGetTroubleInfo(boolean isSuccess, int errorCode, String errMsg, TroubleBean troubleBean) {
                super.onGetTroubleInfo(isSuccess, errorCode, errMsg, troubleBean);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess && troubleBean != null) {
                    mTroubleBean = troubleBean;
                    setTroubleView();
                }
            }
        });
    }

    private void setTroubleView() {
        if (mTroubleBean != null) {
            int processId = mTroubleBean.getProcessId();
            int uploadType = mTroubleBean.getUploadType();

            /*隐患上报模块*/
            reportInfoModule.initTroubleInfo(mTroubleBean, isEditable, troubleSelectType);

            verifyRouteModule.setVisibility(View.GONE);

            verifyMakePlanModule.setVisibility(View.GONE);
            executeVerifyLayout.setVisibility(View.GONE);
            if (processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDERTAKER_VERIFY
                    || processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDERTAKER_MAKE_SCHEME) {
                undertakerVerifyModule.setVisibility(View.GONE);
            }else {
                undertakerVerifyModule.setVisibility(View.VISIBLE);
                undertakerVerifyModule.initTroubleInfo(mTroubleBean, isEditable);
                if (processId >= TroubleBean.TROUBLE_PROCESS_TURNED_INTO_SAVED_TROUBLE) {
                    /*副部长审核, 方案编制模块*/
                    boolean hasSchemeModule = checkHasSchemeModule();
                    if (hasSchemeModule) {
                        verifyMakePlanModule.setVisibility(View.VISIBLE);
                        verifyMakePlanModule.initTroubleInfo(mTroubleBean, isEditable);
                    }else {
                        verifyMakePlanModule.setVisibility(View.GONE);
                    }

                    /*执行及回报审核模块*/
                    boolean hasExecuteModule = checkHasExecuteModule();
                    if (hasExecuteModule) {
                        executeVerifyLayout.setVisibility(View.VISIBLE);
                        executeVerifyLayout.initTroubleInfo(mTroubleBean, isEditable);
                    }else {
                        executeVerifyLayout.setVisibility(View.GONE);
                    }
                }
            }
        }
    }

    private boolean checkHasSchemeModule() {
        boolean hasModule = false;
        List<TroubleProcessBean> checks = mTroubleBean.getChecks();
        for (TroubleProcessBean check : checks) {
            if (check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_VERIFY_SCHEME
                    || check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_JOB_TAKER_EXECUTE
                    || check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_JOB_TAKER_EXECUTE_FROM_SECTARY) {
                hasModule = true;
                break;
            }
        }
        return hasModule;
    }

    private boolean checkHasExecuteModule() {
        boolean hasModule = false;
        List<TroubleProcessBean> checks = mTroubleBean.getChecks();
        for (TroubleProcessBean check : checks) {
            if (check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_JOB_TAKER_EXECUTE
                    || check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_UNDERTAKER_VERIFY_EXECUTE
                    || check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_JOB_TAKER_EXECUTE_FROM_SECTARY
                    || check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_VERIFY_EXECUTE) {
                hasModule = true;
                break;
            }
        }
        return hasModule;
    }

    protected void initData() {

    }

    protected void initListener() {

    }

    @Subscribe
    public void onMessageEvent(MyTroubleEvent event) {
        if (MyTroubleEvent.TROUBLE_UPLOADED.equals(event.message) ||
                MyTroubleEvent.TROUBLE_SAVED.equals(event.message)) {
            finish();
        } else if (MyTroubleEvent.TROUBLE_DEVICE_UPDATED.equals(event.message)) {
            getTroubleDataFromServer();
        }
    }

    @OnClick({})
    public void onClick(View view) {

    }

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

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

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

    }
}
