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.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.BuildappendAdapter;
import com.tofly.ghrq_route.module.buildManage.BuildExaminesBean;
import com.tofly.ghrq_route.module.buildManage.buildFilesInfo;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageCallback;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageController;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBeans;
import com.tofly.ghrq_route.module.buildManage.domain.VoiceBeans;
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 UiBuildDetail 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_process_add)
    LinearLayout llProcessAdd;
    @BindView(R.id.ll_process)
    LinearLayout llProcess;
    @BindView(R.id.ll_examine_add)
    LinearLayout llExamineAdd;
    @BindView(R.id.ll_examine)
    LinearLayout llExamine;
    @BindView(R.id.ll_dispatch)
    LinearLayout llDispatch;
    @BindView(R.id.text_build_address)
    TextView textBuildAddress;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_dispatch_note)
    TextView tvDispatchNote;

    private BuildManageBeans buildManageDetail;

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

    private void initData() {
        String id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            getData(id);
        }
    }

    private void getData(String id) {
        BuildManageController buildManageController = new BuildManageController(this);
        buildManageController.getBuildDetail(id, new BuildManageCallback() {
            @Override
            public void onBuildDataDetail(boolean isSuccess, int errorCode, String errMsg, BuildManageBeans buildManageBeans, int total) {
                super.onBuildDataDetail(isSuccess, errorCode, errMsg, buildManageBeans, total);
                if (isSuccess) {
                    buildManageDetail = buildManageBeans;
                    initView();
                }
            }
        });
    }

    private void initView() {
        if (buildManageDetail != null) {
            /*工地监护名称*/
            if (!TextUtils.isEmpty(buildManageDetail.getSiteName())) {
                editBuildName.setText(buildManageDetail.getSiteName());
            }
            /*工程建设类别*/
            if (!TextUtils.isEmpty(buildManageDetail.getSiteTypeName())) {
                textBuildType.setText(buildManageDetail.getSiteTypeName());
            }
            /*施工单位名称*/
            if (!TextUtils.isEmpty(buildManageDetail.getConstructionUnit())) {
                editBuildCompany.setText(buildManageDetail.getConstructionUnit());
            }

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

            /*施工负责人*/
            if (!TextUtils.isEmpty(buildManageDetail.getPersonInCharge())) {
                editBuildFzr.setText(buildManageDetail.getPersonInCharge());
            }
            /*施工负责人电话*/
            if (!TextUtils.isEmpty(buildManageDetail.getPersonInChargePhone())) {
                editSceneChargePhone.setText(buildManageDetail.getPersonInChargePhone());
            }
            /*主管人姓名*/
            if (!TextUtils.isEmpty(buildManageDetail.getManager())) {
                editManageCharger.setText(buildManageDetail.getManager());
            }
            /*主管单位名字*/
            if (!TextUtils.isEmpty(buildManageDetail.getManageUnit())) {
                editManageCompany.setText(buildManageDetail.getManageUnit());
            }

            if (!TextUtils.isEmpty(buildManageDetail.getDispatchDate())) {
                tvDispatchTime.setText(buildManageDetail.getDispatchDate());
            } else {
                tvDispatchTime.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(buildManageDetail.getConstructionPersonName())) {
                tvDispatch.setText(buildManageDetail.getConstructionPersonName());
            } else {
                tvDispatch.setVisibility(View.GONE);
                llDispatch.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(buildManageDetail.getConstructionAddress())) {
                textBuildAddress.setText(buildManageDetail.getConstructionAddress());
            }
            if (!TextUtils.isEmpty(buildManageDetail.getDescription())) {
                tvDescription.setText(buildManageDetail.getDescription());
            }
            if (!TextUtils.isEmpty(buildManageDetail.getNote())) {
                tvDispatchNote.setText(buildManageDetail.getNote());
            }
            if (buildManageDetail.getVoice() != null && buildManageDetail.getVoice().size() > 0) {
                buildManageDetail.getFiles().addAll(buildManageDetail.getVoice());
            }
            List<buildFilesInfo> appendList = new ArrayList<>();
            if (buildManageDetail.getFiles() != null && buildManageDetail.getFiles().size() > 0) {
                for (VoiceBeans fileBean : buildManageDetail.getFiles()) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getPath())) {
                        info.setPath(fileBean.getPath());
                    }
                    if (!TextUtils.isEmpty(fileBean.getFileName())) {
                        info.setId(buildManageDetail.getId() + "-" + fileBean.getFileName());
                        info.setName(fileBean.getFileName());
                    }
                    appendList.add(info);
                }
                if (appendList.size() > 0) {
                    llAppend.setVisibility(View.VISIBLE);
                }
                BuildappendAdapter adapter = new BuildappendAdapter(this, appendList);
                listAppendix.setAdapter(adapter);
            }
            if (buildManageDetail.getProcessManages() != null && buildManageDetail.getProcessManages().size() > 0) {
                llProcess.setVisibility(View.VISIBLE);
                for (BuildManageBeans.ProcssManageBean procssManageBean : buildManageDetail.getProcessManages()) {
                    View view = getProcessView(procssManageBean);
                    llProcessAdd.addView(view);
                }
            }
            if (buildManageDetail.getAuditList() != null && buildManageDetail.getAuditList().size() > 0) {
                llExamine.setVisibility(View.VISIBLE);
                for (BuildExaminesBean buildExaminesBean : buildManageDetail.getAuditList()) {
                    View view = getExamineView(buildExaminesBean);
                    llExamineAdd.addView(view);
                }
            }
        }
    }

    private View getProcessView(BuildManageBeans.ProcssManageBean 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 (buildProcessManageBean.getProcessVoice() != null && buildProcessManageBean.getProcessVoice().size() > 0) {
            buildProcessManageBean.getProcessFiles().addAll(buildProcessManageBean.getProcessVoice());
        }
        if (buildProcessManageBean.getProcessFiles() != null && buildProcessManageBean.getProcessFiles().size() > 0) {
            List<buildFilesInfo> appendList1 = new ArrayList<>();
            ll_append1.setVisibility(View.VISIBLE);
            for (VoiceBeans fileBean : buildProcessManageBean.getProcessFiles()) {
                buildFilesInfo info = new buildFilesInfo();
                if (!TextUtils.isEmpty(fileBean.getPath())) {
                    info.setPath(fileBean.getPath());
                }
                if (!TextUtils.isEmpty(fileBean.getFileName())) {
                    info.setId(buildManageDetail.getId() + "-" + fileBean.getFileName());
                    info.setName(fileBean.getFileName());
                }
                appendList1.add(info);
            }
            BuildappendAdapter adapter1 = new BuildappendAdapter(this, appendList1);
            list_process.setAdapter(adapter1);
        }

        return view;
    }

    private View getExamineView(BuildExaminesBean 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())) {
            tv_examine_description.setText(buildExamineBean.getAuditDescription());
            ll_description.setVisibility(View.VISIBLE);
        }
        if (buildExamineBean.getAuditEndFiles() != null && buildExamineBean.getAuditEndFiles().size() > 0) {
            List<buildFilesInfo> appendList2 = new ArrayList<>();
            ll_append2.setVisibility(View.VISIBLE);
            for (VoiceBeans fileBean : buildExamineBean.getAuditEndFiles()) {
                buildFilesInfo info = new buildFilesInfo();
                if (!TextUtils.isEmpty(fileBean.getPath())) {
                    info.setPath(fileBean.getPath());
                }
                if (!TextUtils.isEmpty(fileBean.getFileName())) {
                    info.setName(fileBean.getFileName());
                    info.setId(buildManageDetail.getId() + "-" + fileBean.getFileName());
                }
                appendList2.add(info);
            }
            BuildappendAdapter adapter2 = new BuildappendAdapter(this, appendList2);
            list_examine.setAdapter(adapter2);
        }

        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApp.cancelAllRequest(getClass().getName());
    }
}
