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;

/**
 * @descrition:传入多个测点(统一编码)获取最大值
 * @author:Wanghs
 * @date:2020-04-07
 */
public class AlarmFunctionMAX extends AlarmFunction {
    public AlarmFunctionMAX(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> maxValue = Optional.empty();

        for (int i = 0; i < this.getAlarmExpression().getVarList().size(); i++) {
            PointData tagInfo = this.getTagInfo(this.getAlarmExpression().getVarList().get(i));

            if (tagInfo == null) {
                continue;
            }

            //System.out.println("@@@@@" + this.getAlarmExpression().getVarList().get(i));
            if (!maxValue.isPresent()) {
                maxValue = Optional.of(tagInfo.getPointValueInDouble());
            } else {
                if (maxValue.get() <= tagInfo.getPointValueInDouble()) {
                    maxValue = Optional.of(tagInfo.getPointValueInDouble());
                }
            }
        }

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

}
