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

import com.zanxiang.game.module.sdk.annotation.UnSignCheck;
import com.zanxiang.game.module.sdk.annotation.ValidLogin;
import com.zanxiang.game.module.sdk.pojo.param.LoginMobileParam;
import com.zanxiang.game.module.sdk.pojo.param.LoginPasswordParam;
import com.zanxiang.game.module.sdk.pojo.param.LoginVxCodeParam;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.pojo.vo.CpTokenCheckVO;
import com.zanxiang.game.module.sdk.pojo.vo.UserLoginVO;
import com.zanxiang.game.module.sdk.service.IRegisterLoginService;
import com.zanxiang.game.module.sdk.service.IUserTokenService;
import com.zanxiang.module.util.pojo.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author : lingfeng
 * @time : 2022-06-21
 * @description : 用户注册登录
 */
@RestController
@RequestMapping(value = "/api/login")
public class LoginController {

    @Autowired
    private IRegisterLoginService registerLoginService;

    @Autowired
    private IUserTokenService userTokenService;

    @PostMapping("/wx/code")
    public ResultVO<UserLoginVO> loginWxCode(@Validated @RequestBody LoginVxCodeParam param, UserData userData,
                                             HttpServletRequest request) {
        return registerLoginService.loginWxCode(param, userData, request);
    }

    @UnSignCheck
    @GetMapping("/token/check/new")
    public ResultVO<CpTokenCheckVO> cpTokenCheckNew(@RequestParam String appId, @RequestParam Long userId,
                                                    @RequestParam String token, @RequestParam String sign) {
        return userTokenService.cpTokenCheckNew(appId, userId, token, sign);
    }

    @UnSignCheck
    @GetMapping("/token/check")
    public ResultVO userTokenCheck(@RequestParam String appId, @RequestParam Long userId, @RequestParam String token,
                                   @RequestParam String sign) {
        return userTokenService.cpTokenCheck(appId, userId, token, sign);
    }

    @GetMapping("/token/expireTime/check")
    public ResultVO<Boolean> userTokenExpireTimeCheck(UserData userData) {
        return ResultVO.ok(userTokenService.userTokenExpireTimeCheck(userData));
    }

    @PostMapping("/password")
    public ResultVO<UserLoginVO> loginPassword(@Validated @RequestBody LoginPasswordParam param, UserData userData) {
        return registerLoginService.loginPassword(param, userData);
    }

    @PostMapping("/mobile")
    public ResultVO<UserLoginVO> loginMobile(@Validated @RequestBody LoginMobileParam param, UserData userData) {
        return registerLoginService.loginMobile(param, userData);
    }

    @GetMapping("/sign/out")
    public ResultVO<Boolean> loginOut(@ValidLogin UserData userData) {
        return ResultVO.ok(registerLoginService.loginOut(userData));
    }

    @GetMapping("/write/off")
    public ResultVO<Boolean> writeOff(@ValidLogin UserData userData) {
        return ResultVO.ok(registerLoginService.writeOff(userData));
    }
}
