package com.kxs.user.biz.controller.kxsapp;


import cn.hutool.core.util.StrUtil;
import com.kxs.common.core.util.R;
import com.kxs.common.security.annotation.Inner;
import com.kxs.user.api.dto.kxsapp.app.RegisterDTO;
import com.kxs.user.api.dto.kxsapp.app.ForgetPwdDTO;
import com.kxs.user.biz.service.KxsUserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.bind.annotation.*;


/**
 * 用户表(KxsSecurityUser)表控制层
 *
 * @author 快秃顶的码农
 * @since 2023-12-05 14:06:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("app")
public class KxsAppController {

    /**
     * 服务对象
     */
    private final KxsUserService kxsUserService;

    /**
     * 注册
     *
     * @param registerDTO 注册dto
     * @return {@link R}
     */
    @PostMapping("/register")
    @Inner(value = false)
    public R register(@Valid @RequestBody RegisterDTO registerDTO)
    {

        return kxsUserService.register(registerDTO);
    }

    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @return 发送验证码
     */
    @Inner(value = false)
    @GetMapping("/sendCode")
    public R sendCode(@RequestParam("phone") String phone) {

        return R.ok(kxsUserService.sendCode(phone));
    }


    /**
     * 是否设置支付密码
     *
     * @return 是否设置支付密码
     */
    @GetMapping("/existPayPwd")
    public R existPayPwd() {
        return R.ok(kxsUserService.existPayPwd());
    }


    /**
     * 验证支付密码
     *
     * @param payPwd 支付密码
     * @return 验证支付密码
     */
    @GetMapping("/checkPayPwd")
    public R checkPayPwd(@RequestParam("payPwd") String payPwd) {

        return R.ok(kxsUserService.checkPayPwd(payPwd));
    }


    /**
     * 使用推荐码获取用户信息
     *
     * @param userCode 邀请码
     * @return 获取用户头像
     */
    @GetMapping("/userCodeInfo")
    @Inner(false)
    public R userCodeInfo(@RequestParam("userCode") String userCode) {
        return R.ok(kxsUserService.userCodeInfo(userCode));
    }


    /**
     * 验证手势密码
     *
     * @param handPwd 手势密码
     * @return 验证手势密码
     */
    @GetMapping("/checkHandPwd")
    public R checkHandPwd(@RequestParam("handPwd") String handPwd) {

        return R.ok(kxsUserService.checkHandPwd(handPwd));
    }


    /**
     * 验证登陆密码
     *
     * @param password 密码
     * @return 验证登陆密码
     */
    @GetMapping("/checkLoginPwd")
    public R checkLoginPwd(@RequestParam("password") String password) {

        return R.ok(kxsUserService.checkLoginPwd(password));
    }


    /**
     * 退出登陆
     *
     * @return 退出登陆
     */
    @DeleteMapping()
    public R logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) {
        if (StrUtil.isBlank(authHeader)) {
            return R.ok(Boolean.TRUE);
        }
        String tokenValue = authHeader.replace(OAuth2AccessToken.TokenType.BEARER.getValue(), StrUtil.EMPTY).trim();
        return R.ok(kxsUserService.logout(tokenValue));
    }


    /**
     * 找回密码
     *
     * @param param 实体对象
     * @return 找回密码
     */
    @Inner(value = false)
    @PostMapping("/forgetPwd")
    public R forgetPwd(@Valid @RequestBody ForgetPwdDTO param) {

        return R.ok(kxsUserService.forgetPwd(param));
    }


}

