package com.smppw.core.trend.calc;



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

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

public class DrawdownTrend extends Trend {

    @Override
    public List<DateValue> getDateValueTrend(List<String> dateList, List<DateValue> secNavList, List<DateValue> benchmarkNavList) {
        Map<String, DateValue> dateSecNavMap = new HashMap<>();
        for (DateValue secNav : secNavList) {
            dateSecNavMap.put(secNav.getDate(), secNav);
        }

        Double heap = null;
        List<DateValue> drawDownValueList = new ArrayList<>();
        for (String date : dateList) {
            Double value = dateSecNavMap.get(date) == null ? null : dateSecNavMap.get(date).getValue();
            Double drawdown = null;
            if (value != null) {
                if (heap == null || value > heap) {
                    heap = value;
                    drawdown = 0.0;
                } else {
                    drawdown = value / heap - 1;
                }
            }
            DateValue drawDownValue = new DateValue();
            drawDownValue.setDate(date);
            drawDownValue.setValue(drawdown);
            drawDownValueList.add(drawDownValue);
        }
        return drawDownValueList;
    }
}
