package com.tofly.ghrq_route.module.emergencyHandle.ui.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gaoyuan.weixinrecord.example.Record;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.ScheduleExecuteBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.common.util.ContextUtil;
import com.tofly.ghrq_route.common.util.file.FileUtil;
import com.tofly.ghrq_route.common.util.file.FileViewUtil;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/2/21.
 */

@SuppressLint("NonConstantResourceId")
public class EmergencyAssignHandleInfoModuleLayout extends LinearLayout {
    @BindView(R.id.text_reporter)
    TextView textReporter;
    @BindView(R.id.text_report_time)
    TextView textReportTime;
    @BindView(R.id.text_is_maintenance)
    TextView textIsMaintenance;
    @BindView(R.id.text_is_audit)
    TextView textIsAudit;
    @BindView(R.id.text_over_time)
    TextView textOverTime;
    @BindView(R.id.layout_over_time)
    LinearLayout layoutOverTime;
    @BindView(R.id.text_handle_note)
    TextView textHandleNote;
    @BindView(R.id.layout_handle_note)
    LinearLayout layoutHandleNote;
    @BindView(R.id.sign_gallery)
    LinearLayout signGallery;
    @BindView(R.id.layout_sign_area)
    LinearLayout layoutSignArea;
    @BindView(R.id.addition_gallery)
    LinearLayout additionGallery;
    @BindView(R.id.layout_addition_area)
    LinearLayout layoutAdditionArea;

    private final Context mContext;

    private final FileViewUtil fileViewUtil;

    private EmergencyHandleData mEmergencyHandleData;

    private final List<Record> mRecords;

    public EmergencyAssignHandleInfoModuleLayout(Context context) {
        this(context, null);
    }

    public EmergencyAssignHandleInfoModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmergencyAssignHandleInfoModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;
        Activity activity = ContextUtil.getActivity(mContext);
        fileViewUtil = new FileViewUtil(activity);
        mRecords = new ArrayList<>();

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_emergency_assign_handle_info_module, this);
        ButterKnife.bind(this);
    }

    private void initListener() {

    }

    public void initEmergencyInfo(EmergencyHandleData emergencyData) {
        if (emergencyData != null) {
            mEmergencyHandleData = emergencyData;
            setViewContent(emergencyData);
        }
    }

    private void setViewContent(EmergencyHandleData emergencyData) {
        /*上报人*/
        String dispatchUserName = emergencyData.getDispatchUserName();
        textReporter.setText(dispatchUserName);

        /*上报时间*/
        String completeTime = emergencyData.getCompleteTime();
        textReportTime.setText(completeTime);

        /*是否抢修*/
        int isMaintenance = emergencyData.getIsMaintenance();
        textIsMaintenance.setText(isMaintenance == 0 ? "不抢修" : "需要抢修");

        /*是否审核*/
        int auditFlag = emergencyData.getAuditFlag();
        textIsAudit.setText(auditFlag == 0 ? "不审核" : "需要审核");

        /*到位时间*/
        String overTime = emergencyData.getOverTime();
        if (overTime != null && !overTime.isEmpty()) {
            layoutOverTime.setVisibility(VISIBLE);
            textOverTime.setText(overTime);
        }else {
            layoutOverTime.setVisibility(GONE);
        }

        /*处置情况*/
        String emergencyHandling = emergencyData.getEmergencyHandling();
        if (emergencyHandling != null && !emergencyHandling.isEmpty()) {
            layoutHandleNote.setVisibility(VISIBLE);
            textHandleNote.setText(emergencyHandling);
        }else {
            layoutHandleNote.setVisibility(GONE);
        }

        /*附件*/
        String emergencyId = emergencyData.getId() + "";
        String fileNote = "附件";

        signGallery.removeAllViews();
        List<AuditFileBean> signFiles = emergencyData.getConfirmingTheLink();  ///签名文件
        int signCount = 0;
        if (signFiles != null) {
            signCount = signFiles.size();
            for (AuditFileBean auditFileBean : signFiles) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, signGallery);
            }
        }
        if (isMaintenance == 0 || signCount > 0) {
            layoutSignArea.setVisibility(View.VISIBLE);
        } else {
            layoutSignArea.setVisibility(View.GONE);
        }

        additionGallery.removeAllViews();
        List<AuditFileBean> emergencyFiles = emergencyData.getEmergencyFile();  ///附件
        int fileCount = 0;
        if (emergencyFiles != null) {
            fileCount = emergencyFiles.size();
            for (AuditFileBean auditFileBean : emergencyFiles) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
            }
        }

        List<AuditFileBean> emergencyVoices = emergencyData.getEmergencyVoice();  ///附件
        if (emergencyVoices != null) {
            fileCount = fileCount + emergencyVoices.size();
            for (AuditFileBean auditFileBean : emergencyVoices) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
            }
        }

        if (fileCount > 0) {
            layoutAdditionArea.setVisibility(View.VISIBLE);
        } else {
            layoutAdditionArea.setVisibility(View.GONE);
        }
    }

    public void initEmergencyInfo(EmergencyHandleData emergencyData, ScheduleExecuteBean executeBean) {
        if (emergencyData != null) {
            mEmergencyHandleData = emergencyData;
            setViewContent(emergencyData, executeBean);
        }
    }

    private void setViewContent(EmergencyHandleData emergencyData, ScheduleExecuteBean executeBean) {
        /*上报人*/
        String dispatchUserName = emergencyData.getDispatchUserName();
        textReporter.setText(dispatchUserName);

        /*上报时间*/
        String completeTime = executeBean.getCompleteTime();
        textReportTime.setText(completeTime);

        /*是否抢修*/
        int isMaintenance = executeBean.getIsMaintenance();
        textIsMaintenance.setText(isMaintenance == 0 ? "不抢修" : "需要抢修");

        /*是否审核*/
        int auditFlag = executeBean.getAuditFlag();
        textIsAudit.setText(auditFlag == 0 ? "不审核" : "需要审核");

        /*到位时间*/
        String overTime = executeBean.getOverTime();
        if (overTime != null && !overTime.isEmpty()) {
            layoutOverTime.setVisibility(VISIBLE);
            textOverTime.setText(overTime);
        }else {
            layoutOverTime.setVisibility(GONE);
        }

        /*处置情况*/
        String emergencyHandling = executeBean.getEmergencyHandling();
        if (emergencyHandling != null && !emergencyHandling.isEmpty()) {
            layoutHandleNote.setVisibility(VISIBLE);
            textHandleNote.setText(emergencyHandling);
        }else {
            layoutHandleNote.setVisibility(GONE);
        }

        /*附件*/
        String emergencyId = executeBean.getId() + "";
        String fileNote = "附件";

        fileViewUtil.clearAllImages();
        signGallery.removeAllViews();
        List<AuditFileBean> signFiles = executeBean.getConfirmingTheLinkList();  ///签名文件
        int signCount = 0;
        if (signFiles != null) {
            signCount = signFiles.size();
            for (AuditFileBean auditFileBean : signFiles) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, signGallery);
            }
        }
        if (isMaintenance == 0 || signCount > 0) {
            layoutSignArea.setVisibility(View.VISIBLE);
        } else {
            layoutSignArea.setVisibility(View.GONE);
        }

        additionGallery.removeAllViews();
        List<AuditFileBean> emergencyFiles = executeBean.getEmergencyFileList();  ///附件
        int fileCount = 0;
        if (emergencyFiles != null) {
            fileCount = emergencyFiles.size();
            for (AuditFileBean auditFileBean : emergencyFiles) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
            }
        }

        List<AuditFileBean> emergencyVoices = executeBean.getEmergencyVoiceList();  ///附件
        if (emergencyVoices != null) {
            fileCount = fileCount + emergencyVoices.size();
            for (AuditFileBean auditFileBean : emergencyVoices) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
            }
        }

        if (fileCount > 0) {
            layoutAdditionArea.setVisibility(View.VISIBLE);
        } else {
            layoutAdditionArea.setVisibility(View.GONE);
        }
    }

    private void getSchemeFiles(String schemeId, AuditFileBean fileBean, String fileNote, final ViewGroup rootView) {
        if (fileBean != null) {
            FileViewUtil.GetViewCallback getViewCallback = (isSuccess, view) -> {
                if (isSuccess && view != null) {
                    rootView.addView(view);
                }
            };

            String fileName = fileBean.getFileName();
            String filePath = fileBean.getPath();
            if (FileUtil.isFileTypeImage(fileName)) {
                fileViewUtil.getImageFileView(filePath, getViewCallback);
            } else if (FileUtil.isFileTypeAudio(fileName)) {
                fileViewUtil.getAudioFileView(schemeId, filePath, 1, fileNote, mRecords, null, getViewCallback);
            } else {
                fileViewUtil.getFileView(schemeId, filePath, fileName, fileNote, getViewCallback);
            }
        }
    }
}
