package com.kxs.common.pay.service.yue;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.kxs.common.core.constant.enums.ErrorTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.core.util.R;
import com.kxs.common.core.util.RetOps;
import com.kxs.common.pay.config.WechatAccountConfig;
import com.kxs.common.pay.enums.PayPlatformEnum;
import com.kxs.common.pay.handlers.PayMessageHandler;
import com.kxs.common.pay.model.*;
import com.kxs.common.pay.service.PayService;
import com.kxs.user.api.dto.kxsapp.userAccount.PayRequestDTO;
import com.kxs.user.api.feign.RemoteKxsUserService;
import com.kxs.user.api.feign.RemoteYuePayService;
import com.kxs.user.api.model.KxsUser;
import com.lly835.bestpay.config.WxPayConfig;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.core.Ordered;

import java.util.Comparator;
import java.util.Map;
import java.util.Optional;

/**
 * 余额支付 服务
 *
 * @author 没秃顶的码农
 * @date 2024-04-24
 */
@RequiredArgsConstructor
public class YuePayService implements PayService {

    private final RemoteYuePayService remoteYuePayService;

    @Override
    public PayResponse appPay(PayRequest payReq) {

        PayRequestDTO payRequestDTO = new PayRequestDTO();
        BeanUtil.copyProperties(payReq, payRequestDTO);
        R result = remoteYuePayService.yuePay(payRequestDTO);
        if (result.getStatus() != 1) {
            throw new GlobalCustomerException(result.getMsg());
        }
        YuePayMessage payMessage = BeanUtil.toBean(result.getData(), YuePayMessage.class);

        Optional.ofNullable(payMessage).orElseThrow(() -> new GlobalCustomerException("yue pay is empty"));

        return PayResponse.builder()
                .payPlatformEnum(PayPlatformEnum.YUEPAY)
                .result(JSON.toJSONString(payMessage))
                .build();
    }

    @Override
    public QueryResponse query(QueryRequest queryReq) {
        return null;
    }

    @Override
    public String payBack(HttpServletRequest request) {
        return null;
    }

}
