package io.github.lonamiwebs.stringlate.classes.locales;

import android.support.annotation.NonNull;

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

public class LocaleString {

    final private String mLocale;
    final private String mLocaleDisplay;

    // Support a locale with any separator (i.e. "zh", "zh-TW", "zh_rTW", "zh/TW")
    private final static Pattern SANITIZE_PATTERN = Pattern.compile(
            "^(\\w{2})(?:[ -_/]r?(\\w{2}))?$", Pattern.CASE_INSENSITIVE);

    public LocaleString(@NonNull String locale) {
        mLocale = locale;
        mLocaleDisplay = getDisplay(locale);
    }

    public String getCode() {
        return mLocale;
    }

    @Override
    public String toString() {
        return mLocaleDisplay;
    }

    public static String getDisplay(@NonNull String locale) {
        // If there is an hyphen, then a country was also specified
        // Not sure why but the 'r' prefix is needed for the country
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName();
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty()) // Ensure that it's empty (issue #58)
                        return l.getDisplayName();
        }
        return locale;
    }

    // Useful when exporting to issue for example
    public static String getEnglishDisplay(@NonNull String locale) {
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName(Locale.ENGLISH);
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty())
                        return l.getDisplayName(Locale.ENGLISH);
        }
        return locale;
    }

    public static String getFullCode(@NonNull Locale locale) {
        String language = locale.getLanguage();
        String country = locale.getCountry();
        return country.isEmpty() ? language : language + "-r" + country;
    }

    public static boolean isValid(@NonNull String locale) {
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return true;
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    return true;
        }
        return false;
    }

    @NonNull
    public static String sanitizeLocale(String locale) {
        locale = locale.trim();
        Matcher m = SANITIZE_PATTERN.matcher(locale);
        if (m.find()) {
            String lang = m.group(1).toLowerCase();
            String country = m.group(2);
            if (country != null)
                return String.format("%s-r%s", lang, country.toUpperCase());
            else
                return lang;
        }
        return locale;
    }
}

@@ -120,16 +120,59 @@
 Locale;%0A
+import java.util.MissingResourceException;%0A
 import j
@@ -2646,179 +2646,121 @@
    f
-or (Locale l : Locale.getAvailableLocales())%0A                if (!l.getCountry().isEmpty())%0A                    if (locale.equals(l.getLanguage() + %22-r%22 + l.getCountry(
+inal String%5B%5D parts = locale.split(%22-%22);%0A            return isValid(new Locale(parts%5B0%5D, parts%5B1%5D.substring(1
 )))
+;
 %0A   
@@ -2760,37 +2760,35 @@
 ));%0A        
-     
+%7D%0A%0A
         
    return tr
@@ -2779,98 +2779,108 @@
     
-   
 return 
-true;%0A        %7D else %7B
+isValid(new Locale(locale));
 %0A    
+%7D%0A%0A
     
-    for (Locale l : Locale.getAvailableL
+private static boolean isValid(final Locale l
 ocale
-s())
+) %7B
 %0A   
@@ -2888,95 +2888,159 @@
     
-        if (locale.equals(l.getLanguage()))%0A                    return true;%0A    
+try %7B%0A            return locale.getISO3Language() != null && locale.getISO3Country() != null;%0A        %7D catch (MissingResourceException ignored) %7B%0A
     
-%7D%0A
     
@@ -3057,16 +3057,26 @@
  false;%0A
+        %7D%0A
     %7D%0A%0A 

Enhance LocaleString.isValid()