package com.smppw.common.pojo.enums;

import cn.hutool.core.map.MapUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author Yu Liu
 *
 */
public enum TimeRange {

	Custom(-1, "custom", "Custom", "Custom"), /**/
	FromThisMonth(-2, "1m", "ma-0", "ma-0"), /**/
	FromThisQuarter(-3, "1q", "qa-0", "qa-0"), /**/
	FromThisYear(13, "ytd", "ya-0", "ytd"), /**/
	Last1Week(0.25, "1w", "wa-1", "wb-1"), /**/
	Last1Month(1, "1m", "ma-1", "mb-1"), /**/
	Last2Months(2, "1m", "ma-2", "mb-2"), /**/
	Last3Months(3, "3m", "ma-3", "mb-3"), /**/
	LastHalfYear(6, "6m", "ma-6", "mb-6"), /**/
	Last1Year(12, "1y", "ya-1", "yb-1"), /**/
	Last2Years(24, "2y", "ya-2", "yb-2"), /**/
	Last3Years(36, "3y", "ya-3", "yb-3"), /**/
	Last4Years(48, "4y", "ya-4", "yb-4"), /**/
	Last5Years(60, "5y", "ya-5", "yb-5"), /**/
	Last10Years(120, "10y", "ya-10", "yb-10"), /**/
	FromSetup(0, "incep", "FromSetup", "FromSetup"),
	Operation(-10, "operation", "Operation", ""),
	ThisWeek(1002, "tw", "tw-1", ""),
	ThisMonth(1003, "tm", "tm-1", ""),
	ThisQuarter(1004, "tq", "tq-1", ""),
	Last1RollYear(1005, "1ry", "ya-1-r", "yb-1-r"), /**/
	Last2RollYears(1006, "2ry", "ya-2-r", "yb-2-r"), /**/
	Last3RollYears(1007, "3ry", "ya-3-r", "yb-3-r"), /**/
	Last4RollYears(1008, "4ry", "ya-4-r", "yb-4-r"), /**/
	Last5RollYears(1009, "5ry", "ya-5-r", "yb-5-r") /**/;

	private double id;
	private String symbel;
	private String floatDateSymbel;
	private String amacSymbel;

	public static final List<TimeRange> TRAILING_DATES = Arrays.asList(FromThisYear, Last1Week, Last1Month, Last3Months,
			LastHalfYear, Last1Year, Last2Years, Last3Years, Last5Years, FromSetup);

	public static final List<TimeRange> TRAILING_DATES_WITH_TEN_YEARS = Arrays.asList(FromThisYear, Last1Week,
			Last1Month, Last3Months, LastHalfYear, Last1Year, Last2Years, Last3Years, Last5Years, Last10Years,
			FromSetup);

	public static final List<TimeRange> STRATEHGY_AVG_DATES = Arrays.asList(FromThisYear, Last1Month, Last3Months,
			LastHalfYear, Last1Year, Last2Years, Last3Years, Last5Years, Last10Years);

	public static final List<TimeRange> ROLLING_TIMERANGE = Arrays.asList(ThisWeek, ThisMonth, ThisQuarter, Last1RollYear, Last2RollYears, Last3RollYears, Last4RollYears, Last5RollYears);

	public static final List<TimeRange> YEAR_ROLLING_TIMERANGE = Arrays.asList(Last1RollYear, Last2RollYears, Last3RollYears, Last4RollYears, Last5RollYears);

	/**
	 * 私募观察-主策略指数使用时段
	 */
	public static final List<TimeRange> STRATEGY_INDEX_DATES = Arrays.asList(Last1Week, Last1Month, Last3Months,
			LastHalfYear, FromThisYear, Last1Year, Last2Years, Last3Years, Last5Years, FromSetup, Last10Years);

	public static final List<TimeRange> TIMERANGE_LIST = Arrays.asList(LastHalfYear, FromThisYear, Last1Year, Last2Years, Last3Years, Last5Years, FromSetup);

	/**
	 * 排排网风格总览
	 */
	public static final Map<TimeRange, String> RZ_STYLE_TIMERANGE = MapUtil.<TimeRange, String>builder(MapUtil.newHashMap(true))
			.put(Last1Year, "近一年")
			.put(Last2Years, "近两年")
			.put(Last3Years, "近三年")
			.put(FromSetup, "成立以来")
			.build();

	public static final Map<TimeRange, String> COR_DATES = MapUtil.<TimeRange, String>builder(MapUtil.newHashMap(true))
			.put(FromThisYear, "今年以来")
			.put(Last3Months, "近三月")
			.put(LastHalfYear, "近六月")
			.put(Last1Year, "近一年")
			.put(Last2Years, "近二年")
			.put(Last3Years, "近三年")
			.put(Last5Years, "近五年")
			.put(Last10Years, "近十年")
			.put(FromSetup, "成立以来")
			.build();

	public static final Map<TimeRange, String> INDEX_INDICATOR_DATES = MapUtil.<TimeRange, String>builder(MapUtil.newHashMap(true))
			.put(Last1Week,"近一周")
			.put(Last1Month, "近一月")
			.put(Last3Months, "近三月")
			.put(LastHalfYear, "近六月")
			.put(Last1Year, "近一年")
			.put(Last2Years, "近二年")
			.put(Last3Years, "近三年")
			.put(Last5Years, "近五年")
			.put(Last10Years, "近十年")
			.put(FromThisYear, "今年以来")
			.put(FromSetup, "成立以来")
			.put(Custom, "统计区间")
			.build();

	public static final Map<TimeRange, String> RANGE_INDICATOR_DATES = MapUtil.<TimeRange, String>builder(MapUtil.newHashMap(true))
			.put(FromThisYear, "今年以来")
			.put(Last1Week, "近一周")
			.put(Last1Month, "近一月")
			.put(Last3Months, "近三月")
			.put(LastHalfYear, "近半年")
			.put(Last1Year, "近一年")
			.put(Last2Years, "近二年")
			.put(Last3Years, "近三年")
			.put(Last5Years, "近五年")
			.put(Custom,"对比区间")
			.build();

	private TimeRange(double id, String symbel, String floatDateSymbel, String amacSymbel) {
		this.id = id;
		this.symbel = symbel;
		this.floatDateSymbel = floatDateSymbel;
		this.amacSymbel = amacSymbel;
	}

	public double getId() {
		return id;
	}

	public String getFloatDateSymbel() {
		return floatDateSymbel;
	}

	public String getSymbel() {
		return symbel;
	}

	public String getAmacSymbel() {
		return amacSymbel;
	}

	public static TimeRange getTimeRange(double id) {
		return Stream.of(values()).filter(e -> e.id == id).findFirst().orElse(null);
	}

	public static TimeRange getTimeRange(String floatDateSymbel) {
		return Stream.of(values()).filter(e -> e.floatDateSymbel.equals(floatDateSymbel)).findFirst().orElse(null);
	}

	public static TimeRange getTimeRangeBySmbel(String symbel) {
		return Stream.of(values()).filter(e -> e.symbel.equals(symbel)).findFirst().orElse(null);
	}

	public static TimeRange getTimeRangeByFsOrAsOrS(String date) {
		TimeRange tr = null;
		TimeRange[] values = TimeRange.values();
		for (TimeRange timeRange : values) {
			if (timeRange.getFloatDateSymbel().equals(date) || timeRange.getAmacSymbel().equals(date) || timeRange.getSymbel().equals(date)) {
				tr = timeRange;
				break;
			}
		}
		return tr;
	}

	public static TimeRange getTimeRangeByName(String name) {
		TimeRange tr = null;
		TimeRange[] values = TimeRange.values();
		for (TimeRange timeRange : values) {
			if (timeRange.name().equals(name)){
				tr = timeRange;
				break;
			}
		}
		return tr;
	}

	public static List<String> timeRanges2StringList(List<TimeRange> trs) {
		List<String> tsStrList = new ArrayList<>();
		for (TimeRange tr : trs) {
			tsStrList.add(tr.toString());
		}
		return tsStrList;
	}

	public static List<String> getSymbels(List<TimeRange> timeRanges) {
		List<String> res = new ArrayList<>(timeRanges.size());
		for (TimeRange timeRange : timeRanges) {
			res.add(timeRange.getSymbel());
		}
		return res;
	}
}
