package cn.pluss.platform.controller.home;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.pluss.platform.klk.service.LkLPayServiceImpl;
import cn.pluss.platform.pay.PayService;
import cn.pluss.platform.ryx.pay.RyxPayService;
import cn.pluss.platform.sxf.pay.SxfPayService;
import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 统一接收支付成功异步地址
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/notify")
public class PayNotifyController {

    @Setter(onMethod_ = {@Autowired, @Qualifier("ysPayOldService")})
    private PayService ysPayOldService;

    @Setter(onMethod_ = {@Autowired, @Qualifier("ysPayService")})
    private PayService ysPayService;


    @Setter(onMethod_ = {@Autowired, @Qualifier("lkLPayService")})
    private PayService lklPayService;




    @Setter(onMethod_ = {@Autowired, @Qualifier("hfPayService")})
    private PayService hfPayService;



    @Setter(onMethod_ = {@Autowired, @Lazy})
    private SxfPayService sxfPayService;

    @Setter(onMethod_ = {@Autowired, @Lazy})
    private RyxPayService ryxPayService;

    @RequestMapping(value = "/sxfCallBack",method = RequestMethod.POST)
    public Map<String,String> sxfCallBack(@RequestBody Map<String, String> map) {
        log.info("【随行付】异步回调开始，【随行付】回调参数:{}<=====================",JSONObject.toJSONString(map));
        return (Map<String, String>) sxfPayService.payNotifyCallBack(map);
    }

    @RequestMapping(value = "/old/ysCallBack",method = RequestMethod.POST)
    public String oldYsCallBack(@RequestParam Map<String,String> params) {
        log.info("【银盛】旧版接口异步回调开始，【银盛】回调参数:{}<=====================",JSONObject.toJSONString(params));
        return (String) ysPayOldService.payNotifyCallBack(params);
    }

    @RequestMapping(value = "/ysCallBack",method = RequestMethod.POST)
    public String ysCallBack(@RequestParam Map<String,String> params) {
        log.info("【银盛】新版异步回调开始，【银盛】回调参数:{}<=====================",JSONObject.toJSONString(params));
        return (String) ysPayService.payNotifyCallBack(params);
    }
    @RequestMapping(value = "/ryxCallBack",method = RequestMethod.POST)
    public String ryxCallBack(@RequestParam Map<String,String> params) {
        log.info("【瑞银信】异步回调开始，【瑞银信】回调参数:{}<=====================",JSONObject.toJSONString(params));
        return (String) ryxPayService.payNotifyCallBack(params);
    }


        @RequestMapping(value = "/lklCallBack",method = RequestMethod.POST)
    public String lklCallBack(@RequestBody Map<String,String> params) {
        log.info("【拉卡拉】异步回调开始，【拉卡拉】回调参数:{}<=====================",JSONObject.toJSONString(params));
        return String.valueOf(lklPayService.payNotifyCallBack(params));
    }
    @RequestMapping(value = "/lklCallBackVoiceBox",method = RequestMethod.POST)
    public String lklCallBackVoiceBox(@RequestBody Map<String,String> params) {
        log.info("【拉卡拉音箱】异步回调开始，【拉卡拉】回调参数:{}<=====================",JSONObject.toJSONString(params));
        return String.valueOf(lklPayService.payNotifyCallBack(params));
    }




    @PostMapping("hfCallBack")
    public String hfCallBack(HttpServletRequest request){
        log.info("回调返回信息:{}", JSONUtil.toJsonStr(request.getParameterMap()));
        Map<String,String> map=new HashMap<>();
        try {
            //验签请参data
            String data = request.getParameter("data");
            //验签请参sign
            String sign = request.getParameter("sign");

            String type=request.getParameter("type");
            //验签标记
            boolean checkSign;
            //验签请参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            log.info("验签请参：data={}sign={}");
            //验签
            checkSign = AdapaySign.verifySign(data, sign, publicKey);
            if(checkSign){
                map.put("type",type);
                map.put("data",data);
                Map object=(Map)hfPayService.payNotifyCallBack(map);
                if(ObjectUtil.isNotEmpty(object)&&object.containsKey("code")&&"success".equals(object.get("code"))){
                    return "SUCCESS";
                }

                //验签成功逻辑
            }else {
                return "FAIL";
                //验签失败逻辑
            }
        }catch (Exception e){
            log.info("异步回调开始，参数，request={}");
            e.printStackTrace();
        }
        return "FAIL";
    }






}
