package com.njzscloud.common.core.ienum;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.njzscloud.common.core.fastjson.serializer.DictObjectDeserializer;
import com.njzscloud.common.core.fastjson.serializer.DictObjectSerializer;
import com.njzscloud.common.core.jackson.serializer.DictDeserializer;
import com.njzscloud.common.core.jackson.serializer.DictSerializer;

/**
 * 字典枚举<br/>
 * 仅两个子接口 DictInt、DictStr<br/>
 *
 * @see DictInt
 * @see DictStr
 * @see DictDeserializer
 * @see DictSerializer
 * @see DictObjectDeserializer
 * @see DictObjectSerializer
 */
@JsonDeserialize(using = DictDeserializer.class)
@JsonSerialize(using = DictSerializer.class)
public interface Dict<T> extends IEnum {

    /**
     * 枚举单独序列化时的属性<br/>
     * 存放枚举的 val 属性值
     */
    String ENUM_VAL = "val";

    /**
     * 枚举单独序列化时的属性<br/>
     * 存放枚举的 txt 属性值
     */
    String ENUM_TXT = "txt";

    /**
     * 根据 "值" 获取到对应的枚举对象
     *
     * @param val 值
     * @param ds  枚举对象数组
     * @param <V> 值类型
     * @param <D> 枚举类型
     * @return Dict
     */
    static <V, D extends Dict<V>> D parse(V val, D[] ds) {
        for (D d : ds) {
            if (d.getVal().equals(val)) {
                return d;
            }
        }
        return null;
    }

    /**
     * 值
     *
     * @return T
     */
    T getVal();

    /**
     * 文本表示
     *
     * @return String
     */
    String getTxt();


}
