package net.northking.cctp.element.basic.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 TypeConversionEnum implements EnumClass {

    object2Str("对象转换成字符串", "1"),
    num2Binary("数字转换为2进制", "2"),
    num2Octal("数字转换为8进制", "3"),
    num2Hex("数字转换为16进制", "4"),
    binary2Num("2进制转换为数字", "5"),
    octal2Num("8进制转换为数字", "6"),
    hex2Num("16进制转换为数字", "7"),
    str2Hex("字符串转换为16进制", "8"),
    ;

    public String key;
    public String value;

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

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

    @Override
    public String getEnumData() {
        List<Map<String, Object>> result = new ArrayList<>();
        for (TypeConversionEnum item : EnumSet.allOf(TypeConversionEnum.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);
    }
}
