package com.sckw.auth.controller;

import com.sckw.auth.model.vo.req.*;
import com.sckw.auth.service.IAuthService;
import com.sckw.core.common.enums.enums.DictEnum;
import com.sckw.core.model.enums.LoginMethodEnum;
import com.sckw.core.model.enums.SystemTypeEnum;
import com.sckw.core.utils.RegularUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.redis.constant.RedisConstant;
import com.sckw.redis.utils.RedissonUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @desc: 用户权限
 * @author: czh
 * @date: 2023/6/19
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private IAuthService authService;

    /**
     * @param
     * @return
     * @description 登录参数校验
     * @author zk
     * @date 2020/6/14 18:14
     **/
    public HttpResult checkParams(LoginBase params) {
        if (StringUtils.isBlank(params.getSystemType())) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "应用服务类型不能为空！");
        }
        if (StringUtils.isBlank(params.getClientType())) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "客户端类型不能为空！");
        }
        if (StringUtils.isBlank(params.getAccount())) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的账号!");
        }
        if (params.getLoginMethod() == LoginMethodEnum.ORDINARY.getValue() && StringUtils.isBlank(params.getPassword())) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的密码!");
        }

        if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !RegularUtils.matchs(RegularUtils.PHONE_REG, params.getAccount())) {
            return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, "手机号格式不正确,请检查并重新输入！");
        }
        if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(params.getCaptcha())) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的验证码！");
        }
        String key = StringUtils.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, DictEnum.SMS_LOGIN.getValue(), params.getAccount());

        RedissonUtils.putString(key, params.getCaptcha(), RedisConstant.SMS_VERIFY_CODE_VALID_TIME);
        String smsCaptcha = RedissonUtils.getString(key);
        if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(smsCaptcha)) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取！");
        }
        if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(smsCaptcha)) {
            return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取！");
        }
        if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !smsCaptcha.equals(params.getCaptcha())) {
            return HttpResult.error(HttpStatus.UN_LOGIN_CODE, "验证码不正确,请检查并重新输入!");
        }

        return HttpResult.ok();
    }
    /**--------------------------------------------------------------------------------------------------------------**/

    /**
     * @param reqVo 登录入参
     * @return HttpResult
     * @desc: 用户登录
     * @author: czh
     * @date: 2023/6/16
     */
    @PostMapping("/login")
    public HttpResult login(@Valid @RequestBody LoginReqVo reqVo,
                            @RequestHeader(name = "Client-Type", required = true) String clientType,
                            @RequestHeader(name = "System-Type", required = true) int systemType) {
        reqVo.setSystemType(systemType);
        reqVo.setClientType(clientType);
        LoginBase loginBase = new LoginBase();
        loginBase.setAccount(reqVo.getAccount());
        loginBase.setPassword(reqVo.getPassword());
        loginBase.setCaptcha(reqVo.getCaptcha());
        loginBase.setSystemType(systemType);
        loginBase.setClientType(clientType);

        loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
        if (StringUtils.isNotBlank(loginBase.getCaptcha())) {
            loginBase.setLoginMethod(LoginMethodEnum.SMS.getValue());
        }

        /**参数校验**/
        HttpResult result = checkParams(loginBase);
        if (result.getCode() != HttpStatus.SUCCESS_CODE) {
            return result;
        }

        /**运营端/企业端登录(PC/APP)**/
        if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
                || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
            return authService.commonAuth(loginBase);
        }

        /**司机端**/
        if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
            return authService.driverAuth(loginBase);
        }

        return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
    }


    /**
     * @return HttpResult
     * @desc: 根据token获取登录信息
     * @author: czh
     * @date: 2023/9/27
     */
    @PostMapping("getLoginResByToken")
    public HttpResult getLoginResByToken(@RequestHeader(name = "Client-Type", required = true) String clientType,
                                         @RequestHeader(name = "Access-Token", required = true) String token) {
        return HttpResult.ok(authService.getLoginResByToken(clientType, token));
    }


    /**
     * @param id 主键ID
     * @description 重置密码
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/resetPassword")
    public HttpResult resetPassword(@RequestParam Long id) {
        authService.resetPassword(id);
        return HttpResult.ok(HttpStatus.MSG_001);
    }

}