package com.smppw.core.reta.calc;


import cn.hutool.core.collection.ListUtil;
import com.smppw.constants.IndicatorConst;
import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.core.reta.Rate;
import com.smppw.utils.DataUtil;
import com.smppw.utils.MathUtils;

import java.util.ArrayList;
import java.util.List;

public class Omega extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		double LPM1 = 0.0;
		List<Double> rates = new ArrayList<>();
		List<Double> rfs = new ArrayList<>();
		List<String> dateList = ListUtil.list(true);
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfRf() == null) {
				continue;
			}
			double max = value.getRetOfRf() - value.getRetOfFund();
			if (max < 0) {
				max = 0;
			}
			LPM1 += max;
			rates.add(value.getRetOfFund());
			rfs.add(value.getRetOfRf());
			dateList.add(value.getEndDate());
		}
		Double ratesAnnual = MathUtils.getAnnualReturn(rates, dateList, true);
		Double rfsAnnual = MathUtils.getAnnualReturn(rfs, dateList, true);
		if (list.size() <= 1 || ratesAnnual == null || rfsAnnual == null) {
			return null;
		}
		LPM1 /= list.size() - 1;
		LPM1 *= frequency.getNum();
		if (LPM1 <= IndicatorConst.MIN_DENOMINATOR_VALUE) {
			return null;
		}

		Double result = (ratesAnnual - rfsAnnual) / LPM1 + 1;
		return DataUtil.handleOverflowData(result);
	}

}
