package com.zanxiang.game.module.sdk.controller;

import com.zanxiang.game.module.base.pojo.enums.PayWayEnum;
import com.zanxiang.game.module.sdk.annotation.UnSignCheck;
import com.zanxiang.game.module.sdk.annotation.ValidLogin;
import com.zanxiang.game.module.sdk.pojo.param.ProductPayParam;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.pojo.vo.PayParamVO;
import com.zanxiang.game.module.sdk.pojo.vo.SessionCheckVO;
import com.zanxiang.game.module.sdk.service.IOrderPayService;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付公共入口
 *
 * @author xufeng
 * @date 2022/6/8 14:49
 */
@RestController
@RequestMapping(value = "/api/pay")
@Slf4j
public class PayController {

    @Autowired
    private IOrderPayService orderPayService;

    @GetMapping(value = "/session/check")
    public ResultVO<SessionCheckVO> sessionCheck(@ValidLogin UserData userData) {
        return ResultVO.ok(orderPayService.sessionCheck(userData));
    }

    @UnSignCheck
    @GetMapping(value = "/param/applet")
    public ResultVO<PayParamVO> getCustomPayParam(@RequestParam String code, @RequestParam String orderId, HttpServletRequest request) {
        return ResultVO.ok(orderPayService.getPayParam(code, null, orderId, request));
    }

    @GetMapping(value = "/param/mp")
    public ResultVO<PayParamVO> getMpPayParam(@RequestParam String openId, @RequestParam String orderId,
                                              @ValidLogin UserData userData, HttpServletRequest request) {
        return ResultVO.ok(orderPayService.getPayParam(null, openId, orderId, request));
    }

    @GetMapping(value = "/device/applet")
    public ResultVO<Long> getAppletPayDevice(@ValidLogin UserData userData) {
        return ResultVO.ok(orderPayService.getAppletPayDevice(userData));
    }

    @PostMapping(value = "/create")
    public ResultVO<Map> create(@Validated @RequestBody ProductPayParam product, @ValidLogin UserData userData) {
        log.error("接收到支付请求, product : {}, userData : {}", JsonUtil.toString(product), JsonUtil.toString(userData));
        return ResultVO.ok(orderPayService.payCreate(product, userData));
    }

    @UnSignCheck
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public String aliPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return orderPayService.notify(request, response, PayWayEnum.ALI_PAY.getPayWayId());
    }

    @UnSignCheck
    @RequestMapping(value = "/wxPayNotify", method = RequestMethod.POST)
    public String wxPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return orderPayService.notify(request, response, PayWayEnum.WX_PAY.getPayWayId());
    }

    @RequestMapping(value = "/miPayNotify", method = RequestMethod.GET)
    public ResultVO<Map> miPayNotify(@RequestParam String orderId, @ValidLogin UserData userData) {
        //返回参数
        Map<String, Object> payParamMap = new HashMap<>(2);
        payParamMap.put("status", "0");
        payParamMap.put("billNo", "success");
        return ResultVO.ok(payParamMap);
    }

    @GetMapping(value = "/get/result")
    public ResultVO<Boolean> getResult(@RequestParam String orderId) {
        return ResultVO.ok(orderPayService.payResult(orderId));
    }
}
