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

import com.zanxiang.game.module.sdk.annotation.UnSignCheck;
import com.zanxiang.game.module.sdk.pojo.param.MiniAppPayParam;
import com.zanxiang.game.module.sdk.service.IGameAppletService;
import com.zanxiang.game.module.sdk.service.IPayApplicationService;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author : lingfeng
 * @time : 2023-01-04
 * @description : 小程序接口
 */
@Api(tags = "小程序接口")
@RestController
@RequestMapping(value = "/api")
@Slf4j
public class AppletController {

    @Autowired
    private IGameAppletService gameAppletService;

    @Autowired
    private IPayApplicationService payApplicationService;

    @UnSignCheck
    @ApiOperation(value = "小程序客服消息认证")
    @GetMapping("/applet/custom/msg/{appId}")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)})
    public String appletMsgCheck(@PathVariable("appId") String appId, @RequestParam String signature,
                                 @RequestParam String timestamp, @RequestParam String nonce,
                                 @RequestParam String echostr) throws Exception {
        return gameAppletService.appletMsgCheck(appId, signature, timestamp, nonce, echostr);
    }

    @UnSignCheck
    @ApiOperation(value = "小程序客服消息事件监听")
    @PostMapping("/applet/custom/msg/{appId}")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)})
    public String appletMsg(@PathVariable("appId") String appId, @RequestParam String signature,
                            @RequestParam String timestamp, @RequestParam String nonce,
                            @RequestBody String postData) throws Exception {
        return gameAppletService.appletMsg(appId, signature, timestamp, nonce, postData);
    }

    @UnSignCheck
    @ApiOperation(value = "商城小程序支付")
    @PostMapping(value = "/pay/applet/create")
    public ResultVO<Map> create(@Validated @RequestBody MiniAppPayParam payParam, HttpServletRequest request) {
        log.error("接收到支付请求, payParam : {}", JsonUtil.toString(payParam));
        return payApplicationService.appletStorePay(payParam, request);
    }

    @UnSignCheck
    @ApiOperation(value = "商城小程序支付回调")
    @RequestMapping(value = "/pay/applet/wxPayNotify", method = RequestMethod.POST)
    public String wxAppletPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return payApplicationService.appletStoreNotify(request, response);
    }

    @UnSignCheck
    @ApiOperation(value = "小程序接口封禁回调")
    @GetMapping("/applet/pay/application/ban")
    public ResultVO<Boolean> payApplicationBan(@RequestParam String orderId, @RequestParam String appId) {
        return ResultVO.ok(payApplicationService.payApplicationBan(orderId, appId));
    }
}
