package com.engine.salary.util;

import com.engine.salary.enums.BaseEnum;
import com.engine.salary.enums.sicategory.PaymentScopeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 枚举工具类
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class SalaryEnumUtil {

    /**
     * 根据枚举的value获取枚举对象
     *
     * @param value 枚举中的value
     * @param list
     * @param T
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T enumMatchByValue(Integer value, BaseEnum<Integer>[] list, Class<? extends BaseEnum<Integer>> T) {
        return (T) Arrays.stream(list).filter(item -> Objects.equals(item.getValue(), value)).findFirst().get();
    }

    /**
     * 根据枚举的value获取枚举对象
     *
     * @param value
     * @param clazz
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T extends BaseEnum<R>, R> T enumMatchByValue(R value, Class<T> clazz) {
        T[] enumConstants = clazz.getEnumConstants();
        for (T enumConstant : enumConstants) {
            if (Objects.equals(enumConstant.getValue(), value)) {
                return enumConstant;
            }
        }
        return null;
    }

    /**
     * 枚举数组转字符串
     *
     * @param list
     * @return
     */
    public static String enumArrToString(BaseEnum<Integer>[] list) {
        List<String> collect = Arrays.stream(list).map(item -> String.valueOf(item.getValue())).collect(Collectors.toList());
        return StringUtils.join(collect, ",");
    }

    public static PaymentScopeEnum[] stringToEnums(String values, String charSequence) {
        String[] arr = values.split(charSequence);
        PaymentScopeEnum[] enumConstants = PaymentScopeEnum.values();
        List<PaymentScopeEnum> collect = Arrays.stream(arr)
                .map(item -> Arrays.stream(enumConstants).filter(s -> Objects.equals(String.valueOf(s.getValue()), item)).findFirst().get()).collect(Collectors.toList());
        return collect.toArray(new PaymentScopeEnum[collect.size()]);
    }


    /**
     * 获取枚举下拉列表
     * @param enumName 枚举路径，列如：com.engine.salary.enums.salaryarchive.SalaryArchiveStatusEnum
     * @return
     */
    public static List<Map<String, Object>> enumSelectList(String enumName) {

        try {
            List<Map<String, Object>> keyValueList = new ArrayList<Map<String, Object>>();

            Class cls = Class.forName(enumName);

            Method method = cls.getMethod("values");

            BaseEnum enums[] = (BaseEnum[]) method.invoke(null, null);

            for (BaseEnum enumMessage : enums) {
                Map<String, Object> hashMap = new HashMap<String, Object>();
                hashMap.put("enum", enumMessage);
                hashMap.put("value", enumMessage.getValue());
                hashMap.put("defaultLabel", enumMessage.getDefaultLabel());
                hashMap.put("labelId", enumMessage.getLabelId());
                keyValueList.add(hashMap);

            }
            return keyValueList;

        } catch (Exception e) {
            log.error("获取枚举下拉列表异常", e);
            throw new SalaryRunTimeException("指定类型未找到");
        }


    }
}
