package com.cloudcc.core;

import java.util.Date;
import java.util.Random;

public class StringUtils {

    public static boolean isNull(Object s) {
        return s == null;
    }

    public static boolean isNullOrEmpty(Object s) {
        return s == null || s.equals("");
    }

    public static boolean isNotNullAndEmpty(Object s) {
        return s != null && !s.equals("");
    }

    public static Object convertNullToEmpty(Object obj) {
        return obj == null ? "" : obj;
    }

    /**
     * Get the object prefix of the record ID (first 3 characters).
     */
    public static String getObjectPrefix(String recordId) {
        if (recordId != null && recordId.length() >= 3) {
            return recordId.substring(0, 3);
        }
        return recordId;
    }

    /**
     * Compare whether two values are equal (supports String and Date).
     * null and "" are considered equal.
     */
    public static boolean assertEqual(Object obj1, Object obj2) {
        if (isNotNullAndEmpty(obj1) && isNullOrEmpty(obj2)) return false;
        if (isNotNullAndEmpty(obj2) && isNullOrEmpty(obj1)) return false;
        if (isNullOrEmpty(obj1) && isNullOrEmpty(obj2)) return true;
        if (obj1 instanceof String) return obj1.equals(obj2);
        if (obj1 instanceof Date) return ((Date) obj1).getTime() == ((Date) obj2).getTime();
        return false;
    }

    /**
     * Compare whether two values are equal (strictly distinguishes null and "").
     */
    public static boolean assertEqualNew(Object obj1, Object obj2) {
        if (isNull(obj1) && isNull(obj2)) return true;
        if (isNull(obj1) && !isNull(obj2) && "".equals(obj2)) return false;
        if (isNull(obj2) && !isNull(obj1) && "".equals(obj1)) return false;
        if (isNotNullAndEmpty(obj1) && isNullOrEmpty(obj2)) return false;
        if (isNotNullAndEmpty(obj2) && isNullOrEmpty(obj1)) return false;
        if (isNullOrEmpty(obj1) && isNullOrEmpty(obj2)) return true;
        if (obj1 instanceof String) return obj1.equals(obj2);
        if (obj1 instanceof Date) return ((Date) obj1).getTime() == ((Date) obj2).getTime();
        return false;
    }

    /**
     * Simple escape for HTML special characters to prevent XSS (only escape &lt;).
     */
    public static String escapeHtml(Object o) {
        if (o != null && !"".equals(o)) {
            return o.toString().replaceAll("<", "&lt;");
        }
        return "";
    }

    /**
     * Get the actual byte length of a string, where Chinese characters and other wide characters are counted as 3, and ASCII is counted as 1.
     */
    public static int getLength(String s) {
        if (s == null || "".equals(s)) return 0;
        char[] c = s.toCharArray();
        int len = 0;
        for (char ch : c) {
            len++;
            if ((ch / 0x80) != 0) len += 2;
        }
        return len;
    }

    /**
     * Generate a random string of specified length containing uppercase letters and digits.
     */
    public static String getCharAndNumber(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        String charStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int charLength = charStr.length();
        for (int i = 0; i < length; i++) {
            sb.append(charStr.charAt(random.nextInt(charLength)));
        }
        return sb.toString();
    }
}
