package com.smppw.core.reta.calc;


import cn.hutool.core.collection.ListUtil;
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 TreynorRatio extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		List<String> dateList = ListUtil.list(true);
		List<Double> rates = new ArrayList<>();
		List<Double> rfs = new ArrayList<>();
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null && value.getRetOfRf() != null)
				continue;
			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);
		Double beta = new Beta().calc(list, frequency, ifAnnualized);

		if (beta == null || rfsAnnual == null || ratesAnnual == null) {
			return null;
		}
//		} else if (beta == 0) {
//			return DataUtil.handleZeroValue(ratesAnnual - rfsAnnual);
//		}
//
//		Double result = (ratesAnnual - rfsAnnual) / beta;
		return DataUtil.divNearZeroValue(ratesAnnual - rfsAnnual, beta);
	}

}
