package com.engine.salary.util;

import com.engine.salary.annotation.I18n;
import com.engine.salary.exception.SalaryRunTimeException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import weaver.general.Util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 多语言工具类
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryI18nUtil {


    public static <T> T i18n(T data) {
        if (data == null ) {
            return data;
        }
        try {
            List<Field> fieldList = getFields(data.getClass());
            if (CollectionUtils.isNotEmpty(fieldList)) {
                for (Field field : fieldList) {
                    field.setAccessible(true);
                    String fieldValue = (String) field.get(data);
                    if (StringUtils.isNotBlank(fieldValue)) {
                        String encryptValue = Util.formatMultiLang(fieldValue);
                        field.set(data, encryptValue);
                    }
                }
            }
            return data;
        } catch (Exception e) {
            throw new SalaryRunTimeException("国际化解析异常");
        }
    }

    public static <T> List<T> i18nList(List<T> dataList) {
        if (CollectionUtils.isEmpty(dataList) ) {
            return dataList;
        }
        try {
            List<Field> fieldList = getFields(dataList.get(0).getClass());
            if (CollectionUtils.isNotEmpty(fieldList)) {
                for (T data : dataList) {
                    for (Field field : fieldList) {
                        field.setAccessible(true);
                        String fieldValue = (String) field.get(data);
                        if (StringUtils.isNotBlank(fieldValue)) {
                            String encryptValue = Util.formatMultiLang(fieldValue);
                            field.set(data, encryptValue);
                        }
                    }
                }

            }
            return dataList;
        } catch (Exception e) {
            throw new SalaryRunTimeException("国际化批量解析异常");
        }
    }

    @NotNull
    private static <T> List<Field> getFields(Class<T> clazz) {
        List<Class<?>> allClasses = new ArrayList<Class<?>>();
        for (Class<?> superClass = clazz; superClass != null; superClass = superClass.getSuperclass()) {
            if (superClass != Object.class) {
                allClasses.add(superClass);
            }
        }
        return allClasses.stream()
                .map(Class::getDeclaredFields)
                .flatMap(Arrays::stream)
                .filter(field -> field.isAnnotationPresent(I18n.class)).collect(Collectors.toList());
    }

    /**
     * 获取多语言信息
     *
     * @param labelId      多语言对应的labelId
     * @param defaultLabel 默认中文
     * @return
     */
    public static String getI18nLabel(int labelId, String defaultLabel) {
        return defaultLabel;
    }
//
//    /**
//     * 获取多语言信息
//     *
//     * @param tenantKey    租户key
//     * @param employeeId   人员id
//     * @param labelId      多语言对应的labelId
//     * @param defaultLabel 默认中文
//     * @return
//     */
//    public static String getI18nLabel(String tenantKey, Long employeeId, int labelId, String defaultLabel) {
//        int languageId = I18nLanguageUtil.getLangId(employeeId);
//        return SalaryI18nUtil.getI18nLabel(labelId, languageId, tenantKey, defaultLabel);
//    }
//
//    /**
//     * 获取多语言信息
//     *
//     * @param simpleEmployee 租户信息
//     * @param labelId        多语言对应的labelId
//     * @param defaultLabel   默认中文
//     * @return
//     */
//    public static String getI18nLabel(DataCollectionEmployee simpleEmployee, int labelId, String defaultLabel) {
//        int languageId = I18nLanguageUtil.getLangId(simpleEmployee.getEmployeeId());
//        return SalaryI18nUtil.getI18nLabel(labelId, languageId, simpleEmployee.getTenantKey(), defaultLabel);
//    }
}
