package com.gyee.alarm.model.vo;

import com.gyee.alarm.init.CacheContext;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Data
@Slf4j
public class AlarmTag {


    /**
     * 表名
     */
    private String id;

    /**
     * 时间戳
     */
    private Long ts;

    /**
     * 当前值
     */
    private Double val;

    /**
     * 报警类别，0触发，1触发，-1变化触发，大于等于3ai点触发
     */
    private Integer triggerType;

    /**
     * 测点
     */
    private String tagId;

    /**
     * 过去值
     */
    private Double oval;

    /**
     * 报警级别
     */
    private Integer rank;
    /**
     * 结束时间戳
     */
    private Long endts;
    /**
     * 报警时长
     */
    private Double timeLong;
    /**
     * 报警描述
     */
    private String description;


    private  Boolean confirmed;

    private String superTalbe;

    /**
     * 故障原因
     */
    private String faultCause;
    /**
     * 类型
     */
    private String characteristic;
    private String alarmId;
    private String deviceId;
    /**
     * 解决方法
     */
    private String resolvent;

    /**
     * 报警类型 /自定义报警 custom，风机 windturbine，场站 station，升压站 booststation
     */
    private String alarmType;

    /**
     * 设备类型，场站 station，风机 windturbine，升压站 booststation
     */
    private String deviceType;

    private Boolean isClose;

    private String wpName;

    private String wpId;
    /**
     * 统一识别码(如果通过uniformCode配置，则设备类型，场站，机型不能为空)
     */
    private String uniformCode;
    /**
     * 更新并检测是否触发报警
     */
    public synchronized Boolean updateAndCheck(PointData pointData) {

        if(alarmType.equals(AlarmTypeValue.BT.getCode()) && StringUtils.notEmp(wpId) && wpId.contains("_SBS"))
        {
            if(pointData.getPointValueInDouble()<1)
            {
                pointData.setPointValueInDouble(0.0);
            }else
            {
                pointData.setPointValueInDouble(1.0);
            }
        }

        if(null == oval)
        {
            oval=pointData.getPointValueInDouble();
            return false;
        }


        if (pointData.getPointValueInDouble() == oval.doubleValue()) {
            return false;
        }

        if (StringUtils.empty(pointData.getPointTime())) {
            return false;
        }

        if (pointData.getPointTime().intValue()==0) {
            return false;
        }
        if(StringUtils.notEmp(wpId) && wpId.contains("_SBS"))
        {
            log.info("判定相等报警tagId_"+tagId+"_wpId_"+wpId+"_ts_"+ts+"_val_"+oval.doubleValue());
        }
        ts = pointData.getPointTime();
        oval = pointData.getPointValueInDouble();
        endts= pointData.getPointTime();
        val=0.0;


        if(StringUtils.notEmp(wpId) && wpId.contains("_SBS"))
        {
            log.info("报警发生tagId_"+tagId+"_wpId_"+wpId+"_ts_"+ts+"_val_"+pointData.getPointValueInDouble());
        }
        if(StringUtils.notEmp(uniformCode) && uniformCode.startsWith("BI")){

            int decimal = oval.intValue(); // 十进制报警数

            if(decimal==0)
            {
                val=0.0;
            }

            String binary = intToBinary32(decimal, 32); // 转换为二进制报警字符串

            String reverse = new StringBuffer(binary).reverse().toString();//翻转字符串，便于报警分析
            int value = (int) reverse.charAt(triggerType.intValue())-48;
            if (value == 1) {
                val=1.0;
            }else {
                val=0.0;
            }


        }else  if(triggerType.intValue()==-1)
        {
            val=1.0;
        }else  if(triggerType.intValue()==0)
        {
            if (oval.intValue() == 0)
            {
                val=1.0;
            }else
            {
                val=0.0;

            }
        }else  if(triggerType.intValue()==1)
        {
            if (oval.intValue() == 1)
            {
                val=1.0;
            }else
            {
                val=0.0;

            }
        }else {
            if (oval.intValue() == triggerType.intValue()) {
                val=1.0;
            }else {
                val=2.0;
            }

        }

        return true;
    }


    public synchronized Boolean updateAndCheckNoInfo(PointData pointData) {

        if(alarmType.equals(AlarmTypeValue.BT.getCode()))
        {
            if(pointData.getPointValueInDouble()<1)
            {
                pointData.setPointValueInDouble(0.0);
            }else
            {
                pointData.setPointValueInDouble(1.0);
            }
        }
        ts = pointData.getPointTime();
        oval = pointData.getPointValueInDouble();
        endts= pointData.getPointTime();
        val=0.0;

//        if(StringUtils.notEmp(wpId) && wpId.contains("_SBS"))
//        {
//            log.info("tagId_"+tagId+"_wpId_"+wpId+"_ts_"+ts+"_val_"+pointData.getPointValueInDouble());
//        }
        if(StringUtils.notEmp(uniformCode) && uniformCode.startsWith("BI")){

            int decimal = oval.intValue(); // 十进制报警数
            String binary = intToBinary32(decimal, 32); // 转换为二进制报警字符串

            String reverse = new StringBuffer(binary).reverse().toString();//翻转字符串，便于报警分析
            int value = (int) reverse.charAt(triggerType.intValue())-48;
            if (value == 1) {
                val=1.0;
            }else {
                val=0.0;
            }

        }else if(triggerType.intValue()==-1)
        {
            val=1.0;
        }else  if(triggerType.intValue()==0)
        {
            if (oval.intValue() == 0)
            {
                val=1.0;
            }else
            {
                val=0.0;

            }
        }else  if(triggerType.intValue()==1)
        {
            if (oval.intValue() == 1)
            {
                val=1.0;
            }else
            {
                val=0.0;

            }
        }else {
            if (oval.intValue() == triggerType) {
                val=1.0;
            }else {
                val=2.0;
            }

        }

        return true;
    }


    // i 是要转换的整数，bitNum 是期望的二进制字符串位数
    public  String intToBinary32(int i, int bitNum) {
        String binaryStr = Integer.toBinaryString(i);
        while (binaryStr.length() < bitNum) {
            binaryStr = "0" + binaryStr;
        }
        return binaryStr;
    }
}
