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

import com.zanxiang.game.module.sdk.annotation.PushDataCheck;
import com.zanxiang.game.module.sdk.annotation.UnSignCheck;
import com.zanxiang.game.module.sdk.pojo.param.MiniAppPayParam;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.pojo.vo.GameShareVO;
import com.zanxiang.game.module.sdk.service.IGameAppletService;
import com.zanxiang.game.module.sdk.service.IPayApplicationService;
import com.zanxiang.game.module.sdk.service.IUserShareService;
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.Map;

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

    @Autowired
    private IUserShareService userShareService;

    @Autowired
    private IGameAppletService gameAppletService;

    @Autowired
    private IPayApplicationService payApplicationService;

    @UnSignCheck
    @PushDataCheck
    @GetMapping("/get/applet/accessToken")
    public ResultVO<Map<String, String>> getAppletToken(@RequestParam String gameId) {
        return ResultVO.ok(gameAppletService.getAppletToken(gameId));
    }

    @UnSignCheck
    @GetMapping("/applet/custom/msg/{appId}")
    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
    @PostMapping("/applet/custom/msg/{appId}")
    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
    @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
    @RequestMapping(value = "/pay/applet/wxPayNotify", method = RequestMethod.POST)
    public String wxAppletPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return payApplicationService.appletStoreNotify(request, response);
    }

    @UnSignCheck
    @GetMapping("/applet/pay/application/ban")
    public ResultVO<Boolean> payApplicationBan(@RequestParam String orderId, @RequestParam String appId) {
        return ResultVO.ok(payApplicationService.payApplicationBan(orderId, appId));
    }

    @GetMapping("/applet/kf/show/card")
    public ResultVO<GameShareVO> shareGame(UserData userData) {
        return ResultVO.ok(userShareService.getShowCard(userData));
    }
}
