package com.fdkankan.ucenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.RedisKeyUtil;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.service.impl.LoginService;
import com.fdkankan.ucenter.vo.request.LoginParam;
import com.fdkankan.ucenter.vo.request.RegisterParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


/**
 * 登录
 */
@RestController
@RequestMapping("/ucenter/sso/user")
public class LoginController extends BaseController {

    @Autowired
    LoginService loginService;
    @Autowired
    RedisUtil redisUtil;
    @Value("${spring.profiles.active}")
    private String environment;
    @Value("${admin.register.validCode:2a22bac40f44af4d3b5fdc20ea706fc5}")
    private String registerValidCode;
    /**
     * 登录
     * phoneNum 用户名
     * password 密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginParam param){
        return Result.success(loginService.login(param));
    }
    /**
     * 登录
     * phoneNum 用户名
     * password 密码
     */
    @PostMapping("/loginClear")
    public Result loginClear(@RequestBody LoginParam param){
        return Result.success(loginService.loginClear(param));
    }

    /**
     * 相机扫码登录 获取二维码
     */
    @GetMapping("/createLoginQrCode")
    public Result createLoginQrCode() throws Exception {
        return  Result.success(loginService.createLoginQrCode());
    }

    /**
     * 扫码验证登录
     */
    @PostMapping("/sendUserInfo")
    public Result sendUserInfo(@RequestBody JSONObject jsonObject){
        return Result.success(loginService.sendUserInfo(jsonObject.getString("uuid")));
    }
    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result logout(){
        loginService.logout(getToken());
        return Result.success();
    }

    /**
     *  检测用户名
     *  phoneNum  用户名
     */
    @PostMapping("/checkUser")
    public Result checkUser(@RequestBody LoginParam param){
        loginService.checkUser(param.getPhoneNum(),true);
        return Result.success();
    }
    /**
     *  获取验证码
     *  areaNum  区号
     *  phoneNum 手机号码
     */
    @PostMapping("/getMsgAuthCode")
    public Result getMsgAuthCode(@RequestBody LoginParam param) throws Exception {
        loginService.getMsgAuthCode(param.getAreaNum(),param.getPhoneNum());
        return Result.success();
    }
    /**
     * 发送邮件验证码
     */
    @PostMapping("/getEmailAuthCode")
    public Result getEmailAuthCode(@RequestBody RegisterParam param) throws Exception {
        loginService.getEmailAuthCode(param.getEmail(),param.getCountry());
        return Result.success();
    }

    /**
     *  注册
     *  areaNum  区号
     *  phoneNum 手机号码
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterParam param) throws Exception {
        if(registerValidCode.equals(param.getMsgAuthCode())){
            param.setClear("YES");
        }
        param.setAgentKey(getAgentKey());
        loginService.register(param);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody RegisterParam param){
        loginService.changePassword(param);
        return Result.success();
    }
    /**
     * 检验token是否过期
     */
    @PostMapping(value = "/checkToken")
    public Result checkToken(){
        loginService.loginCheck(getToken());
        return Result.success();
    }

    /**
     * 检验验证码是否有效
     */
    @PostMapping(value = "/checkSms")
    public Result checkSms(@RequestBody RegisterParam param){
        loginService.checkSms(param.getMsgAuthCode(),getUserName(),false);
        return Result.success();
    }

    /**
     * getToken
     */
    @PostMapping("/getToken")
    public Result getToken(@RequestBody LoginParam param){
        return Result.success(loginService.loginClear(param));
    }


    /**
     *  测试使用 获取具体验证码
     * @param user
     * @return
     */
   // @RequestMapping(value = "/findMsgAuthCode", method = RequestMethod.POST)
    public Result findMsgAuthCode(@RequestBody LoginParam user) {

        String redisKey = RedisKeyUtil.PREFIX_MSG_AUTH_CODE +user.getPhoneNum();
        if(!"prod".equals(environment)){
            return Result.success(redisUtil.get(redisKey));
        }

        if("18819272208".equals(user.getPhoneNum()) || "12369874542".equals(user.getPhoneNum())){
            return Result.success(redisUtil.get(redisKey));
        }

        return Result.success();
    }

}

