package com.bonus.core.allocation.api;

import cn.hutool.core.util.StrUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.core.common.enums.MetadataModelTypeEnum;
import com.bonus.core.common.thirdutils.alipay.model.AlipayReq;
import com.bonus.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName: AllocMetadataApi
 * @author: tqzhang
 * @Date: 2025/1/24 23:51
 * @version: 1.0
 * @description:
 */
@Component
public class AllocMetadataApi {
    private static final Logger log = LoggerFactory.getLogger(AllocMetadataApi.class);
    @Resource
    @Lazy
    private GlobalMetadataApi globalMetadataApi;
    @Resource
    @Lazy
    private AllocPayMetadataApi allocPayMetadataApi;
    @Autowired
    @Lazy
    private AllocMealtimeApi allocMealtimeApi;
    @Autowired
    @Lazy
    private AllocOrderMetadataApi allocOrderMetadataApi;
    @Autowired
    @Lazy
    private AllocDeliveryApi allocDeliveryApi;

    public <T> T queryMetadataModel(MetadataModelTypeEnum modelTypeEnum, T t) {
        return this.globalMetadataApi.getModel(modelTypeEnum, t, (Long)null, (Long)null);
    }

    public AlipayReq checkAndCreateAlipayReq() {
        String alipayAppAuthToken = this.getMetadataValue(MetadataModelTypeEnum.MOBILE, "alipayAppAuthToken");
        String alipayNotifyUrl = this.allocPayMetadataApi.queryUnifyNotifyHost();
        String aliPayPublicKey = this.getMetadataValue(MetadataModelTypeEnum.PAY, "aliPayPublicKey");
        String alipayAesKey = this.getMetadataValue(MetadataModelTypeEnum.MOBILE, "alipayAesKey");
        if (StringUtils.isBlank(alipayAppAuthToken)) {
            log.debug("[支付宝]未找到当前应用信息");
            throw new ServiceException("未找到当前应用信息");
        } else {
            return new AlipayReq(alipayAppAuthToken, alipayNotifyUrl, aliPayPublicKey, alipayAesKey);
        }
    }
    public String getMetadataValue(MetadataModelTypeEnum modelTypeEnum, String modelKey) {
        return this.getMetadataValue(modelTypeEnum, modelKey, "");
    }
    public String getMetadataValue(MetadataModelTypeEnum modelTypeEnum, String modelKey, String defaultValue) {
        String metadataValue = this.globalMetadataApi.getAvailableValue(modelTypeEnum, modelKey, (Long)null, (Long)null);
        return StrUtil.isNotBlank(metadataValue) ? metadataValue : defaultValue;
    }

    public <T> T queryMetadataModelByAreaId(MetadataModelTypeEnum modelTypeEnum, T t, Long areaId) {
        return this.globalMetadataApi.getModelByAreaId(modelTypeEnum, t, areaId);
    }

    public AllocPayMetadataApi payApi() {
        return this.allocPayMetadataApi;
    }

    public AllocMealtimeApi mealtimeApi() {
        return this.allocMealtimeApi;
    }

    public AllocOrderMetadataApi orderApi() {
        return this.allocOrderMetadataApi;
    }

    public AllocDeliveryApi deliveryApi() {
        return this.allocDeliveryApi;
    }
}
