package com.cf.imes.framework.dict.core.util;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.util.cache.CacheUtils;
import com.cf.imes.module.system.api.dict.DictDataApi;
import com.cf.imes.module.system.api.dict.dto.DictDataRespDTO;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.context.i18n.LocaleContextHolder;

import java.time.Duration;
import java.util.Locale;

/**
 * 字典工具类
 *
 * @author 晨丰科技
 */
@Slf4j
public class DictFrameworkUtils {

    private static DictDataApi dictDataApi;

    private static final DictDataRespDTO DICT_DATA_NULL = new DictDataRespDTO();

    /**
     * 针对 {@link #getDictDataLabel(String, String)} 的缓存
     */
    private static final LoadingCache<Pair<String, String>, DictDataRespDTO> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(10L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public DictDataRespDTO load(Pair<String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()).getCheckedData(),
                            DICT_DATA_NULL);
                }

            });

    /**
     * 针对 {@link #parseDictDataValue(String, String)} 的缓存
     */
    private static final LoadingCache<Pair<String, String>, DictDataRespDTO> PARSE_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(10L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public DictDataRespDTO load(Pair<String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.parseDictData(key.getKey(), key.getValue()).getCheckedData(),
                            DICT_DATA_NULL);
                }

            });

    /**
     * 针对 {@link #getDictI18nDataLabel(String, String)} 的缓存
     */
    private static final LoadingCache<Triple<String, String, String>, DictDataRespDTO> GET_DICT_DATA_CACHE_I18n = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(10L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public DictDataRespDTO load(Triple<String, String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.getDictI18nData(key.getLeft(), key.getMiddle(), key.getRight()).getCheckedData(),
                            DICT_DATA_NULL);
                }

            });

    public static void init(DictDataApi dictDataApi) {
        DictFrameworkUtils.dictDataApi = dictDataApi;
        log.info("[init][初始化 DictFrameworkUtils 成功]");
    }

    @SneakyThrows
    public static String getDictDataLabel(String dictType, Integer value) {
        return GET_DICT_DATA_CACHE.get(new Pair<>(dictType, String.valueOf(value))).getLabel();
    }

    @SneakyThrows
    public static String getDictDataLabel(String dictType, String value) {
        return GET_DICT_DATA_CACHE.get(new Pair<>(dictType, value)).getLabel();
    }

    @SneakyThrows
    public static String getDictI18nDataLabel(String dictType, String value) {
        Locale locale = LocaleContextHolder.getLocale();
        if(locale == null || Locale.CHINESE.equals(locale)) {
            return GET_DICT_DATA_CACHE.get(new Pair<>(dictType, value)).getLabel();
        } else {
            return GET_DICT_DATA_CACHE_I18n.get(Triple.of(dictType, value, locale.getLanguage())).getLabel();
        }
    }

    @SneakyThrows
    public static String parseDictDataValue(String dictType, String label) {
        return PARSE_DICT_DATA_CACHE.get(new Pair<>(dictType, label)).getValue();
    }

}
