package com.smppw.common.pojo;

import com.smppw.common.pojo.enums.strategy.Strategy;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author Rain
 *
 */
public enum InvestmentType {

	Fund(1, "^[HM]F.*"), Company(2, "^CO.*"), Manager(3, "^PL.*"), ExternalFund(4, "^CF.*"), Portfolio(5,
			"^PO.*"), Index(6, "^IN.*"), UDIndex(8, "^CI.*"),BFI(9,"^FA.*"),ADVISORYPOOL(10,"^AP.*");

	private InvestmentType(int id, String regex) {
		this.regex = regex;
		this.id = id;
	}

	private final int id;
	private final String regex;

	public int getId() {
		return id;
	}

	public String getRegex() {
		return regex;
	}

	public static InvestmentType getInvestmentType(int id) {
		return Stream.of(values()).filter(e -> e.id == id).findFirst().orElse(null);
	}

	public static InvestmentType getInvestmentType(String secId) {
		if(null == secId)
			return null;
		
		String secIdUpper = secId.toUpperCase();
		
		for (InvestmentType type : values())
			if (Pattern.matches(type.getRegex(), secIdUpper))
				return type;
		
		return null;
	}

	public static boolean check(String id) {
		if (StringUtils.isEmpty(id))
			return false;
		return getInvestmentType(id) != null;
	}

	/**
	 * 将输入的ID值按类型分为不同的List
	 *
	 * @param secIds ID值
	 * @return 返回值key为类型，value为对应类型的ID值
	 */
	public static Map<InvestmentType, List<String>> classifyIds(List<String> secIds) {
		Map<InvestmentType, List<String>> map = new HashMap<>();
		for (String id : secIds) {
			if (id == null || Strategy.isStrategy(id))
				continue;
			InvestmentType type = getInvestmentType(id);
			if (!map.containsKey(type))
				map.put(type, new ArrayList<>());
			map.get(type).add(id);
		}
		return map;
	}
	
}
