package com.bonus.core.marketing.util;

import cn.hutool.core.map.MapUtil;

import java.util.*;

public final class EnumUtil {
   public static <T extends ICommonEnum> T getEnumByKeyword(Class<T> tClass, String keyword) {
      ICommonEnum[] var2 = (ICommonEnum[])tClass.getEnumConstants();
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         T t = (T) var2[var4];
         if (String.valueOf(t.getKey()).equals(keyword) || t.getValue().equals(keyword)) {
            return t;
         }
      }

      return null;
   }

   public static <T extends ICommonEnum> T getEnumByKey(Class<T> tClass, Integer key) {
      ICommonEnum[] var2 = (ICommonEnum[])tClass.getEnumConstants();
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         T t = (T) var2[var4];
         if (t.getKey().equals(key)) {
            return t;
         }
      }

      return null;
   }

   public static <T extends ICommonEnum> T getEnumByValue(Class<T> tClass, String value) {
      ICommonEnum[] var2 = (ICommonEnum[])tClass.getEnumConstants();
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         T t = (T) var2[var4];
         if (t.getValue().equals(value)) {
            return t;
         }
      }

      return null;
   }

   public static <T extends ICommonEnum> String getValueByKeyword(Class<T> tClass, String keyword) {
      T t = getEnumByKeyword(tClass, keyword);
      return Objects.nonNull(t) ? t.getValue() : null;
   }

   public static <T extends ICommonEnum> String getValueByKey(Class<T> tClass, Integer key) {
      T t = getEnumByKey(tClass, key);
      return Objects.nonNull(t) ? t.getValue() : null;
   }

   public static <T extends ICommonEnum> Integer getKeyByValue(Class<T> tClass, String value) {
      T t = getEnumByValue(tClass, value);
      return Objects.nonNull(t) ? t.getKey() : null;
   }

   public static <T extends ICommonEnum> List<Map> getAllEnumsList(Class<T> tClass) {
      List<Map> list = new ArrayList();
      Arrays.stream((ICommonEnum[])tClass.getEnumConstants()).forEach((e) -> {
         Map map = MapUtil.builder().put("key", e.getKey()).put("value", e.getValue()).build();
         if (Objects.nonNull(e.getDesc())) {
            map.put("desc", e.getDesc());
         }

         if (Objects.nonNull(e.getEnableRule())) {
            map.put("enableRule", e.getEnableRule());
         }

         list.add(map);
      });
      return list;
   }

   private EnumUtil() {
      throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
   }
}
