package net.northking.cctp.element.string.enums;

import com.alibaba.fastjson.JSON;
import net.northking.cctp.element.core.type.EnumClass;

import java.util.*;

/**
 * TypeConversionEnum
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public enum TypeConversionSubStringEnum implements EnumClass {
    substringBySE("通过起始结束坐标截取", "0"),
    substringAfter("截取字符串第一次出现后的", "1"),
    substringBefore("截取字符串第一次出现前的", "2"),
    substringBetween("截取开始和结束字符串中间的", "3"),
    substringAfterLast("截取字符串最后一次出现后的", "4"),
    substringBeforeLast("截取字符串最后一次出现前的", "5"),
    ;

    private String key;
    private String value;

    TypeConversionSubStringEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public static TypeConversionSubStringEnum from(String value) {
        for (TypeConversionSubStringEnum item : TypeConversionSubStringEnum.values()) {
            if (item.value.equals(value)) {
                return item;
            }
        }
        throw new IllegalArgumentException("未知的转换类型:" + value);
    }

    @Override
    public String getEnumData() {
        List<Map<String, Object>> result = new ArrayList<>();
        for (TypeConversionSubStringEnum item : EnumSet.allOf(TypeConversionSubStringEnum.class)) {
            Map<String, Object> one = new HashMap(1);
            one.put("label", item.key);
            one.put("value", item.value);
            result.add(one);
        }
        return JSON.toJSONString(result);
    }
}
