
package com.tmerclub.cloud.api.platform.config;


import cn.hutool.core.util.StrUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.bean.*;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 商城配置文件
 * 支付配置、文件上传配置、短信配置、快递配置、小程序配置、微信网页开发配置、公众号配置
 * @author tmerclub
 */
@Component
public class FeignShopConfig {

    @DubboReference
    private ConfigFeignClient configFeignClient;

    /**
     * 缓存
     */
    private static final Cache<Object, Object> CONFIG_MAP = Caffeine.newBuilder()
            // 数量上限
            .maximumSize(100)
            // 过期机制
            .expireAfterWrite(5, TimeUnit.MINUTES).build();


    public DaYu getDaYu() {
        DaYu daYu = getSysConfigObject(Constant.ALIDAYU_CONFIG, DaYu.class);
        if (daYu == null || StrUtil.isBlank(daYu.getAccessKeyId())) {
            throw new LuckException("无法获取短信配置，无法发送短信");
        }
        return daYu;
    }


    public QuickBird getQuickBird() {
        return getSysConfigObject(Constant.QUICKBIRD_CONFIG, QuickBird.class);
    }

    public AliLive getAliLive() {
        return getSysConfigObject(Constant.ALI_LIVE_CONFIG, AliLive.class);
    }

    public Quick100 getQuick100() {
        return getSysConfigObject(Constant.QUICK100_CONFIG, Quick100.class);
    }

    public AliQuick getAliQuickConfig() {
        return getSysConfigObject(Constant.ALI_QUICK_CONFIG, AliQuick.class);
    }


    public WxMiniApp getWxMiniApp() {
        WxMiniApp wxMiniApp = getSysConfigObject(Constant.MA_CONFIG, WxMiniApp.class);
        if (wxMiniApp == null || StrUtil.isBlank(wxMiniApp.getAppId())) {
            // 请在后台页面设置微信小程序信息后，再进行操作
            throw new LuckException("请在后台页面设置微信小程序信息后，再进行操作");
        }
        return wxMiniApp;
    }

    public WxMp getWxMp() {
        WxMp wxMp = getSysConfigObject(Constant.MP_CONFIG, WxMp.class);
        if (wxMp == null || StrUtil.isBlank(wxMp.getAppId())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new LuckException("请在后台页面设置微信公众号信息后，再进行操作");
        }
        return wxMp;
    }

    public WxApp getWxApp() {
        WxApp wxApp = getSysConfigObject(Constant.MX_APP_CONFIG, WxApp.class);
        if (wxApp == null || StrUtil.isBlank(wxApp.getAppId())) {
            // 请在后台页面设置微信开放平申请应用相关信息后，再进行操作
            throw new LuckException("请在后台页面设置微信开放平申请应用相关信息后，再进行操作");
        }
        return wxApp;
    }

    public WxPay getWxPay() {
        WxPay wxPay = getSysConfigObject(Constant.WXPAY_CONFIG, WxPay.class);
        if (wxPay == null || StrUtil.isBlank(wxPay.getMchId())) {
            // 请在后台页面设置微信支付信息后，再进行操作
            throw new LuckException("请在后台页面设置微信支付信息后，再进行操作");
        }
        return wxPay;
    }

    public Alipay getAlipay() {
        return getSysConfigObject(Constant.ALIPAY_CONFIG, Alipay.class);
    }

    public Allinpay getAllinpay() {
        return getSysConfigObject(Constant.ALLINPAY_CONFIG, Allinpay.class);
    }

    /**
     * 获取文件上传配置
     * @return 文件上传配置
     */
    public UploadFile getUploadFile() {
        return getSysConfigObject(Constant.UPLOAD_FILE_CONFIG, UploadFile.class);
    }

    /**
     * 获取系统配置，缓存起来
     */
    @SuppressWarnings("unchecked")
    public <T> T getSysConfigObject(String key, Class<T> clazz) {
//        T configData = (T) CONFIG_MAP.get(key, cacheKey -> {
        ServerResponseEntity<String> configResponse = configFeignClient.getConfig(key);
        if (!configResponse.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        String value = configResponse.getData();
        if (Objects.equals(String.class, clazz)) {
            return (T) value;
        } else {
            return Json.parseObject(value, clazz);
        }
    }
}
