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

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
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.
 */

public class TroubleVerifyRouteModuleLayout extends LinearLayout {
    @BindView(R.id.text_taker_name)
    TextView textTakerName;
    @BindView(R.id.text_verify_time)
    TextView textVerifyTime;
    @BindView(R.id.text_verify_note)
    TextView textVerifyNote;
    @BindView(R.id.layout_verify_module)
    LinearLayout layoutVerifyModule;
    @BindView(R.id.text_router_name)
    TextView textRouterName;
    @BindView(R.id.text_check_time)
    TextView textCheckTime;
    @BindView(R.id.text_is_trouble)
    TextView textIsTrouble;
    @BindView(R.id.text_trouble_kind)
    TextView textTroubleKind;
    @BindView(R.id.layout_trouble_kind)
    LinearLayout layoutTroubleKind;
    @BindView(R.id.text_trouble_type)
    TextView textTroubleType;
    @BindView(R.id.layout_trouble_type)
    LinearLayout layoutTroubleType;
    @BindView(R.id.text_trouble_level)
    TextView textTroubleLevel;
    @BindView(R.id.layout_trouble_level)
    LinearLayout layoutTroubleLevel;
    @BindView(R.id.layout_handle_note)
    LinearLayout layoutHandleNote;
    @BindView(R.id.text_handle_note)
    TextView textHandleNote;
    @BindView(R.id.layout_check_note)
    LinearLayout layoutCheckNote;
    @BindView(R.id.text_check_note)
    TextView textCheckNote;
    @BindView(R.id.addition_gallery)
    LinearLayout additionGallery;
    @BindView(R.id.layout_addition_area)
    LinearLayout layoutAdditionArea;
    @BindView(R.id.layout_check_report_module)
    LinearLayout layoutCheckReportModule;
    @BindView(R.id.btn_action)
    Button btnAction;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;
    private Context mContext;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

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

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

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

        mContext = context;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();

        initView();
        initListener();
    }

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

    private void initListener() {

    }

    public void initTroubleInfo(TroubleBean troubleBean, boolean isEditable) {
        if (troubleBean != null) {
            mTroubleBean = troubleBean;
            this.isEditable = isEditable;
            setViewContent(troubleBean);
        }
    }

    private void setViewContent(TroubleBean troubleBean) {
        int uploadType = troubleBean.getUploadType();
        int processId = troubleBean.getProcessId();
        String troubleId = troubleBean.getId();

/*        if (processId <= TroubleBean.TROUBLE_PROCESS_NEED_ROUTER_CHECK_PLACE) {
            layoutCheckReportModule.setVisibility(GONE);
        }*/

        if (uploadType == 2) {  //工作日非现场
            layoutVerifyModule.setVisibility(VISIBLE);
            textTakerName.setText(troubleBean.getUndertakerUserName());
            String verify_time = troubleBean.getUndertakerCheckTime();
            if (verify_time != null)
                textVerifyTime.setText(verify_time);
            String checkNotes = troubleBean.getUndertakerCheckNotes();
            if (checkNotes != null)
                textVerifyNote.setText(checkNotes);
        }else {
            layoutVerifyModule.setVisibility(GONE);
        }

        layoutHandleNote.setVisibility(GONE);
        layoutCheckNote.setVisibility(GONE);

        String fileNote = "附件";
        additionGallery.removeAllViews();
        if (uploadType == 2) {  //工作日非现场
            textRouterName.setText(troubleBean.getUndertakerHandleUserName());
            String check_time = troubleBean.getUndertakerHandleTime();
            if (check_time != null)
                textCheckTime.setText(check_time);
            String handleNotes = troubleBean.getUndertakerHandleNotes();
            if (handleNotes != null && !handleNotes.isEmpty()) {
                layoutCheckNote.setVisibility(VISIBLE);
                textCheckNote.setText(handleNotes);
            }

/*            boolean isNotTrouble = (processId == TroubleBean.TROUBLE_PROCESS_ROUTER_CONFIRM_NOT_TROUBLE);
            String stateName =  isNotTrouble ? getResources().getString(R.string.no) : getResources().getString(R.string.yes);
            textIsTrouble.setText(stateName);
            if (isNotTrouble) {
                layoutTroubleKind.setVisibility(GONE);
                layoutTroubleType.setVisibility(GONE);
                layoutTroubleLevel.setVisibility(GONE);
            }*/

            List<AuditFileBean> auditSchemeFiles = troubleBean.getInspectFiles();    ///附件
            int auditSchemeFileCount = 0;
            if (auditSchemeFiles != null) {
                auditSchemeFileCount = auditSchemeFiles.size();
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
                }
            }
            List<AuditFileBean> auditSchemeVoice = troubleBean.getInspectVoice();    ///音频
            int auditSchemeVoiceCount = 0;
            if (auditSchemeVoice != null) {
                auditSchemeVoiceCount = auditSchemeVoice.size();
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
                }
            }

            if (auditSchemeFileCount > 0 || auditSchemeVoiceCount > 0) {
                layoutAdditionArea.setVisibility(View.VISIBLE);
            } else {
                layoutAdditionArea.setVisibility(View.GONE);
            }
        }else if (uploadType == 0) {  //非工作日
            textRouterName.setText(troubleBean.getOnDutyUserName());
            String check_time = troubleBean.getReportTime();
            if (check_time != null)
                textCheckTime.setText(check_time);
            textCheckTime.setVisibility(GONE);
            String handleNotes = troubleBean.getUndertakerHandleNotes();
            if (handleNotes != null && !handleNotes.isEmpty()) {
                layoutCheckNote.setVisibility(VISIBLE);
                textCheckNote.setText(handleNotes);
            }
/*            boolean isNotTrouble = (processId == TroubleBean.TROUBLE_PROCESS_ONDUTY_CONFIRM_NOT_TROUBLE);
            String stateName = isNotTrouble ? getResources().getString(R.string.no) : getResources().getString(R.string.yes);
            textIsTrouble.setText(stateName);
            if (isNotTrouble) {
                layoutTroubleKind.setVisibility(GONE);
                layoutTroubleType.setVisibility(GONE);
                layoutTroubleLevel.setVisibility(GONE);
            }*/

            String handleProject = troubleBean.getTempHandleProject();
            if (handleProject != null)
                textHandleNote.setText(handleProject);
            layoutHandleNote.setVisibility(VISIBLE);

            List<AuditFileBean> auditSchemeFiles = troubleBean.getDutyFiles();    ///附件
            int auditSchemeFileCount = 0;
            if (auditSchemeFiles != null) {
                auditSchemeFileCount = auditSchemeFiles.size();
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
                }
            }
            List<AuditFileBean> auditSchemeVoice = troubleBean.getDutyVoice();    ///音频
            int auditSchemeVoiceCount = 0;
            if (auditSchemeVoice != null) {
                auditSchemeVoiceCount = auditSchemeVoice.size();
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
                }
            }

            if (auditSchemeFileCount > 0 || auditSchemeVoiceCount > 0) {
                layoutAdditionArea.setVisibility(View.VISIBLE);
            } else {
                layoutAdditionArea.setVisibility(View.GONE);
            }
        }

        String troubleKindName = troubleBean.getEventTypeName();  //重大隐患，一般隐患
            textTroubleKind.setText(troubleKindName);
        String troubleTypeName = troubleBean.getTroubleTypeName();
        if (troubleTypeName != null)
            textTroubleType.setText(troubleTypeName);
        String levelName = troubleBean.getLevelName();
        if (levelName != null)
            textTroubleLevel.setText(levelName);
        setActionButton(troubleBean);
    }


    private void setActionButton(TroubleBean troubleBean) {
        if (!isEditable) {
            layoutAction.setVisibility(GONE);
            return;
        }

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, troubleBean);
        intent.putExtras(bundle);

/*        int processId = troubleBean.getProcessId();
        if (processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_ASSIGN ||
                processId == TroubleBean.TROUBLE_PROCESS_NEED_ROUTER_CHECK_PLACE) {
            layoutAction.setVisibility(VISIBLE);
            if (processId == TroubleBean.TROUBLE_PROCESS_NEED_ROUTER_CHECK_PLACE) {
                btnAction.setText(mContext.getString(R.string.trouble_check_place));
                intent.setClass(mContext, UiTroubleUndertakerHandle.class);
            } else if (processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_ASSIGN) {
                btnAction.setText(mContext.getString(R.string.trouble_under_sectary_verify));
                intent.setClass(mContext, UiTroubleUnderSectaryVerifyAssign.class);
            }
        } else {
            layoutAction.setVisibility(GONE);
        }*/

        btnAction.setOnClickListener(view -> {
            mContext.startActivity(intent);
        });
    }

    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);
            }
        }
    }
}
