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

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

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

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.MaintainExecuteBean;
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.EmergencyDispatchInfoModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyRepairHandleHistoryModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyRepairHandleInfoModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyReportInfoModuleLayout;
import com.tofly.ghrq_route.module.emergencyHandle.ui.view.EmergencyScheduleAssignInfoModuleLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zombiexq on 2016/7/4.
 */
public class UiEmergencyDetail extends BaseUi {
    @BindView(R.id.layout_collapse_toolbar)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.main_toolbar)
    Toolbar toolbar;
    @BindView(R.id.layout_module_emergency_info)
    EmergencyReportInfoModuleLayout emergencyInfoModule;
    @BindView(R.id.layout_module_assign_handle_history)
    EmergencyAssignHandleHistoryModuleLayout assignHandleHistoryModule;
    @BindView(R.id.layout_module_assign_handle_info)
    EmergencyAssignHandleInfoModuleLayout assignHandleInfoModule;
    @BindView(R.id.layout_module_schedule_assign_info)
    EmergencyScheduleAssignInfoModuleLayout scheduleAssignInfoModule;
    @BindView(R.id.layout_module_dispatch_info)
    EmergencyDispatchInfoModuleLayout dispatchInfoModule;
    @BindView(R.id.layout_module_repair_history)
    EmergencyRepairHandleHistoryModuleLayout repairHistoryModule;
    @BindView(R.id.layout_module_repair_handle_info)
    EmergencyRepairHandleInfoModuleLayout repairHandleInfoModule;

    private EmergencyHandleData mEmergencyData;

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

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

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

    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;
                emergencyInfoModule.initEmergencyInfo(handleData);
                int workOrderState = handleData.getWorkOrderState();
                assignHandleHistoryModule.setVisibility(View.GONE);
                if (workOrderState > EmergencyHandleData.EMERGENCY_PROCESS_ASSIGN_HANDLE) {
                    assignHandleHistoryModule.setVisibility(View.VISIBLE);
                    assignHandleHistoryModule.initEmergencyInfo(handleData);
                    assignHandleInfoModule.initEmergencyInfo(handleData);
                }

                scheduleAssignInfoModule.setVisibility(View.GONE);
                dispatchInfoModule.setVisibility(View.GONE);
                repairHistoryModule.setVisibility(View.GONE);
                repairHandleInfoModule.setVisibility(View.GONE);
                boolean isNeedRepair = handleData.getIsMaintenance() == 1;
                if (isNeedRepair) {
                    scheduleAssignInfoModule.setVisibility(View.VISIBLE);
                    scheduleAssignInfoModule.initEmergencyInfo(handleData);

                    if (workOrderState > EmergencyHandleData.EMERGENCY_PROCESS_NEED_VERIFY) {
                        dispatchInfoModule.setVisibility(View.VISIBLE);
                        dispatchInfoModule.initEmergencyInfo(handleData);
                    }
                    List<MaintainExecuteBean> maintenanceHisList = handleData.getSchedulerMaintenanceHisList();
                    if (maintenanceHisList != null && maintenanceHisList.size() > 0) {
                        repairHistoryModule.setVisibility(View.VISIBLE);
                        repairHistoryModule.initEmergencyInfo(handleData);
                    }
                    repairHandleInfoModule.setVisibility(View.VISIBLE);
                    repairHandleInfoModule.initEmergencyInfo(mEmergencyData);
                }
            }
        }
    }

    public void initListener() {

    }

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