package com.smppw.core.reta.calc;

import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.common.pojo.enums.Indicator;
import com.smppw.core.reta.IndicatorFactory;
import com.smppw.core.reta.Rate;
import com.smppw.utils.DateUtil;
import org.joda.time.DateTime;

import java.util.List;

/**
 * @author zhengsongliang
 * @datetime 2017年7月27日 上午10:04:32
 */
public class ExcessAnnualAriIntervalReturn extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		Rate rate = IndicatorFactory.getInstance().get(Indicator.ExcessAriIntervalReturn);
		Double intervalReturn = rate.calc(list, frequency, ifAnnualized);
		if (list == null) {
			return null;
		}

		int n = list.size();
		if (intervalReturn == null || n < 1)
			return null;

		String firstDate = list.get(0).getEndDate();
		String lastDate = list.get(n - 1).getEndDate();

		Double annualReturn = intervalReturn;
		int dateDis = DateUtil.getDateDistance(firstDate, lastDate, Frequency.Daily, false);

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

		return annualReturn;
	}
	
	// GIPS少于一年不年化
	private boolean lessThanOneYear(String startDate, String endDate) {
		DateTime startDT = DateTime.parse(startDate).withMillisOfDay(0);
		DateTime endDT = DateTime.parse(endDate).withMillisOfDay(0);

		DateTime oneYearAgo = endDT.minusYears(1).plusDays(1);
		return oneYearAgo.isBefore(startDT);
	}
}
