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

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gaoyuan.weixinrecord.example.Record;
import com.gaoyuan.weixinrecord.utils.PermissionHelper;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.DeviceAttributesBean;
import com.tofly.ghrq_route.module.trouble.domain.PipeAttributesBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.common.ui.activity.UiCheckAddr;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;
import com.tofly.ghrq_route.common.util.app.AppUtil;
import com.tofly.ghrq_route.common.util.file.FileUtil;
import com.tofly.ghrq_route.common.util.file.FileViewUtil;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleSelectDevice;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleUnderSectaryVerifyAssign;

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

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

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

@SuppressLint("NonConstantResourceId")
public class TroubleReportInfoModuleLayout extends LinearLayout {
    @BindView(R.id.text_addr)
    TextView textAddr;
    @BindView(R.id.layout_device)
    LinearLayout layoutDevice;
    @BindView(R.id.text_device)
    TextView textDevice;
    @BindView(R.id.layout_device_addr)
    LinearLayout layoutDeviceAddr;
    @BindView(R.id.layout_trouble_name)
    LinearLayout layoutTroubleName;
    @BindView(R.id.text_trouble_name)
    TextView textTroubleName;
    @BindView(R.id.layout_upload_time)
    LinearLayout layoutUploadTime;
    @BindView(R.id.text_upload_type)
    TextView textUploadType;
    @BindView(R.id.layout_upload_type)
    LinearLayout layoutUploadType;
    @BindView(R.id.text_upload_time)
    TextView textUploadTime;
    @BindView(R.id.layout_unit_name)
    LinearLayout layoutUnitName;
    @BindView(R.id.text_unit_name)
    TextView textUnitName;
    @BindView(R.id.img_new)
    ImageView imgNew;
    @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_trouble_info)
    LinearLayout layoutTroubleInfo;
    @BindView(R.id.text_trouble_info)
    TextView textTroubleInfo;
    @BindView(R.id.addition_gallery)
    LinearLayout additionGallery;
    @BindView(R.id.layout_addition_area)
    LinearLayout layoutAdditionArea;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;
    @BindView(R.id.btn_action)
    Button btnAction;
    @BindArray(R.array.trouble_report_work_states)
    String[] workStates;

    private Context mContext;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

    private int deviceSelectType = TroubleBean.TROUBLE_SELECT_DEVICE_CHECK;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

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

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

    public TroubleReportInfoModuleLayout(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_report_info_module, this);
        ButterKnife.bind(this);
    }

    private void initListener() {

    }

    public void initTroubleInfo(TroubleBean troubleBean, boolean isEditable, int trouble_select_type) {
        if (troubleBean != null) {
            mTroubleBean = troubleBean;
            this.isEditable = isEditable;
/*            if (trouble_select_type == TroubleBean.TROUBLE_SELECT_ONDUTY_CHECK_PLACE ||
                    trouble_select_type == TroubleBean.TROUBLE_SELECT_ROUTER_CHECK_PLACE) {
                deviceSelectType = TroubleBean.TROUBLE_SELECT_DEVICE_UPDATE;
            }*/
            setViewContent(troubleBean);
        }
    }

    private void setViewContent(TroubleBean troubleBean) {
        int uploadType = troubleBean.getUploadType();
        String troubleId = troubleBean.getId();
        String fileNote = "附件";

        textAddr.setText(troubleBean.getAddress());

        String troubleName = troubleBean.getTroubleName();
        if (troubleName != null && !troubleName.isEmpty()) {
            layoutTroubleName.setVisibility(VISIBLE);
            textTroubleName.setText(troubleName);
        }else {
            layoutTroubleName.setVisibility(GONE);
        }

        String findTime = troubleBean.getFindTime();
        if (findTime != null && !findTime.isEmpty()) {
            layoutUploadTime.setVisibility(VISIBLE);
            textUploadTime.setText(findTime);
        }else {
            layoutUploadTime.setVisibility(GONE);
        }

        String unitName = troubleBean.getUnitName();
        if (unitName != null && !unitName.isEmpty()) {
            layoutUnitName.setVisibility(VISIBLE);
            textUnitName.setText(unitName);
        }else {
            layoutUnitName.setVisibility(GONE);
        }

        String troubleInfo = troubleBean.getTroubleInfo();
        if (troubleInfo != null && !troubleInfo.isEmpty()) {
            layoutTroubleInfo.setVisibility(VISIBLE);
            textTroubleInfo.setText(troubleInfo);
        }else {
            layoutTroubleInfo.setVisibility(GONE);
        }

        additionGallery.removeAllViews();
        /*上报选择为工作日现场*/
        List<AuditFileBean> workingDayFiles = troubleBean.getWorkingDayFiles();///附件
        int workingDayFileCount = 0;
        if (workingDayFiles != null) {
            workingDayFileCount = workingDayFiles.size();
            for (AuditFileBean auditFileBean : workingDayFiles) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }
        List<AuditFileBean> workingDayVoice = troubleBean.getWorkingDayVoice();    ///音频
        int workingDayVoiceCount = 0;
        if (workingDayVoice != null) {
            workingDayVoiceCount = workingDayVoice.size();
            for (AuditFileBean auditFileBean : workingDayVoice) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }
        /*上报选择为非工作日*/
        List<AuditFileBean> nonworkingDayFiles = troubleBean.getNonworkingDayFiles();    ///附件
        int nonworkingDayFileCount = 0;
        if (nonworkingDayFiles != null) {
            nonworkingDayFileCount = nonworkingDayFiles.size();
            for (AuditFileBean auditFileBean : nonworkingDayFiles) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }
        List<AuditFileBean> nonworkingDayVoice = troubleBean.getNonworkingDayVoice();    ///音频
        int nonworkingDayVoiceCount = 0;
        if (nonworkingDayVoice != null) {
            nonworkingDayVoiceCount = nonworkingDayVoice.size();
            for (AuditFileBean auditFileBean : nonworkingDayVoice) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }
        /*上报选择为工作日非现场*/
        List<AuditFileBean> workingOtherFiles = troubleBean.getWorkingOtherFiles();    ///附件
        int workingOtherFileCount = 0;
        if (workingOtherFiles != null) {
            workingOtherFileCount = workingOtherFiles.size();
            for (AuditFileBean auditFileBean : workingOtherFiles) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }
        List<AuditFileBean> workingOtherVoice = troubleBean.getWorkingOtherVoice();    ///音频
        int workingOtherVoiceCount = 0;
        if (workingOtherVoice != null) {
            workingOtherVoiceCount = workingOtherVoice.size();
            for (AuditFileBean auditFileBean : workingOtherVoice) {
                getSchemeFiles(troubleId, auditFileBean, fileNote, additionGallery);
            }
        }

        if (workingDayFileCount > 0 || workingDayVoiceCount > 0 ||
                nonworkingDayFileCount > 0 || nonworkingDayVoiceCount > 0 ||
                workingOtherFileCount > 0 || workingOtherVoiceCount > 0) {
            layoutAdditionArea.setVisibility(View.VISIBLE);
        } else {
            layoutAdditionArea.setVisibility(View.GONE);
        }

        String troubleTypeName = troubleBean.getTroubleTypeName();
        if (troubleTypeName != null && !troubleTypeName.isEmpty()) {
            layoutTroubleType.setVisibility(VISIBLE);
            textTroubleType.setText(troubleTypeName);
        }else {
            layoutTroubleType.setVisibility(GONE);
        }

        String levelName = troubleBean.getLevelName();
        if (levelName != null && !levelName.isEmpty()) {
            layoutTroubleLevel.setVisibility(VISIBLE);
            textTroubleLevel.setText(levelName);
        }else {
            layoutTroubleLevel.setVisibility(GONE);
        }

        setDeviceContent(troubleBean);
        setActionButton(troubleBean);
    }

    private void setDeviceContent(TroubleBean troubleBean) {
        int pipe_count = 0;
        List<PipeAttributesBean> pipeInfos = troubleBean.getPipeInfos();
        if (pipeInfos != null && !pipeInfos.isEmpty()) {
            pipe_count = pipeInfos.size();
        }

        int device_count = 0;
        List<DeviceAttributesBean> deviceInfos = troubleBean.getDeviceInfos();
        if (deviceInfos != null && !deviceInfos.isEmpty()) {
            device_count = deviceInfos.size();
        }

        String strDeviceInfo = String.format(Locale.CHINA, "%d个设备 | %d个管线", device_count, pipe_count);
        textDevice.setText(strDeviceInfo);
        if (pipe_count > 0 || device_count > 0) {
            textDevice.setTextColor(mContext.getResources().getColor(R.color.colorPrimary_new));
            layoutDevice.setOnClickListener(view -> checkDeviceDetail());
        } else {
            textDevice.setTextColor(mContext.getResources().getColor(R.color.text));
        }
    }

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

        layoutAction.setVisibility(GONE);

        int processId = troubleBean.getProcessId();
        if (processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDERTAKER_VERIFY ||
                processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDERTAKER_MAKE_SCHEME) {
            layoutAction.setVisibility(VISIBLE);
            btnAction.setOnClickListener(view -> goToUiUndertakerVerify());
        }
    }

    private void goToUiUndertakerVerify() {
        Intent intent = new Intent();
        intent.setClass(mContext, UiTroubleUnderSectaryVerifyAssign.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTroubleBean);
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }

    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 if (FileUtil.isFileTypeVideo(fileName)) {
                fileViewUtil.getVideoFileView(schemeId, filePath, fileName, fileNote, getViewCallback);
            } else {
                fileViewUtil.getFileView(schemeId, filePath, fileName, fileNote, getViewCallback);
            }
        }
    }


    private void checkDeviceDetail() {
        Intent intent = new Intent();
        intent.setClass(mContext, UiTroubleSelectDevice.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTroubleBean);
        bundle.putInt(TroubleBean.TROUBLE_BEAN_SELECT_DEVICE_TYPE, deviceSelectType);
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }
}
