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.buildFilesInfo;
import com.tofly.ghrq_route.module.buildManage.domain.VoiceBeans;
import com.tofly.ghrq_route.module.rushrepair.controller.RushRepairCallback;
import com.tofly.ghrq_route.module.rushrepair.controller.RushRepairController;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairBeanList;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairExamineList;
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 UiRepairDetail 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_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.tv_part)
    TextView tvPart;
    @BindView(R.id.tv_rush_note)
    TextView tvRushNote;
    @BindView(R.id.tv_note)
    TextView tvNote;

    private RushRepairBeanList data;


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

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

    private void getData(String id) {
        RushRepairController rushRepairController = new RushRepairController(this);
        rushRepairController.getDataDetail(id, new RushRepairCallback() {
            @Override
            public void onRepairDetail(boolean isSuccess, int errorCode, String errMsg, RushRepairBeanList buildManage, int total) {
                super.onRepairDetail(isSuccess, errorCode, errMsg, buildManage, total);
                if (isSuccess) {
                    data = buildManage;
                    initView();
                }
            }
        });
    }

    private void initView() {
        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.getResponsibleName())) {
                tvDispatch.setText(data.getResponsibleName());
            } else {
                tvDispatch.setVisibility(View.GONE);
                llDispatch.setVisibility(View.GONE);
            }
            /*派发时间*/
            if (!TextUtils.isEmpty(data.getDispatchDate())) {
                tvDispatchTime.setText(data.getDispatchDate());
            } else {
                tvDispatchTime.setVisibility(View.GONE);
            }
            /*参与人员*/
            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());
            }
            List<buildFilesInfo> appendList = new ArrayList<>();
            if (data.getVoice() != null && data.getVoice().size() > 0) {
                data.getFiles().addAll(data.getVoice());
            }
            if (data.getFiles() != null && data.getFiles().size() > 0) {
                for (VoiceBeans fileBean : data.getFiles()) {
                    buildFilesInfo info = new buildFilesInfo();
                    if (!TextUtils.isEmpty(fileBean.getPath())) {
                        info.setPath(fileBean.getPath());
                    }
                    if (!TextUtils.isEmpty(fileBean.getFileName())) {
                        info.setId(data.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 (data.getProcessManages() != null && data.getProcessManages().size() > 0) {
                llProcess.setVisibility(View.VISIBLE);
                for (RushRepairBeanList.ProcssManageBean rushRepairProcessBean : data.getProcessManages()) {
                    View view = getProcessView(rushRepairProcessBean);
                    llProcessAdd.addView(view);
                }
            }
            if (data.getAuditList() != null && data.getAuditList().size() > 0) {
                llExamine.setVisibility(View.VISIBLE);
                for (RushRepairExamineList repairExamineBean : data.getAuditList()) {
                    View view = getExamineView(repairExamineBean);
                    llExamineAdd.addView(view);
                }
            }
        }
    }

    private View getProcessView(RushRepairBeanList.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(data.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(RushRepairExamineList 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.getPath())) {
                    info.setPath(fileBean.getPath());
                }
                if (!TextUtils.isEmpty(fileBean.getFileName())) {
                    info.setName(fileBean.getFileName());
                    info.setId(data.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());
    }
}
