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;

/**
 * 计算超额收益，仅适用于动态列计算，
 * 原有的ExcessReturn方法可能存在错误，但涉及面较广，需要确认后在进行修改；
 * 计算出的结果有问题，因此改用本方法
 */
public class ExcessReturnDynamic extends Rate {

    @Override
    public Double calc(List<DateValue> navList, List<DateValue> benchMark, Frequency frequency, boolean ifAnnualized) {
        if (navList == null || benchMark == null || navList.isEmpty() || benchMark.isEmpty()) {
            return null;
        }
        IntervalReturn intervalReturn = new IntervalReturn();
        Double fundRet = intervalReturn.calc(navList);
        Double indexRet = intervalReturn.calc(benchMark);
        Double result = fundRet == null || indexRet == null ? null : (fundRet - indexRet);
        int trands = DateUtil.getDateDistance(navList.get(0).getDate(), navList.get(navList.size() - 1).getDate(), frequency, true);
        if (result != null && ifAnnualized) {
            if (trands == 0) {
                return null;
            }
            result = Math.pow(1 + result, frequency.getNum() / trands) - 1;
        }
        return result;

    }


}
