package com.example.iot_controlhost.model;

import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.R;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.network.PublicNetRequest;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;

public class DeviceErrorInfo {

    /**
     * 一级-出错次数-则弹窗+微信
     */
    private static final int errorCheckTimes1 = 3;
    /**
     * 二级-出错次数-则弹窗+微信+语音
     */
    public static final int errorCheckTimes2 = 6;
    /**
     * 设备
     */
    private String name;
    /**
     * 值-上次
     */
    private double value;
    /**
     * 开关状态-上次
     */
    private boolean open;
    /**
     * 操作类型-上次
     */
    private int operateType;
    /**
     * 出错次数
     */
    private int errorTimes;
    /**
     * 错误信息（如果有）
     */
    private String errorMessage;
    /**
     * 错误信息（不需要时间）
     */
    private String errorMessageNoTime;
    /**
     * 警告图标样式
     */
    private int resId;

    public DeviceErrorInfo(String name, double value) {
        this.name = name;
        this.value = value;
        this.open = false;
        this.operateType = 1;
        errorTimes = 0;
        this.resId = R.mipmap.warning_other;
    }

    public DeviceErrorInfo(String name, double value, int resId) {
        this(name, value);
        this.resId = resId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public int getErrorTimes() {
        return errorTimes;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public void setErrorTimes(int errorTimes) {
        this.errorTimes = errorTimes;
    }

    /**
     * 检查是否需要实施报警措施
     *
     */
    public void checkNotice() {
        checkNotice(3);
    }

    /**
     * 检查是否需要实施报警措施
     * @param noticeType 1数据采集故障 2设备操作故障 3设备疑似故障
     */
    public void checkNotice(int noticeType) {
        if (StringUtils.isEmpty(getErrorMessage())) {
            //已正常
            clearErrorTimes();
            RxBusUtils.get().post(RxTag.MAIN_WARNING, this);
        } else {
            errorTimes++;
            MyLog.warningError(getName() + getErrorTimes() + "次-" + getErrorMessage());
            // 只有在一级和二级次数允许时 才预警
            if (getErrorTimes() == errorCheckTimes1 || getErrorTimes() == errorCheckTimes2) {
                // 弹窗
                sentDialog();
                // 公众号提示
                int noticeTypeCode = 0;
                if (getErrorTimes() == errorCheckTimes2) {
                    // 语音通知
                    noticeTypeCode = noticeType;
                    //重新计数，但不删除错误信息，即界面仍会显示错误信息，
                    this.errorTimes = 0;
                }
                PublicNetRequest.AnomalyNotice(getName(), noticeTypeCode, 3, getErrorMessageNoTime());
            } else {
                MyLog.warning(getName() + "错误次数：" + getErrorTimes() + "次,暂不符合预警触发条件");
            }
        }
    }

    /**
     * 显示弹窗
     */
    private void sentDialog() {
        UserLog.operateError(getErrorMessage());
        RxBusUtils.get().post(RxTag.MAIN_WARNING, this);
    }

    public void clearErrorTimes() {
        setErrorMessage(null);
        this.errorTimes = 0;
    }

    public int getOperateType() {
        return operateType;
    }

    public void setOperateType(int operateType) {
        this.operateType = operateType;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = StringUtils.isEmpty(errorMessage) ? errorMessage : MyUtil.getDateTime() + "\n" + errorMessage;
        setErrorMessageNoTime(errorMessage);
    }

    public String getErrorMessageNoTime() {
        return errorMessageNoTime;
    }

    public void setErrorMessageNoTime(String errorMessageNoTime) {
        this.errorMessageNoTime = errorMessageNoTime;
    }
}