package com.gci.boot.common.util;

import cn.hutool.core.util.StrUtil;
import com.gci.boot.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class StringUtil {

    public static String checkString(String arg, Integer length) {
        if(StrUtil.isBlank(arg)){
            return "不能为空！";
        }
        String temp = arg.replace(" ","");
        if(StrUtil.isBlank(temp)){
            return "不能为空！";
        }
        if(StrUtil.isNotBlank(arg)){
            Pattern p = Pattern.compile(CommonConstant.SPECIAL_CHAR);
            Matcher m = p.matcher(arg);
            if (m.find()) {
                StringBuilder specialCharacters = new StringBuilder();
                do {
                    specialCharacters.append(m.group());
                } while (m.find());
                return "输入的字符串： "+ arg +" 包含特殊字符：" + specialCharacters.toString()+ " ,请修改！";
            }
            if(length != 0){
                if(arg.length() > length){
                    return "长度不能超过" + length + "字符！";
                }
            }
        }
        return null;
    }

    public static String checkString2(String arg, Integer length) {
        if(Objects.nonNull(arg) && arg.length() > 0){
            boolean isAllSpaces = true;
            for (int i = 0; i < arg.length(); i++) {
                if (!Character.isWhitespace(arg.charAt(i))) {
                    isAllSpaces = false;
                    break;
                }
            }
            if(isAllSpaces){
                return "不能全为空格！";
            }
        }

        if(StrUtil.isNotBlank(arg)){
            Pattern p = Pattern.compile(CommonConstant.SPECIAL_CHAR);
            Matcher m = p.matcher(arg);
            if (m.find()) {
                StringBuilder specialCharacters = new StringBuilder();
                do {
                    specialCharacters.append(m.group());
                } while (m.find());
                return "输入的字符串： "+ arg +" 包含特殊字符：" + specialCharacters.toString()+ " ,请修改！";
            }
            if(length != 0){
                if(arg.length() > length){
                    return "长度不能超过" + length + "字符！";
                }
            }
        }
        return null;
    }
}
