package com.smppw.core.reta.calc;


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 java.util.List;

public class SortinoRatio extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		Double SortinoRatio = 0.0;
		int count = 0;
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfRf() == null) {
				continue;
			}
			count++;
			SortinoRatio += value.getRetOfFund() - value.getRetOfRf();
		}
		if (count == 0) {
			return null;
		}
		SortinoRatio /= count;

		Double downsideStdDev = new DownsideStdDev().calc(list, frequency, false);
//		if (downsideStdDev == null) {
//			return null;
//		} else if (downsideStdDev == 0) {
//			return DataUtil.handleZeroValue(SortinoRatio);
//		}
//
//		SortinoRatio /= downsideStdDev;
		SortinoRatio = DataUtil.divNearZeroValue(SortinoRatio, downsideStdDev);
		if (SortinoRatio == null) {
			return null;
		}
		if (ifAnnualized) {
			SortinoRatio = Rate.AnnualizedBySqrt(SortinoRatio, frequency);
		}

		return DataUtil.handleOverflowData(SortinoRatio);
	}
}
