package net.northking.cctp.element.xml.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 TypeConversionGetEnum implements EnumClass {
    GetFirstElemnt("获取xpath匹配的第一个元素", "0"),
    GetAttrOfFE("获取xpath匹配的第一个元素的属性", "1"),
    GetTextOfFE("获取xpath匹配的第一个元素的文本", "2"),
    GetAllElemnt("获取xpath匹配的所有元素", "3"),
    GetTextOfAE("获取xpath匹配的所有元素的文本", "4"),
    GetChildElemnt("获取xpath匹配的子元素", "5"),
    ;
    private String key;
    private String value;

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

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

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