package com.gyee.alarm.rule;


import com.gyee.alarm.model.vo.DeviceTypeValue;
import com.gyee.alarm.rule.expression.AlarmExpression;
import com.gyee.alarm.util.realtimesource.timeseries.DoubleTsData;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;


// 判定状态（表达式成立）持续的时间是否超过给定的时间
// 应用场景： 判断状态持续的时间
// 函数原型： Sustain（expression，时间（秒））
@Data
@NoArgsConstructor
@Slf4j
public class AlarmFunctionSustain extends AlarmFunction {

    private boolean lastUpdateStatus;
    private long lastUpdateTs;
    private long originalTs;
    private long interval; //时间范围，单位秒

    public AlarmFunctionSustain(AlarmExpression alarmExpression, DeviceTypeValue tType, String tId) {
        super(alarmExpression, tType, tId);
        String val = alarmExpression.getFunParams().get(0);
        interval = Long.valueOf(val);
    }


    private String firstId = "";

    @Override
    public Object explain() {

        long ts = getLastExplainTime();
        if (originalTs == 0)
            originalTs = ts;

        lastUpdateTs = ts;

        Object obj = super.explain();
        if (obj.equals(Boolean.TRUE)) {
            if (lastUpdateStatus == false) {
                originalTs = ts;
                lastUpdateStatus = true;
                return false;
            } else {
                long tmp = lastUpdateTs - originalTs;
                if (tmp > interval * 1000)
                    return true;
                else
                    return false;
            }
        } else {
            lastUpdateStatus = false;
            //originalTs = ts;
            return false;
        }
    }


    public DoubleTsData getValue(String varName) {
        return null;
    }
}
