package cn.estsh.i3plus.pojo.mes.model;

import cn.estsh.i3plus.pojo.mes.bean.MesTaktAlarmContinuousConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesTaktAlarmLevelConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesTaktAlarmRecord;
import lombok.Data;

import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author Wynne.Lu
 * @date 2020/9/28 13:26
 * @desc
 */
@Data
public class AlarmMessage implements Delayed {

    private MesTaktAlarmRecord taktAlarmRecord;

    private List<MesTaktAlarmLevelConfig> levelConfigs;

    private List<MesTaktAlarmContinuousConfig> continuousConfigs;

    private long time;

    public AlarmMessage(MesTaktAlarmRecord taktAlarmRecord, List<MesTaktAlarmLevelConfig> levelConfigs,
                        List<MesTaktAlarmContinuousConfig> continuousConfigs, long time, TimeUnit unit) {
        this.taktAlarmRecord = taktAlarmRecord;
        this.levelConfigs = levelConfigs;
        this.continuousConfigs = continuousConfigs;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        AlarmMessage alarmMessage = (AlarmMessage) o;
        long diff = this.time - alarmMessage.time;
        if (diff <= 0) {
            return -1;
        } else {
            return 1;
        }
    }
}
