package com.github.mjdev.libaums.fs.fat32;

import com.seewo.vcommons.constants.SeewoConstants;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
class ShortNameGenerator {
    private static boolean isValidChar(char c) {
        if (c < '0' || c > '9') {
            return (c >= 'A' && c <= 'Z') || c == '$' || c == '%' || c == '\'' || c == '-' || c == '_' || c == '@' || c == '~' || c == '`' || c == '!' || c == '(' || c == ')' || c == '{' || c == '}' || c == '^' || c == '#' || c == '&';
        }
        return true;
    }

    ShortNameGenerator() {
    }

    private static boolean containsInvalidChars(String str) {
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if (!isValidChar(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private static String replaceInvalidChars(String str) {
        int length = str.length();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char cCharAt = str.charAt(i);
            if (isValidChar(cCharAt)) {
                sb.append(cCharAt);
            } else {
                sb.append("_");
            }
        }
        return sb.toString();
    }

    static String getNextHexPart(String str, int i) {
        String hexString = Long.toHexString(Long.parseLong(str, 16) + 1);
        if (hexString.length() > i) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 < i - hexString.length(); i2++) {
            sb.append("0");
        }
        return sb.toString() + hexString;
    }

    static ShortName generateShortName(String str, Collection<ShortName> collection) {
        String strTrim = str.toUpperCase(Locale.ROOT).trim();
        int i = 0;
        int i2 = 0;
        while (i2 < strTrim.length() && strTrim.charAt(i2) == '.') {
            i2++;
        }
        String strReplaceInvalidChars = "";
        String strReplace = strTrim.substring(i2).replace(" ", "");
        int iLastIndexOf = strReplace.lastIndexOf(".");
        if (iLastIndexOf != -1) {
            String strSubstring = strReplace.substring(0, iLastIndexOf);
            String strSubstring2 = strReplace.substring(iLastIndexOf + 1);
            if (strSubstring2.length() > 3) {
                strSubstring2 = strSubstring2.substring(0, 3);
            }
            strReplaceInvalidChars = strSubstring2;
            strReplace = strSubstring;
        }
        if (containsInvalidChars(strReplace)) {
            strReplace = replaceInvalidChars(strReplace);
        }
        if (containsInvalidChars(strReplaceInvalidChars)) {
            strReplaceInvalidChars = replaceInvalidChars(strReplaceInvalidChars);
        }
        if (strReplace.length() == 0) {
            strReplace = "__";
        } else if (strReplace.length() == 1) {
            strReplace = strReplace + "_";
        } else if (strReplace.length() != 2 && strReplace.length() > 2) {
            strReplace = strReplace.substring(0, 2);
        }
        if (strReplaceInvalidChars.length() == 0) {
            strReplaceInvalidChars = "000";
        } else if (strReplaceInvalidChars.length() == 1) {
            strReplaceInvalidChars = strReplaceInvalidChars + SeewoConstants.KEY_DEFAULT_VALUE;
        } else if (strReplaceInvalidChars.length() == 2) {
            strReplaceInvalidChars = strReplaceInvalidChars + "0";
        }
        ShortName shortName = new ShortName(strReplace + "0000~0", strReplaceInvalidChars);
        String nextHexPart = "0000";
        while (containShortName(collection, shortName)) {
            if (getNextHexPart(nextHexPart, 4) == null) {
                i++;
                if (i >= 10) {
                    break;
                }
                nextHexPart = "0000";
            } else {
                nextHexPart = getNextHexPart(nextHexPart, 4);
            }
            shortName = new ShortName(strReplace + nextHexPart + "~" + i, strReplaceInvalidChars);
        }
        return shortName;
    }

    public static boolean containShortName(Collection<ShortName> collection, ShortName shortName) {
        Iterator<ShortName> it = collection.iterator();
        while (it.hasNext()) {
            if (it.next().getString().equalsIgnoreCase(shortName.getString())) {
                return true;
            }
        }
        return false;
    }
}
