package com.zanxiang.game.module.sdk.util;

import com.zanxiang.game.module.base.pojo.enums.HttpStatusEnum;
import com.zanxiang.module.util.encryption.Md5Util;
import org.apache.logging.log4j.util.Strings;

/**
 * @author : lingfeng
 * @time : 2022-06-22
 * @description : 用户注册工具类
 */
public class RegisterUtil {

    /**
     * 密码加密盐值
     */
    private static final String CMF_PASSWORD_SALT = "ZX_PASSWORD_SALT";

    /**
     * 用户名最小长度
     */
    private static final int USER_NAME_LENGTH_MIN = 8;

    /**
     * 用户名最大长度
     */
    private static final int USER_NAME_LENGTH_MAX = 16;

    /**
     * 密码最小长度
     */
    private static final int PASSWORD_LENGTH_MIN = 6;

    /**
     * 密码最大长度
     */
    private static final int PASSWORD_LENGTH_MAX = 24;

    /**
     * 用户名合规检测
     *
     * @param userName : 用户名验证
     * @return : 返回验证结果
     */
    public static HttpStatusEnum checkUserName(String userName) {
        if (Strings.isBlank(userName)) {
            return HttpStatusEnum.USERNAME_EMPTY;
        }
        //用户名长度验证
        if (userName.length() < USER_NAME_LENGTH_MIN) {
            return HttpStatusEnum.USERNAME_TOO_SHORT;
        }
        if (userName.length() > USER_NAME_LENGTH_MAX) {
            return HttpStatusEnum.USERNAME_TOO_LONG;
        }
        //用户名只能是数字字母组合
        if (!RegexUtil.checkUserName(userName)) {
            return HttpStatusEnum.USERNAME_BAD_CHAR;
        }
        //密码验证
        return HttpStatusEnum.SUCCESS;
    }

    /**
     * 密码合规检测
     *
     * @param password : 密码验证
     * @return : 返回验证结果
     */
    public static HttpStatusEnum checkPassword(String password) {
        if (Strings.isBlank(password)) {
            return HttpStatusEnum.PASSWORD_EMPTY;
        }
        if (password.length() < PASSWORD_LENGTH_MIN) {
            return HttpStatusEnum.PASSWORD_TOO_SHORT;
        }
        if (password.length() > PASSWORD_LENGTH_MAX) {
            return HttpStatusEnum.PASSWORD_TOO_LONG;
        }
        //密码只能是数字字母组合
        if (!RegexUtil.checkPassword(password)) {
            return HttpStatusEnum.PASSWORD_BAD_CHAR;
        }
        return HttpStatusEnum.SUCCESS;
    }

    /**
     * 随机用户昵称
     *
     * @return : 返回用户昵称
     */
    public static String randomNickName(String userName) {
        return "smlgm" + Math.abs(userName.hashCode());
    }

    /**
     * 密码cfm加密
     *
     * @param password : 密码
     * @return : 返回加密完的密码
     */
    public static String cmfPassword(String password) {
        String passwordSalt = password + CMF_PASSWORD_SALT;
        return "###" + Md5Util.encrypt32(Md5Util.encrypt32(passwordSalt));
    }
}
