package com.tzld.piaoquan.growth.common.utils;

import org.apache.http.util.TextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseUtils {

    public static void copyProperties(Object orig, Object dest) {
        // spring的BeanUtils性能较好
        BeanUtils.copyProperties(orig, dest);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static String getUUIDStr() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    public static String getShortUUIDStr() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public static String getShortUUIDStr_6() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        System.out.println("uuid = " + uuid.length() + " , chars = " + chars.length);
        for (int i = 0; i < 6; i++) {
            String str = uuid.substring(i * 5, i * 5 + 5);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    /**
     * 带时间戳的uuid
     *
     * @return
     */
    public static String getTimestampUUIDStr() {
        return UUID.randomUUID().toString().replaceAll("-", "") + System.currentTimeMillis();
    }

    public static boolean isUidAvailable(Long uid) {
        return Objects.nonNull(uid) && uid >= 0;
    }

    public static boolean checkEmailAddressAvailable(String email) {
        String check = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }

    public static int getInt(String intValue, int defaultValue) {
        try {
            return Integer.parseInt(intValue);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    public static boolean isContainChinese(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        try {
            Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
            Matcher m = p.matcher(str);
            if (m.find()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String calcRate(Integer a, Integer b) {
        if (a == null || b == null || b == 0) {
            return "0";
        }
        return new BigDecimal(a).divide(new BigDecimal(b), 2, RoundingMode.HALF_UP).toString();
    }

    public static boolean isNumberStr(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    public static String getFirstGroupPlanExeId(String planExeId) {
        if (StringUtils.hasText(planExeId) && planExeId.contains("_")) {
            return planExeId.substring(0, planExeId.indexOf("_"));
        }
        return planExeId;
    }

    public static boolean isFirstGroupPlanExeId(String planExeId) {
        if (StringUtils.hasText(planExeId) && planExeId.contains("_")) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(checkEmailAddressAvailable("a-a_a@q-q.A"));
        System.out.println(getUUIDStr());
        System.out.println(getShortUUIDStr());
        System.out.println(getShortUUIDStr_6());
        System.out.println(isNumberStr("20240201052729691500183"));
        System.out.println(getFirstGroupPlanExeId("20240314125002672235936_2"));
        System.out.println("20240314125002672235936_2".substring("20240314125002672235936_2".indexOf("_") + 1));
    }
}
