package com.fdkankan.ucenter.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.annotation.VerifySign;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.service.impl.AppService;
import com.fdkankan.ucenter.service.impl.LoginService;
import com.fdkankan.ucenter.vo.request.AppLoginParam;
import com.fdkankan.ucenter.vo.request.LoginParam;
import com.fdkankan.ucenter.vo.request.RegisterParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ucenter/sso/app")
public class AppController extends BaseController {

    @Autowired
    private AppService appService;
    @Autowired
    private LoginService loginService;
    /**
     * 登录
     * phoneNum 用户名
     * password 密码
     */
    @PostMapping("/userLogin")
    @VerifySign
    public Result userLogin(@RequestBody LoginParam param){
        return Result.success(appService.login(param));
    }

    /**
     * app扫码登录
     * appUserName  相机设备嘛
     * appPassword  相机密码
     * uuid
     */
    @PostMapping("/login")
    public Result login(@RequestBody AppLoginParam param){
        appService.appLogin(param);
        return Result.success();
    }
    /**
     * app登录
     * appUserName  相机设备嘛
     * appPassword  相机密码
     */
    @PostMapping("/login2")
    @VerifySign
    public Result login2(@RequestBody AppLoginParam param){
        return Result.success(appService.login2(param));
    }

    /**
     * 验证码登陆
     * @return
     */
    @PostMapping("/quickLogin")
    public Result quickLogin(@RequestBody LoginParam param){
        return  Result.success(appService.quickLogin(param));
    }

    /**
     * 注册
     * @param param
     * @return
     */
    @PostMapping("/register")
    @VerifySign
    public Result register(@RequestBody RegisterParam param){
        param.setConfirmPwd(param.getPassword());
        param.setClear("YES");
        param.setAgentKey(getAgentKey());
        loginService.register(param);
        return Result.success();
    }

    /**
     * 登出
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @VerifySign
    public Result logout() {
        appService.logout(getToken());
        return Result.success();
    }

    /**
     * app 获取随机昵称
     */
    @PostMapping("/getNickName")
    @VerifySign
    public Result getNickName(){
        return Result.success(appService.getNickName());
    }

    /**
     * 检测手机号码是否注册
     */
    @PostMapping("/checkUserName")
    @VerifySign
    public Result checkUserName(@RequestBody AppLoginParam param){
        loginService.checkUser(param.getAppUserName(),false);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPassword")
    @VerifySign
    public Result resetPassword(@RequestBody RegisterParam param){
        param.setConfirmPwd(param.getPassword());
        param.setClear("YES");
        loginService.changePassword(param);
        return Result.success();
    }
}
