// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.builtins;

import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.Arrays;
import org.jline.utils.StyleResolver;
import java.util.regex.Pattern;
import java.util.List;

public class Styles
{
    public static final String NANORC_THEME = "NANORC_THEME";
    protected static final List<String> ANSI_STYLES;
    private static final String DEFAULT_LS_COLORS = "di=1;91:ex=1;92:ln=1;96:fi=";
    private static final String DEFAULT_HELP_COLORS = "ti=1;34:co=1:ar=3:op=33:de=";
    private static final String DEFAULT_PRNT_COLORS = "th=1;34:rn=1;34:rs=,~grey15:mk=1;34:em=31:vs=32";
    private static final String LS_COLORS = "LS_COLORS";
    private static final String HELP_COLORS = "HELP_COLORS";
    private static final String PRNT_COLORS = "PRNT_COLORS";
    private static final String KEY = "([a-z]{2}|\\*\\.[a-zA-Z0-9]+)";
    private static final String VALUE = "(([!~#]?[a-zA-Z0-9]+[a-z0-9-;]*)?|[A-Z_]+)";
    private static final String VALUES = "(([!~#]?[a-zA-Z0-9]+[a-z0-9-;]*)?|[A-Z_]+)(,(([!~#]?[a-zA-Z0-9]+[a-z0-9-;]*)?|[A-Z_]+))*";
    private static final Pattern STYLE_ELEMENT_SEPARATOR;
    private static final Pattern STYLE_ELEMENT_PATTERN;
    
    public static StyleResolver lsStyle() {
        return style("LS_COLORS", "di=1;91:ex=1;92:ln=1;96:fi=");
    }
    
    public static StyleResolver helpStyle() {
        return style("HELP_COLORS", "ti=1;34:co=1:ar=3:op=33:de=");
    }
    
    public static StyleResolver prntStyle() {
        return style("PRNT_COLORS", "th=1;34:rn=1;34:rs=,~grey15:mk=1;34:em=31:vs=32");
    }
    
    public static boolean isStylePattern(final String style) {
        final String[] styleElements = Styles.STYLE_ELEMENT_SEPARATOR.split(style);
        return Arrays.stream(styleElements).allMatch(element -> element.isEmpty() || Styles.STYLE_ELEMENT_PATTERN.matcher(element).matches());
    }
    
    public static StyleResolver style(final String name, final String defStyle) {
        String style = consoleOption(name);
        if (style == null) {
            style = defStyle;
        }
        return style(style);
    }
    
    private static ConsoleOptionGetter optionGetter() {
        try {
            return (ConsoleOptionGetter)Class.forName("org.jline.console.SystemRegistry").getDeclaredMethod("get", (Class<?>[])new Class[0]).invoke(null, new Object[0]);
        }
        catch (final Exception ex) {
            return null;
        }
    }
    
    private static <T> T consoleOption(final String name, final T defVal) {
        T out = defVal;
        final ConsoleOptionGetter cog = optionGetter();
        if (cog != null) {
            out = cog.consoleOption(name, defVal);
        }
        return out;
    }
    
    private static String consoleOption(final String name) {
        String out = null;
        final ConsoleOptionGetter cog = optionGetter();
        if (cog != null) {
            out = (String)cog.consoleOption(name);
            if (out != null && !isStylePattern(out)) {
                out = null;
            }
        }
        if (out == null) {
            out = System.getenv(name);
            if (out != null && !isStylePattern(out)) {
                out = null;
            }
        }
        return out;
    }
    
    public static StyleResolver style(final String style) {
        final Map<String, String> colors = Arrays.stream(style.split(":")).collect(Collectors.toMap(s -> s.substring(0, s.indexOf(61)), s -> s.substring(s.indexOf(61) + 1)));
        return new StyleResolver(new StyleCompiler(colors)::getStyle);
    }
    
    static {
        ANSI_STYLES = Arrays.asList("blink", "bold", "conceal", "crossed-out", "crossedout", "faint", "hidden", "inverse", "inverse-neg", "inverseneg", "italic", "underline");
        STYLE_ELEMENT_SEPARATOR = Pattern.compile(":");
        STYLE_ELEMENT_PATTERN = Pattern.compile("([a-z]{2}|\\*\\.[a-zA-Z0-9]+)=(([!~#]?[a-zA-Z0-9]+[a-z0-9-;]*)?|[A-Z_]+)(,(([!~#]?[a-zA-Z0-9]+[a-z0-9-;]*)?|[A-Z_]+))*");
    }
    
    public static class StyleCompiler
    {
        private static final String ANSI_VALUE = "[0-9]*(;[0-9]+){0,2}";
        private static final String COLORS_24BIT = "[#x][0-9a-fA-F]{6}";
        private static final List<String> COLORS_8;
        private static final Map<String, Integer> COLORS_NANO;
        private final Map<String, String> colors;
        private final Map<String, String> tokenColors;
        private final boolean nanoStyle;
        
        public StyleCompiler(final Map<String, String> colors) {
            this(colors, false);
        }
        
        public StyleCompiler(final Map<String, String> colors, final boolean nanoStyle) {
            this.colors = colors;
            this.nanoStyle = nanoStyle;
            this.tokenColors = (Map)consoleOption("NANORC_THEME", new HashMap());
        }
        
        public String getStyle(final String reference) {
            String rawStyle = this.colors.get(reference);
            if (rawStyle == null) {
                return null;
            }
            if (rawStyle.matches("[A-Z_]+")) {
                rawStyle = this.tokenColors.getOrDefault(rawStyle, "normal");
            }
            else if (!this.nanoStyle && rawStyle.matches("[0-9]*(;[0-9]+){0,2}")) {
                return rawStyle;
            }
            final StringBuilder out = new StringBuilder();
            boolean first = true;
            boolean fg = true;
            for (final String s : rawStyle.split(",")) {
                if (s.trim().isEmpty()) {
                    fg = false;
                }
                else {
                    if (!first) {
                        out.append(",");
                    }
                    if (Styles.ANSI_STYLES.contains(s)) {
                        out.append(s);
                    }
                    else if (StyleCompiler.COLORS_8.contains(s) || StyleCompiler.COLORS_NANO.containsKey(s) || s.startsWith("light") || s.startsWith("bright") || s.startsWith("~") || s.startsWith("!") || s.matches("\\d+") || s.matches("[#x][0-9a-fA-F]{6}") || s.equals("normal") || s.equals("default")) {
                        if (s.matches("[#x][0-9a-fA-F]{6}")) {
                            if (fg) {
                                out.append("fg-rgb:");
                            }
                            else {
                                out.append("bg-rgb:");
                            }
                            out.append(s);
                        }
                        else if (s.matches("\\d+") || StyleCompiler.COLORS_NANO.containsKey(s)) {
                            if (fg) {
                                out.append("38;5;");
                            }
                            else {
                                out.append("48;5;");
                            }
                            out.append(s.matches("\\d+") ? s : StyleCompiler.COLORS_NANO.get(s).toString());
                        }
                        else {
                            if (fg) {
                                out.append("fg:");
                            }
                            else {
                                out.append("bg:");
                            }
                            if (StyleCompiler.COLORS_8.contains(s) || s.startsWith("~") || s.startsWith("!") || s.startsWith("bright-")) {
                                out.append(s);
                            }
                            else if (s.startsWith("light")) {
                                out.append("!").append(s.substring(5));
                            }
                            else if (s.startsWith("bright")) {
                                out.append("!").append(s.substring(6));
                            }
                            else {
                                out.append("default");
                            }
                        }
                        fg = false;
                    }
                    first = false;
                }
            }
            return out.toString();
        }
        
        static {
            COLORS_8 = Arrays.asList("white", "black", "red", "blue", "green", "yellow", "magenta", "cyan");
            (COLORS_NANO = new HashMap<String, Integer>()).put("pink", 204);
            StyleCompiler.COLORS_NANO.put("purple", 163);
            StyleCompiler.COLORS_NANO.put("mauve", 134);
            StyleCompiler.COLORS_NANO.put("lagoon", 38);
            StyleCompiler.COLORS_NANO.put("mint", 48);
            StyleCompiler.COLORS_NANO.put("lime", 148);
            StyleCompiler.COLORS_NANO.put("peach", 215);
            StyleCompiler.COLORS_NANO.put("orange", 208);
            StyleCompiler.COLORS_NANO.put("latte", 137);
        }
    }
}
