package com.fdkankan.platform.user.controller.sso;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.user.entity.User;
import com.fdkankan.platform.user.request.RequestUser;
import com.fdkankan.platform.user.service.IUserService;
import com.fdkankan.platform.user.service.sso.LoginService;
import com.fdkankan.platform.user.service.sso.MsgService;
import com.fdkankan.platform.user.vo.SSOUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登陆管理-登陆模块
 */
@RestController
@RequestMapping("/service/sso/user")
public class SsoUserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private MsgService msgService;
    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * phoneNum      用户名
     * cameraType   相机类型，1双目，4八目
     * msgAuthCode  验证码
     */
    @PostMapping(value = "/login")
    public SSOUserVo login(@RequestBody RequestUser user) throws Exception {
        return loginService.login(user);
    }

    /**
     * 验证码登陆
     */
    @PostMapping(value = "/quickLogin")
    public SSOUserVo quickLogin(@RequestBody RequestUser user) throws Exception {
        return loginService.quickLogin(user);
    }

    @PostMapping(value = "/logout")
    public String logout(@RequestHeader String token) {
        return loginService.logout(token);
    }

    /**
     * 注册
     * phoneNum 用户名
     * password 密码
     * confirmPwd 确认密码
     * msgAuthCode 验证码
     * nickName 昵称
     * country 国家
     */
    @PostMapping(value = "/register")
    public String register(@RequestBody RequestUser user) throws Exception {
        return userService.register(user);
    }

    /**
     * 获取验证码
     * areaNum  区号
     * phoneNum 手机号
     */
    @PostMapping(value = "/getMsgAuthCode")
    public String getMsgAuthCode(@RequestBody RequestUser user) throws Exception{
        return msgService.getMsgAuthCode(user);
    }

    /**
     * 发送邮件验证码
     * email    邮箱地址
     * country  国家，0中国，1海外
     */
    @PostMapping(value = "/getEmailAuthCode")
    public String getEmailAuthCode(@RequestBody RequestUser user) throws Exception{
        return msgService.getEmailAuthCode(user);
    }

    /**
     * 创建快速登陆的二维码
     */
    @PostMapping("/createLoginQrCode")
    public JSONObject createLoginQrCode() throws Exception {
        return loginService.createLoginQrCode();
    }

    /**
     * 校验扫码登陆
     * uuid
     */
    @PostMapping(value = "/sendUserInfo")
    public JSONObject sendUserInfo(@RequestBody RequestUser user) throws Exception {
        return loginService.loginQrCode(user.getUuid());
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/changePassword")
    public String changePassword(@RequestBody RequestUser user){
        return userService.changePassword(user);
    }

    /**
     * 检验用户是否存在
     */
    @PostMapping(value = "/checkUser")
    public String checkUser(@RequestBody RequestUser user){
        if (StringUtils.isEmpty(user.getPhoneNum())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        User dbUser = userService.getUserByUserName(user.getPhoneNum());
        if(dbUser == null){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST);
        }
        return ServerCode.SUCCESS.message();
    }

    /**
     * 检验token是否过期
     */
    @PostMapping(value = "/checkToken")
    public String checkToken(@RequestHeader String token){
        return  loginService.loginCheck(token);
    }

    /**
     * 检验验证码是否有效
     * @return
     */
    @PostMapping(value = "/checkSms")
    public String checkSms(@RequestBody RequestUser user,@RequestHeader String token){
        return  loginService.checkSms(token,user.getMsgAuthCode());
    }

    /**
     * 登录
     */
    @PostMapping(value = "/getToken")
    public String getToken(@RequestBody RequestUser user) throws Exception {
        return loginService.getToken(user.getPhoneNum(),user.getPassword());
    }

}
