package com.smppw.core.trend.calc;


import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.dto.DateValue;
import com.smppw.core.trend.Trend;
import com.smppw.utils.BigDecimalUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtraRetGeoTrend extends Trend {
    @Override
    public List<CompoundRet> getCompoundRetTrend(List<DateValue> secNavList, List<CompoundRet> retList, List<CompoundRet> benchmarkRetList) {
        List<CompoundRet> extraRetAriList = new ArrayList<>();

        Map<String, CompoundRet> benchmarkRetMap = new HashMap<>();
        for (CompoundRet compoundRet : benchmarkRetList) {
            benchmarkRetMap.put(compoundRet.getEndDate(), compoundRet);
        }
        for (CompoundRet compoundRet : retList) {
            CompoundRet extraRetAri = new CompoundRet();
            Double secRet = compoundRet.getRetOfFund();
            CompoundRet benchmarkRetDto = benchmarkRetMap.get(compoundRet.getEndDate());
            if (null == benchmarkRetDto || null ==secRet || benchmarkRetDto.getRetOfFund() == null ) {
                extraRetAri.setEndDate(compoundRet.getEndDate());
                extraRetAri.setRetOfFund(null);
            } else if (benchmarkRetDto.getRetOfFund().compareTo(0.0) == 0) {
                extraRetAri.setEndDate(compoundRet.getEndDate());
                extraRetAri.setRetOfFund(0.0);
            } else {
                Double molecule = secRet + 1;
                Double denominator = benchmarkRetDto.getRetOfFund() + 1;
                Double benchmarkRet = BigDecimalUtils.subtract(BigDecimalUtils.divide(new BigDecimal(molecule), new BigDecimal(denominator)), new BigDecimal("1")).doubleValue();
                extraRetAri.setEndDate(compoundRet.getEndDate());
                extraRetAri.setRetOfFund(benchmarkRet);
            }
            extraRetAriList.add(extraRetAri);
        }

        return extraRetAriList;
    }
}
