package com.gyee.alarm.rule;



import com.gyee.alarm.model.vo.DeviceTypeValue;
import com.gyee.alarm.rule.expression.AlarmExpression;
import com.gyee.common.model.PointData;

import java.util.Optional;


public class AlarmFunctionMIN extends AlarmFunction {
    public AlarmFunctionMIN(AlarmExpression alarmExpression, DeviceTypeValue tType, String tId) {
        super(alarmExpression, tType, tId);
    }

    @Override
    public Object explain() {
        if (this.getAlarmExpression().getVarList() == null || this.getAlarmExpression().getVarList().size() < 1)
            return 0;

        Optional<Double> minValue = Optional.empty();

        for (int i = 0; i < this.getAlarmExpression().getVarList().size(); i++) {
            PointData tagInfo = this.getTagInfo(this.getAlarmExpression().getVarList().get(i));
            if (!minValue.isPresent()) {
                minValue = Optional.of(tagInfo.getPointValueInDouble());
            } else {
                if (minValue.get() >= tagInfo.getPointValueInDouble()) {
                    minValue = Optional.of(tagInfo.getPointValueInDouble());
                }
            }
        }

        return minValue.get().doubleValue();
    }

}
