package com.jeequan.jeepay.thirdparty.channel.kqpay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.interfaces.paychannel.IIsvmchWxConfigService;
import com.jeequan.jeepay.core.model.applyment.ApplymentSignInfo;
import com.jeequan.jeepay.core.model.applyment.PaywayFee;
import com.jeequan.jeepay.core.model.params.kqpay.KqpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class KqpayIsvmchWxConfigService implements IIsvmchWxConfigService {
    @Override
    public String queryConfiguredInfo(String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ChannelRetMsg configBindAppId(String configAppId, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ChannelRetMsg configSubscribeAppId(String configAppId, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ChannelRetMsg applyModifyMchShortName(String mchShortName, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ChannelRetMsg applyModifyMchAppPublicKey(String appPublicKey, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ApplymentSignInfo wxOpenSignInfo(MchApplyment mchApplyment) {
        ApplymentSignInfo result = new ApplymentSignInfo();

        // 获取到 服务商配置信息
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        KqpayIsvParams isvParams = (KqpayIsvParams)configContextQueryService.queryIsvParams(mchApplyment);
        // 微信开通意愿二维码地址
        result.setSignUrl(isvParams.getWxOpenUrl());
        result.setImgType(ApplymentSignInfo.IMG_TYPE_QRCONTENT);

        result.setState("当前通道无法确定认证状态");
        result.setErrInfo("");

        String succResParameter = mchApplyment.getSuccResParameter();
        JSONObject succRes = JSON.parseObject(succResParameter);
        JSONArray alipayList = succRes.getJSONArray("wechatList");
        if (alipayList.isEmpty()) {
            result.setState("未报备");
        } else {
            JSONObject aliReportData = alipayList.getJSONObject(0);
            String subMchId = aliReportData.getString("subMchId");
            if (ObjectUtils.isEmpty(subMchId)) {
                result.setErrInfo(aliReportData.getString("errorMsg"));
            } else {
                result.setChannelSubMchId(subMchId);
            }
        }

        return result;
    }

    @Override
    public ChannelRetMsg configPayBaseUrl(String configBaseUrl, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }

    @Override
    public ChannelRetMsg applyModifyMchRate(List<PaywayFee> paywayFeeList, String isvNo, String mchNo, String mchAppId, String ifCode) {
        return null;
    }
}
