package com.smppw.core.reta.calc;

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

import java.util.List;

/**
 * 超额卡玛
 */
public class ExcessReturnCalmarRatio extends Rate {
    @Override
    public Double calc(List<DateValue> dateValues) {
        return new CalmarRatio().calc(dateValues);
    }

    @Override
    public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {

        if (list == null || list.isEmpty()) {
            return null;
        }

        // 超额年化收益
        Double ratesAnnual = null;
        Double retOfFund = null;
        Double retOfBmk = null;
        for (CompoundRet cr : list) {
            if (retOfFund == null) {
                retOfFund = 0.0D;
            }
            if (retOfBmk == null) {
                retOfBmk = 0.0D;
            }
            retOfFund += cr.getRetOfFund()==null?0.0D:cr.getRetOfFund();
            retOfBmk += cr.getRetOfBmk()==null?0.0D:cr.getRetOfBmk();
        }
        String firstDate = list.get(0).getEndDate();
        String lastDate = list.get(list.size() - 1).getEndDate();

        Double value = ((1 + retOfFund) / (1 + retOfBmk)) - 1;
        int dateDis = DateUtil.getDateDistance(firstDate, lastDate, Frequency.Daily, true);

        if (dateDis == 8) {
            ratesAnnual = Math.pow(1 + value, Frequency.Weekly.getNum()) - 1;
        } else {
            ratesAnnual = Math.pow(1 + value, 365.25 / dateDis) - 1;
        }

        // 超额回撤
        ExcessReturnMaxDrawdown excessReturnMaxDrawdown = new ExcessReturnMaxDrawdown();
        Double maxDrawdown = excessReturnMaxDrawdown.calc(list, frequency, ifAnnualized);

//        if (ratesAnnual != null && maxDrawdown != null) {
//            Double result = ratesAnnual / maxDrawdown;
//            return DataUtil.handleOverflowData(result);
//        } else {
//            return null;
//        }
        return DataUtil.divNearZeroValue(ratesAnnual, maxDrawdown);
    }
}
