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 UiBuildFile 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.text_build_address)
    TextView textBuildAddress;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_dispatch_note)
    TextView tvDispatchNote;

    private BuildManageBeans data;
    private List<VoiceBeans> fileBeanList;
    private BuildManageController controller;

    @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) {
        if (controller == null) {
            controller = new BuildManageController(this);
        }

        controller.getBuildDetail(id, new BuildManageCallback() {
            @Override
            public void onBuildDataDetail(boolean isSuccess, int errorCode, String errMsg, BuildManageBeans buildManageList, int total) {
                super.onBuildDataDetail(isSuccess, errorCode, errMsg, buildManageList, total);
                if (isSuccess) {
                    data = buildManageList;
                    initView();
                }
            }
        });
    }

    private void initView() {
        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.getDispatchDate())) {
                tvDispatchTime.setText(data.getDispatchDate());
            }
            if (!TextUtils.isEmpty(data.getConstructionPersonName())) {
                tvDispatch.setText(data.getConstructionPersonName());
            }
            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());
            }
            if (data.getVoice() != null && data.getVoice().size() > 0) {
                data.getFiles().addAll(data.getVoice());
            }
            if (data.getFiles() != null && data.getFiles().size() > 0) {
                fileBeanList = data.getFiles();
            }
            if (fileBeanList != null && fileBeanList.size() > 0) {
                List<buildFilesInfo> appendList = new ArrayList<>();
                for (VoiceBeans fileBean : fileBeanList) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getFileName())) {
                        info.setId(data.getId() + "-" + fileBean.getFileName());
                        info.setName(fileBean.getFileName());
                    }
                    if (!TextUtils.isEmpty(fileBean.getPath())) {
                        info.setPath(fileBean.getPath());
                    }
                    appendList.add(info);
                }
                if (appendList.size() > 0) {
                    llAppend.setVisibility(View.VISIBLE);
                }
                BuildappendAdapter adapter = new BuildappendAdapter(this, appendList);
                listAppendix.setAdapter(adapter);
            }
            if (data.getProcessManages() != null && data.getProcessManages().size() > 0) {
                llProcess.setVisibility(View.VISIBLE);
                for (BuildManageBeans.ProcssManageBean buildProcessManageBean : data.getProcessManages()) {
                    View view = getProcessView(buildProcessManageBean);
                    llProcessAdd.addView(view);
                }
            }
            if (data.getAuditList() != null && data.getAuditList().size() > 0) {
                llExamine.setVisibility(View.VISIBLE);
                for (BuildExaminesBean buildExamineBean : data.getAuditList()) {
                    View view = getExamineView(buildExamineBean);
                    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.getFileName())) {
                    info.setId(buildProcessManageBean.getId() + "-" + fileBean.getFileName());
                    info.setName(fileBean.getFileName());

                }
                if (!TextUtils.isEmpty(fileBean.getPath())) {
                    info.setPath(fileBean.getPath());
                }
                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())) {
            ll_description.setVisibility(View.VISIBLE);
            tv_examine_description.setText(buildExamineBean.getAuditDescription());
        }
        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.getFileName())) {
                    info.setId(buildExamineBean.getId() + "-" + fileBean.getFileName());
                    info.setName(fileBean.getFileName());
                }
                if (!TextUtils.isEmpty(fileBean.getPath())) {
                    info.setPath(fileBean.getPath());
                }
                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());
    }

}
