package com.supServer.project.controller;

import com.supServer.common.constant.Constants;
import com.supServer.common.constant.UserConstants;
import com.supServer.framework.security.mode.LoginBody;
import com.supServer.framework.security.mode.LoginUser;
import com.supServer.framework.security.mode.RegisterBody;
import com.supServer.framework.security.mode.SmsLoginBody;
import com.supServer.framework.security.service.TokenService;
import com.supServer.framework.security.service.UserLoginService;
import com.supServer.framework.security.service.UserRegisterService;
import com.supServer.framework.web.domain.AjaxResult;
import com.supServer.project.entity.UserInfo;
import com.supServer.project.entity.vo.ForgotPasswordVo;
import com.supServer.project.entity.vo.UserResetPwdVo;
import com.supServer.project.mapper.UserInfoMapper;
import com.supServer.project.service.SmsService;
import com.supServer.project.service.UserInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static com.supServer.common.utils.SecurityUtils.getLoginUser;


@RestController
public class UserController {

    @Autowired
    private UserLoginService  userLoginService;

    @Autowired
    private UserRegisterService registerService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        //AesUtil.decode(loginBody.getUserName())
        UserInfo userInfo = userInfoService.selectUserByNumber(loginBody.getUserName());
        if(ObjectUtils.isEmpty(userInfo)||userInfo.getAccountStatus().equals("2")){
            return AjaxResult.error("当前手机号码未注册");
        }
        if(userInfo.getAccountStatus().equals("0")){
            return AjaxResult.error("当前账号认证审核中，请耐心等待！");
        }
        if(userInfo.getAccountStatus().equals("3")){
            return AjaxResult.error("当前账号已注销");
        }
        String oldPassword = BCrypt.hashpw(loginBody.getPassword(), userInfo.getPassword());
        if (!oldPassword.equals(userInfo.getPassword())) {
            return AjaxResult.error("密码错误");
        }
        //AesUtil.decode(loginBody.getUserName()),AesUtil.decode(loginBody.getPassword()),loginBody.getCode(),loginBody.getUuid(),loginBody.getType()
        String token = userLoginService.login(loginBody.getUserName(),loginBody.getPassword(),loginBody.getCode(),loginBody.getUuid());
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 短信登录
     *
     * @param smsLoginBody 登录信息
     * @return 结果
     */
    @PostMapping("/smsLogin")
    public AjaxResult smsLogin( @RequestBody SmsLoginBody smsLoginBody) {
//        UserInfo userInfo = userInfoService.selectUserByNumber(smsLoginBody.getPhone());
//        if(ObjectUtils.isEmpty(userInfo)||userInfo.getAccountStatus().equals("2")){
//            return AjaxResult.error("当前手机号码未注册");
//        }
//        if(userInfo.getAccountStatus().equals("0")){
//            return AjaxResult.error("当前账号认证审核中，请耐心等待！");
//        }
        if(!smsService.validateCode(smsLoginBody.getPhone(), smsLoginBody.getSmsCode())){
            return AjaxResult.error("验证码错误！");
        }
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = userLoginService.smsLogin(smsLoginBody.getPhone(),smsLoginBody.getSmsCode());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }




    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) throws IOException {
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ?  AjaxResult.success() : AjaxResult.error(msg);
    }


    /*** 修改密码
     *@Param:
     *@return:
     */
    @PostMapping("/system/userInfo/resetPwd")
    public AjaxResult resetPwd(@RequestBody UserResetPwdVo userInfoVo, HttpServletRequest request){
        if (StringUtils.isEmpty(userInfoVo.getOldPassword())) {
            return AjaxResult.error("请输入原密码");
        }
        if (StringUtils.isEmpty(userInfoVo.getNewPassword())) {
            return AjaxResult.error("新输入新密码");
        }
        if (userInfoVo.getNewPassword().length() < UserConstants.PASSWORD_MIN_LENGTH ||
                userInfoVo.getNewPassword().length() > UserConstants.PASSWORD_MAX_LENGTH) {
            return AjaxResult.error("密码长度必须在8到12个字符之间");
        }
        return userInfoService.resetPwd(userInfoVo);
    }


    /***忘记密码
     *@Param:
     *@return:
     */
    @PostMapping("/forgotPassword")
    public AjaxResult forgotPassword(@RequestBody ForgotPasswordVo forgotPasswordVo){
        return userInfoService.forgotPassword(forgotPasswordVo);
    }

    /***校验账户
     *@Param:
     *@return:
     */
    @PostMapping("/checkUser")
    public AjaxResult checkUser(@RequestBody ForgotPasswordVo forgotPasswordVo){
        UserInfo userInfo = userInfoService.selectUserByNumber(forgotPasswordVo.getPhone());
        if("0".equals(forgotPasswordVo.getType())&&ObjectUtils.isNotEmpty(userInfo)&&userInfo.getAccountStatus().equals("1")){
            return AjaxResult.error("当前手机号码已注册");
        }
        if("0".equals(forgotPasswordVo.getType())&&ObjectUtils.isNotEmpty(userInfo)&&userInfo.getAccountStatus().equals("0")){
            return AjaxResult.error("注册审核中,请耐心等待");
        }
        if("1".equals(forgotPasswordVo.getType())&&ObjectUtils.isEmpty(userInfo)){
            return AjaxResult.error("当前手机号码未注册");
        }
        if("1".equals(forgotPasswordVo.getType())&&ObjectUtils.isNotEmpty(userInfo)&&!userInfo.getAccountStatus().equals("1")){
            return AjaxResult.error("当前手机号码未注册");
        }
        return smsService.sendCode(forgotPasswordVo.getPhone(),"1");
    }

    /***退出登录
     *@Param:
     *@return:
     */
    @GetMapping("/logOut")
    public void logOut(){
        tokenService.resetPwd(getLoginUser().getUserId());
    }



    /*** 获取用户信息
     *@Param:
     *@return:
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(HttpServletRequest request){
        LoginUser user = tokenService.getLoginUser(request);
        return AjaxResult.success("操作成功",user);
    }
}
