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.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
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 : 用户注册登录
 */
@Api(tags = "注册登录接口")
@RestController
@RequestMapping(value = "/api/login")
public class LoginController {

    @Autowired
    private IRegisterLoginService registerLoginService;

    @Autowired
    private IUserTokenService userTokenService;

    @ApiOperation(value = "微信小游戏code授权登录")
    @PostMapping("/wx/code")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = UserLoginVO.class)})
    public ResultVO<UserLoginVO> loginWxCode(@Validated @RequestBody LoginVxCodeParam param, UserData userData) {
        return registerLoginService.loginWxCode(param, userData);
    }

    @UnSignCheck
    @ApiOperation(value = "CP登录凭证验证")
    @GetMapping("/token/check")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Long.class)})
    public ResultVO userTokenCheck(@RequestParam String appId, @RequestParam Long userId, @RequestParam String token,
                                   @RequestParam String sign) {
        return userTokenService.cpTokenCheck(appId, userId, token, sign);
    }

    @ApiOperation(value = "用户登录凭证时效验证")
    @GetMapping("/token/expireTime/check")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Long.class)})
    public ResultVO<Boolean> userTokenExpireTimeCheck(UserData userData) {
        return ResultVO.ok(userTokenService.userTokenExpireTimeCheck(userData));
    }

    @ApiOperation(value = "用户账号注册/登录")
    @PostMapping("/password")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = UserLoginVO.class)})
    public ResultVO<UserLoginVO> loginPassword(@Validated @RequestBody LoginPasswordParam param, UserData userData) {
        return registerLoginService.loginPassword(param, userData);
    }

    @ApiOperation(value = "用户手机号注册/登录")
    @PostMapping("/mobile")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = UserLoginVO.class)})
    public ResultVO<UserLoginVO> loginMobile(@Validated @RequestBody LoginMobileParam param, UserData userData) {
        return registerLoginService.loginMobile(param, userData);
    }

    @ApiOperation(value = "用户退出")
    @GetMapping("/sign/out")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = UserLoginVO.class)})
    public ResultVO<Boolean> loginOut(@ValidLogin UserData userData) {
        return ResultVO.ok(registerLoginService.loginOut(userData));
    }

    @ApiOperation(value = "用户账号注销")
    @GetMapping("/write/off")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = UserLoginVO.class)})
    public ResultVO<Boolean> writeOff(@ValidLogin UserData userData) {
        return ResultVO.ok(registerLoginService.writeOff(userData));
    }
}
