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.module.buildManage.BuildExamineBean;
import com.tofly.ghrq_route.module.buildManage.buildFilesInfo;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageFileBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildProcessManageBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.common.ui.adapter.BuildappendAdapter;
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 UiBuildProcessDeatil extends BaseUi {
    @BindView(R.id.edit_build_name)
    TextView editBuildName;
    @BindView(R.id.text_build_type)
    TextView textBuildType;
    @BindView(R.id.edit_build_company)
    TextView editBuildCompany;
    @BindView(R.id.edit_scene_charge)
    TextView editSceneCharge;
    @BindView(R.id.edit_scene_charge_phone)
    TextView editSceneChargePhone;
    @BindView(R.id.edit_manage_company)
    TextView editManageCompany;
    @BindView(R.id.edit_manage_charger)
    TextView editManageCharger;
    @BindView(R.id.edit_build_fzr)
    TextView editBuildFzr;
    @BindView(R.id.tv_dispatch)
    TextView tvDispatch;
    @BindView(R.id.tv_dispatch_time)
    TextView tvDispatchTime;
    @BindView(R.id.list_appendix)
    NoListView listAppendix;
    @BindView(R.id.ll_append)
    LinearLayout llAppend;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.ll_process)
    LinearLayout llProcess;
    @BindView(R.id.text_build_address)
    TextView textBuildAddress;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_dispatch_note)
    TextView tvDispatchNote;
    @BindView(R.id.ll_examine_add)
    LinearLayout llExamineAdd;
    @BindView(R.id.ll_examine)
    LinearLayout llExamine;

    private BuildManageBean data;
    private BuildappendAdapter adapter, adapter1, adapter2;
    private List<buildFilesInfo> appendList, appendList1, appendList2;
    private List<BuildManageFileBean> fileBeanList;
    private DBDaoHelper dbDaoHelper;
    private List<BuildProcessManageBean> processManageBeans;
    private List<BuildExamineBean> examineBeans;

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

    private void initData() {
        appendList = new ArrayList<>();
        data = (BuildManageBean) getIntent().getSerializableExtra("build_manage");
        if (data != null) {
            /*工地监护名称*/
            if (!TextUtils.isEmpty(data.getSiteName())) {
                editBuildName.setText(data.getSiteName());
            }
            /*工程建设类别*/
            if (!TextUtils.isEmpty(data.getSiteTypeName())) {
                textBuildType.setText(data.getSiteTypeName());
            }
            /*施工单位名称*/
            if (!TextUtils.isEmpty(data.getConstructionUnit())) {
                editBuildCompany.setText(data.getConstructionUnit());
            }

            /*处置班班长姓名*/
            if (!TextUtils.isEmpty(data.getDispatchUserName())) {
                editSceneCharge.setText(data.getDispatchUserName());
            }

            /*施工负责人*/
            if (!TextUtils.isEmpty(data.getPersonInCharge())) {
                editBuildFzr.setText(data.getPersonInCharge());
            }
            /*施工负责人电话*/
            if (!TextUtils.isEmpty(data.getPersonInChargePhone())) {
                editSceneChargePhone.setText(data.getPersonInChargePhone());
            }
            /*主管人姓名*/
            if (!TextUtils.isEmpty(data.getManager())) {
                editManageCharger.setText(data.getManager());
            }
            /*主管单位名字*/
            if (!TextUtils.isEmpty(data.getManageUnit())) {
                editManageCompany.setText(data.getManageUnit());
            }
            if (!TextUtils.isEmpty(data.getConstructionPersonName())) {
                tvDispatch.setText(data.getConstructionPersonName());
            }
            if (!TextUtils.isEmpty(data.getDispatchDate())) {
                tvDispatchTime.setText(data.getDispatchDate());
            }
            if (!TextUtils.isEmpty(data.getConstructionAddress())) {
                textBuildAddress.setText(data.getConstructionAddress());
            }
            if (!TextUtils.isEmpty(data.getDescription())) {
                tvDescription.setText(data.getDescription());
            }
            if (!TextUtils.isEmpty(data.getNote())) {
                tvDispatchNote.setText(data.getNote());
            }
            dbDaoHelper = DBDaoHelper.getInstance();
            fileBeanList = dbDaoHelper.getBuildManageFile(data.getId());
            if (fileBeanList != null && fileBeanList.size() > 0) {
                for (BuildManageFileBean 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);
            }
            processManageBeans = dbDaoHelper.getBuildProcessManage(data.getId());
            if (processManageBeans != null && processManageBeans.size() > 0) {
                llProcess.setVisibility(View.VISIBLE);
                for (BuildProcessManageBean buildProcessManageBean : processManageBeans) {
                    if (TextUtils.isEmpty(buildProcessManageBean.getSubmit_state())) {
                        View view = getProcessView(buildProcessManageBean);
                        llAdd.addView(view);
                    }
                }
            }
            examineBeans = dbDaoHelper.getBuildExamine(data.getId());
            if (examineBeans != null && examineBeans.size() > 0) {
                llExamine.setVisibility(View.VISIBLE);
                for (BuildExamineBean buildExamineBean : examineBeans) {
                    View view = getExamineView(buildExamineBean);
                    llExamineAdd.addView(view);
                }
            }
        }
    }

    private View getProcessView(BuildProcessManageBean 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<BuildManageFileBean> manageFileBeans = dbDaoHelper.getBuildManageFile(buildProcessManageBean.getWorkOrderId(), buildProcessManageBean.getId());
            if (manageFileBeans != null && manageFileBeans.size() > 0) {
                ll_append1.setVisibility(View.VISIBLE);
                appendList1 = new ArrayList<>();
                for (BuildManageFileBean 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;
    }

    private View getExamineView(BuildExamineBean 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_append2 = view.findViewById(R.id.ll_append2);
        LinearLayout ll_description = view.findViewById(R.id.ll_description);
        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<BuildManageFileBean> manageFileBeans = dbDaoHelper.getBuildManageFiles(buildExamineBean.getWorkOrderId(), buildExamineBean.getId());
            if (manageFileBeans != null && manageFileBeans.size() > 0) {
                appendList2 = new ArrayList<>();
                ll_append2.setVisibility(View.VISIBLE);
                for (BuildManageFileBean 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;
    }
}
