package cc.mrbird.febs.auth.service.impl;

import cc.mrbird.febs.auth.form.RegisterBody;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.SysUser;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserDataExchangeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * 登录校验方法
 * 
 * @author ruoyi
 */
@Component
@RequiredArgsConstructor
public class SysLoginService
{
    private final RemoteUserDataExchangeService remoteUserDataExchangeService;

    private final SysRecordLogService recordLogService;

    public void logout(String loginName)
    {
        recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }

    /**
     * 注册
     */
    public void register(RegisterBody registerBody)
    {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(registerBody.getUsername(), registerBody.getPassword(), registerBody.getPhonenumber(), registerBody.getParentAuthType()))
        {
            throw new ServiceException("用户/密码/手机号码/注册类型必须填写");
        }
        if (registerBody.getUsername().length() < UserConstants.USERNAME_MIN_LENGTH
                || registerBody.getUsername().length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            throw new ServiceException("账户长度必须在2到20个字符之间");
        }
        if (registerBody.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
                || registerBody.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            throw new ServiceException("密码长度必须在8到16个字符之间");
        }
        String regexPassword = "^.*(?=.{8,16})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!\t@#$%^&*()?><|\":/.,\\\\]).*$";
        if (!Pattern.matches(regexPassword,registerBody.getPassword())){
            throw new ServiceException("用户密码长度必须介于8-16位，需包含字母大小写、数字、特殊字符");
        }
        String regex = "^[1]([3-9])[0-9]{9}$";
        if (!Pattern.matches(regex,registerBody.getPhonenumber())){
            throw new ServiceException("手机号码格式不正确");
        }

        // 注册用户信息
        SysUser sysUser = new SysUser();
        sysUser.setUserName(registerBody.getUsername());
        sysUser.setNickName(registerBody.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
        sysUser.setPhonenumber(registerBody.getPhonenumber());
        sysUser.setUserType(registerBody.getUserType());
        sysUser.setParentAuthType(registerBody.getParentAuthType());
        R<?> registerResult = null;
        // 数据交换平台
        registerResult = remoteUserDataExchangeService.registerUserInfo(sysUser, SecurityConstants.INNER);
        if (R.FAIL == registerResult.getCode())
        {
            throw new ServiceException(registerResult.getMsg());
        }
        recordLogService.recordLogininfor(registerBody.getUsername(), Constants.REGISTER, "注册成功");
    }
}
