package com.smppw.core.reta.calc;


import com.smppw.common.pojo.dto.DateValue;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.core.reta.Rate;
import com.smppw.utils.DateUtil;

import java.util.List;

public class LongestNoNewHighDays extends Rate {

    @Override
    public Double calc(List<DateValue> dateValues) {
        Double lastHigh = null;
        String lastHighDate = null;
        int longDateDistance = 0;
        for (DateValue dateValue : dateValues) {
            if (dateValue.getValue() == null) {
                continue;
            }
            if (lastHigh == null) {
                lastHigh = dateValue.getValue();
                lastHighDate = dateValue.getDate();
                continue;
            }
            if (dateValue.getValue().compareTo(lastHigh) >= 0) {//前半部分小于最高的数值
                int dateDistance = DateUtil.getDateDistance(lastHighDate, dateValue.getDate(), Frequency.Daily, false);
                if (dateDistance > longDateDistance) {
                    longDateDistance = dateDistance;
                }
                lastHigh = dateValue.getValue();
                lastHighDate = dateValue.getDate();
            }else{//后半部分小于最高的数值
                int dateDistance = DateUtil.getDateDistance(lastHighDate, dateValue.getDate(), Frequency.Daily, false);
                if (dateDistance > longDateDistance) {
                    longDateDistance = dateDistance;
                }
            }
        }

        if (lastHighDate == null) {
            return null;
        }

        return Double.valueOf(longDateDistance);
    }
}
