package com.tofly.ghrq_route.common.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.BuildappendAdapter;
import com.tofly.ghrq_route.module.buildManage.buildFilesInfo;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairExamineBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairFileBean;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairProcessBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.common.util.NoListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 * 抢维修过程管理详情
 */
public class UiRepairProcessDetail extends BaseUi {

    @BindView(R.id.tv_repair_code)
    TextView tvRepairCode;
    @BindView(R.id.tv_repair_type)
    TextView tvRepairType;
    @BindView(R.id.tv_repair_source)
    TextView tvRepairSource;
    @BindView(R.id.tv_contact)
    TextView tvContact;
    @BindView(R.id.tv_contact_phone)
    TextView tvContactPhone;
    @BindView(R.id.edit_scene_charge)
    TextView editSceneCharge;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.list_appendix)
    NoListView listAppendix;
    @BindView(R.id.ll_append)
    LinearLayout llAppend;
    @BindView(R.id.tv_dispatch)
    TextView tvDispatch;
    @BindView(R.id.tv_dispatch_time)
    TextView tvDispatchTime;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.ll_process)
    LinearLayout llProcess;
    @BindView(R.id.tv_part)
    TextView tvPart;
    @BindView(R.id.tv_rush_note)
    TextView tvRushNote;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.ll_examine_add)
    LinearLayout llExamineAdd;
    @BindView(R.id.ll_examine)
    LinearLayout llExamine;

    private RushRepairBean data;
    private BuildappendAdapter adapter, adapter1, adapter2;
    private List<buildFilesInfo> appendList, appendList1, appendList2;
    private List<RushRepairFileBean> fileBeanList;
    private DBDaoHelper dbDaoHelper;
    private List<RushRepairProcessBean> repairProcessBeans;
    private List<RushRepairExamineBean> examineBeans;

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

    private void initData() {
        appendList = new ArrayList<>();
        data = (RushRepairBean) getIntent().getSerializableExtra("rush_repair");
        if (data != null) {
            /*工单编号*/
            if (!TextUtils.isEmpty(data.getWorkOrderId())) {
                tvRepairCode.setText(data.getWorkOrderId());
            }
            /*工单类型*/
            if (!TextUtils.isEmpty(data.getWorkOrderTypeName())) {
                tvRepairType.setText(data.getWorkOrderTypeName());
            }
            /*工单来源*/
            if (!TextUtils.isEmpty(data.getWorkOrderSource())) {
                tvRepairSource.setText(data.getWorkOrderSource());
            }
            /*抢修班班长姓名*/
            if (!TextUtils.isEmpty(data.getDispatchUserName())) {
                editSceneCharge.setText(data.getDispatchUserName());
            }
            /*联系人*/
            if (!TextUtils.isEmpty(data.getContact())) {
                tvContact.setText(data.getContact());
            }
            /*联系人电话*/
            if (!TextUtils.isEmpty(data.getContactPhone())) {
                tvContactPhone.setText(data.getContactPhone());
            }
            /*地址*/
            if (!TextUtils.isEmpty(data.getAddress())) {
                tvAddress.setText(data.getAddress());
            }
            if (!TextUtils.isEmpty(data.getDispatchDate())) {
                tvDispatchTime.setText(data.getDispatchDate());
            }
            //监护人
            if (!TextUtils.isEmpty(data.getResponsibleName())) {
                tvDispatch.setText(data.getResponsibleName());
            }
            if (!TextUtils.isEmpty(data.getRushRepairPersonsName())) {
                tvPart.setText(data.getRushRepairPersonsName());
            }
            /*备注*/
            if (!TextUtils.isEmpty(data.getDescription())) {
                tvRushNote.setText(data.getDescription());
            }
            /* 派发备注*/
            if (!TextUtils.isEmpty(data.getIntroduction())) {
                tvNote.setText(data.getIntroduction());
            }
            dbDaoHelper = DBDaoHelper.getInstance();
            fileBeanList = dbDaoHelper.getRushRepairFile(data.getId());
            if (fileBeanList != null && fileBeanList.size() > 0) {
                for (RushRepairFileBean fileBean : fileBeanList) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getVoicePath())) {
                        info.setId(data.getId() + "-" + fileBean.getId());
                        info.setPath(fileBean.getVoicePath());
                        info.setName(fileBean.getVoiceName());
                        appendList.add(info);
                    }
                    if (!TextUtils.isEmpty(fileBean.getFilePath())) {
                        buildFilesInfo info1 = new buildFilesInfo();
                        info1.setId(data.getId() + "-" + fileBean.getId());
                        info1.setPath(fileBean.getFilePath());
                        info1.setName(fileBean.getFileName());
                        appendList.add(info1);
                    }
                }
                if (appendList.size() > 0) {
                    llAppend.setVisibility(View.VISIBLE);
                }
                adapter = new BuildappendAdapter(this, appendList);
                listAppendix.setAdapter(adapter);
            }
            repairProcessBeans = dbDaoHelper.getRushProcessManage(data.getId());
            if (repairProcessBeans != null && repairProcessBeans.size() > 0) {
                llProcess.setVisibility(View.VISIBLE);
                for (RushRepairProcessBean repairProcessBean : repairProcessBeans) {
                    if (TextUtils.isEmpty(repairProcessBean.getSubmit_state())) {
                        View view = getProcessView(repairProcessBean);
                        llAdd.addView(view);
                    }
                }
            }
            examineBeans = dbDaoHelper.getRushExamine(data.getId());
            if (examineBeans != null && examineBeans.size() > 0) {
                llExamine.setVisibility(View.VISIBLE);
                for (RushRepairExamineBean buildExamineBean : examineBeans) {
                    View view = getExamineView(buildExamineBean);
                    llExamineAdd.addView(view);
                }
            }
        }
    }

    private View getExamineView(RushRepairExamineBean buildExamineBean) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_build_examine, null);
        TextView tv_examine = view.findViewById(R.id.tv_examine);
        TextView tv_examine_time = view.findViewById(R.id.tv_examine_time);
        TextView tv_examine_idea = view.findViewById(R.id.tv_examine_idea);
        TextView tv_examine_description = view.findViewById(R.id.tv_examine_description);
        LinearLayout ll_description = view.findViewById(R.id.ll_description);
        LinearLayout ll_append2 = view.findViewById(R.id.ll_append2);
        NoListView list_examine = view.findViewById(R.id.list_examine);
        if (!TextUtils.isEmpty(buildExamineBean.getAuditorname())) {
            tv_examine.setText(buildExamineBean.getAuditorname());
        }
        if (!TextUtils.isEmpty(buildExamineBean.getAuditTime())) {
            tv_examine_time.setText(buildExamineBean.getAuditTime());
        }
        if (!TextUtils.isEmpty(buildExamineBean.getAuditmindname())) {
            tv_examine_idea.setText(buildExamineBean.getAuditmindname());
        }
        if (!TextUtils.isEmpty(buildExamineBean.getAuditDescription())) {
            ll_description.setVisibility(View.VISIBLE);
            tv_examine_description.setText(buildExamineBean.getAuditDescription());
        }
        if (!TextUtils.isEmpty(buildExamineBean.getWorkOrderId()) && !TextUtils.isEmpty(buildExamineBean.getId())) {
            List<RushRepairFileBean> manageFileBeans = dbDaoHelper.getRushRepairFiles(buildExamineBean.getWorkOrderId(), buildExamineBean.getId());
            if (manageFileBeans != null && manageFileBeans.size() > 0) {
                appendList2 = new ArrayList<>();
                ll_append2.setVisibility(View.VISIBLE);
                for (RushRepairFileBean fileBean : manageFileBeans) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getAuditEndFilesPath())) {
                        info.setId(fileBean.getBuildId() + "-" + fileBean.getId());
                        info.setPath(fileBean.getAuditEndFilesPath());
                        info.setName(fileBean.getAuditEndFilesName());
                        appendList2.add(info);
                    }
                }
                adapter2 = new BuildappendAdapter(this, appendList2);
                list_examine.setAdapter(adapter2);
            }
        }

        return view;
    }

    private View getProcessView(RushRepairProcessBean buildProcessManageBean) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_build_process, null);
        TextView tv_process_describe = view.findViewById(R.id.tv_process_describe);
        TextView tv_process_date = view.findViewById(R.id.tv_process_date);
        LinearLayout ll_append1 = view.findViewById(R.id.ll_append1);
        NoListView list_process = view.findViewById(R.id.list_process);
        if (!TextUtils.isEmpty(buildProcessManageBean.getDescription())) {
            tv_process_describe.setText(buildProcessManageBean.getDescription());
        }
        if (!TextUtils.isEmpty(buildProcessManageBean.getManageDate())) {
            tv_process_date.setText(buildProcessManageBean.getManageDate());
        }
        if (!TextUtils.isEmpty(buildProcessManageBean.getWorkOrderId()) && !TextUtils.isEmpty(buildProcessManageBean.getId())) {
            List<RushRepairFileBean> manageFileBeans = dbDaoHelper.getRushRepairFile(buildProcessManageBean.getWorkOrderId(), buildProcessManageBean.getId());
            if (manageFileBeans != null && manageFileBeans.size() > 0) {
                appendList1 = new ArrayList<>();
                ll_append1.setVisibility(View.VISIBLE);
                for (RushRepairFileBean fileBean : manageFileBeans) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getProcessVoicePath())) {
                        info.setId(fileBean.getBuildId() + "-" + fileBean.getId());
                        info.setPath(fileBean.getProcessVoicePath());
                        info.setName(fileBean.getProcessVoiceName());
                        appendList1.add(info);
                    }
                    if (!TextUtils.isEmpty(fileBean.getProcessFilePath())) {
                        buildFilesInfo info1 = new buildFilesInfo();
                        info1.setId(fileBean.getBuildId() + "-" + fileBean.getId());
                        info1.setPath(fileBean.getProcessFilePath());
                        info1.setName(fileBean.getProcessFileName());
                        appendList1.add(info1);
                    }
                }
                adapter1 = new BuildappendAdapter(this, appendList1);
                list_process.setAdapter(adapter1);
            }
        }

        return view;
    }
}
