package com.yami.shop.config;


import cn.hutool.core.util.StrUtil;
import com.yami.shop.common.bean.*;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

    @Autowired
    private SysConfigService sysConfigService;

    public Qiniu getQiniu(){
        // 从数据库 / 缓存中获取到配置文件信息
        return sysConfigService.getSysConfigObject(Constant.QINIU_CONFIG, Qiniu.class);
    }

    public AliOss getAliOss() {
        return sysConfigService.getSysConfigObject(Constant.ALI_OSS_CONFIG, AliOss.class);
    }

    public HuaWeiOss getHuaWeiObs() {
        return sysConfigService.getSysConfigObject(Constant.HUAWEI_OBS_CONFIG, HuaWeiOss.class);
    }

    public ALiDaYu getALiDaYu(){
        ALiDaYu aLiDaYu = sysConfigService.getSysConfigObject(Constant.ALIDAYU_CONFIG, ALiDaYu.class);
        if (aLiDaYu == null || StrUtil.isBlank(aLiDaYu.getAccessKeyId())) {
            throw new GlobalException("无法获取短信配置，无法发送短信");
        }
        return aLiDaYu;
    }


    public QuickBird getQuickBird() {
        QuickBird quickBird = sysConfigService.getSysConfigObject(Constant.QUICKBIRD_CONFIG, QuickBird.class);
        if (quickBird == null){
            return null;
        }
        String value = sysConfigService.getValue(Constant.QUICKBIRD_CONFIG);
        // Json无法解析 eBusinessID
        quickBird.setEBusinessID(getEBusinessIDByStr(value));
        return quickBird;
    }


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

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

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

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

    public Alipay getAlipay() {

        return sysConfigService.getSysConfigObject(Constant.ALIPAY_CONFIG,Alipay.class);
    }

    public Domain getDomain() {
        Domain domain = sysConfigService.getSysConfigObject(Constant.DOMAIN_CONFIG, Domain.class);
        if (domain == null || StrUtil.isBlank(domain.getApiDomainName())) {
            throw new GlobalException("请在后台页面设置回调域名后，再进行操作");
        }
        return domain;
    }

    private String getEBusinessIDByStr(String paramValue){
        String[] split = paramValue.split(",");
        for (String s : split) {
            if (s.contains("eBusinessID")){
                String[] split1 = s.split(":");
                String str = split1[1];
                String replace = str.replace("\"", "");
                return replace;
            }
        }
        return "";
    }

}
