package com.ningdatech.kqapi.common.enumeration;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.EnumUtils;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * IEnum-通用枚举类接口
 * </p>
 *
 * @author WendyYang
 * @since 10:10 2024/4/22
 */
public interface IEnum {

    Integer getCode();

    String getDesc();

    default boolean eq(Integer code) {
        return getCode().equals(code);
    }

    static <E extends Enum<E>> List<E> all(Class<E> eClass) {
        return EnumUtils.getEnumList(eClass);
    }

    static <E extends Enum<E> & IEnum> Optional<E> getByCode(Class<E> eClass, Integer code) {
        return all(eClass).stream()
                .filter(w -> w.getCode().equals(code))
                .findFirst();
    }

    static <E extends Enum<E> & IEnum> E getNoNull(Class<E> eClass, Integer code) {
        return all(eClass).stream()
                .filter(w -> w.getCode().equals(code)).findFirst()
                .orElseThrow(() -> new IllegalArgumentException("编码对应枚举值不存在"));
    }

    static <E extends Enum<E> & IEnum> String getDesc(Class<E> eclass, Integer code) {
        return getByCode(eclass, code).flatMap(w -> Optional.of(w.getDesc())).orElse(StrUtil.EMPTY);
    }

    static <E extends Enum<E> & IEnum> Optional<E> getByDesc(Class<E> eclass, String code) {
        return all(eclass).stream().filter(w -> w.getDesc().equals(code)).findFirst();
    }

}
