package com.smppw.core.reta.calc;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超额收益最大回撤，基于净值计算，
 */
public class ExcessReturnMaxDrawdownNav extends Rate {
    @Override
    public Double calc(List<DateValue> dateValues) {
        return new MaxDrawdown().calc(dateValues);
    }

    @Override
    public Double calc(List<DateValue> navList, List<DateValue> benchMark, Frequency frequency, boolean ifAnnualized) {
        Map<String, Double> benchMarkMap = new HashMap<>();
        for (DateValue dv : benchMark) {
            benchMarkMap.put(dv.getDate(), dv.getValue());
        }
        List<DateValue> calcList = new ArrayList<>();
        for (DateValue dv : navList) {
            Double benchMarkValue = benchMarkMap.get(dv.getDate());
            if (benchMarkValue == null || benchMarkValue.equals(0D)) {
                continue;
            }
            calcList.add(new DateValue(dv.getDate(), dv.getValue() / benchMarkValue));
        }
        Double maxDrawdown = CalcUtils.maxDrawdown(calcList);
        return maxDrawdown == null ? null : (maxDrawdown.equals(0D) ? 0D : -maxDrawdown);
    }
}
