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

package org.jline.utils;

import java.util.ArrayList;
import java.util.List;
import org.jline.terminal.Terminal;

public abstract class AttributedCharSequence implements CharSequence
{
    public static final int TRUE_COLORS = 16777216;
    private static final int HIGH_COLORS = 32767;
    static final boolean DISABLE_ALTERNATE_CHARSET;
    
    public void print(final Terminal terminal) {
        terminal.writer().print(this.toAnsi(terminal));
    }
    
    public void println(final Terminal terminal) {
        terminal.writer().println(this.toAnsi(terminal));
    }
    
    public String toAnsi() {
        return this.toAnsi(null);
    }
    
    public String toAnsi(final Terminal terminal) {
        if (terminal != null && "dumb".equals(terminal.getType())) {
            return this.toString();
        }
        int colors = 256;
        ForceMode forceMode = ForceMode.None;
        ColorPalette palette = null;
        String alternateIn = null;
        String alternateOut = null;
        if (terminal != null) {
            final Integer max_colors = terminal.getNumericCapability(InfoCmp.Capability.max_colors);
            if (max_colors != null) {
                colors = max_colors;
            }
            if ("windows-256color".equals(terminal.getType()) || "windows-conemu".equals(terminal.getType())) {
                forceMode = ForceMode.Force256Colors;
            }
            palette = terminal.getPalette();
            if (!AttributedCharSequence.DISABLE_ALTERNATE_CHARSET) {
                alternateIn = Curses.tputs(terminal.getStringCapability(InfoCmp.Capability.enter_alt_charset_mode), new Object[0]);
                alternateOut = Curses.tputs(terminal.getStringCapability(InfoCmp.Capability.exit_alt_charset_mode), new Object[0]);
            }
        }
        return this.toAnsi(colors, forceMode, palette, alternateIn, alternateOut);
    }
    
    @Deprecated
    public String toAnsi(final int colors, final boolean force256colors) {
        return this.toAnsi(colors, force256colors, null, null);
    }
    
    @Deprecated
    public String toAnsi(final int colors, final boolean force256colors, final String altIn, final String altOut) {
        return this.toAnsi(colors, force256colors ? ForceMode.Force256Colors : ForceMode.None, null, altIn, altOut);
    }
    
    public String toAnsi(final int colors, final ForceMode force) {
        return this.toAnsi(colors, force, null, null, null);
    }
    
    public String toAnsi(final int colors, final ForceMode force, final ColorPalette palette) {
        return this.toAnsi(colors, force, palette, null, null);
    }
    
    public String toAnsi(final int colors, final ForceMode force, ColorPalette palette, final String altIn, final String altOut) {
        final StringBuilder sb = new StringBuilder();
        long style = 0L;
        long foreground = 0L;
        long background = 0L;
        boolean alt = false;
        if (palette == null) {
            palette = ColorPalette.DEFAULT;
        }
        for (int i = 0; i < this.length(); ++i) {
            char c = this.charAt(i);
            if (altIn != null && altOut != null) {
                final char pc = c;
                switch (c) {
                    case '\u2518': {
                        c = 'j';
                        break;
                    }
                    case '\u2510': {
                        c = 'k';
                        break;
                    }
                    case '\u250c': {
                        c = 'l';
                        break;
                    }
                    case '\u2514': {
                        c = 'm';
                        break;
                    }
                    case '\u253c': {
                        c = 'n';
                        break;
                    }
                    case '\u2500': {
                        c = 'q';
                        break;
                    }
                    case '\u251c': {
                        c = 't';
                        break;
                    }
                    case '\u2524': {
                        c = 'u';
                        break;
                    }
                    case '\u2534': {
                        c = 'v';
                        break;
                    }
                    case '\u252c': {
                        c = 'w';
                        break;
                    }
                    case '\u2502': {
                        c = 'x';
                        break;
                    }
                }
                final boolean oldalt = alt;
                alt = (c != pc);
                if (oldalt ^ alt) {
                    sb.append(alt ? altIn : altOut);
                }
            }
            final long s = this.styleCodeAt(i) & 0xFFFFFFFFFFFFEFFFL;
            if (style != s) {
                long d = (style ^ s) & 0x1FFFL;
                final long fg = ((s & 0x300L) != 0x0L) ? (s & 0x7FFFFF8300L) : 0L;
                final long bg = ((s & 0xC00L) != 0x0L) ? (s & 0x7FFFFF8000000C00L) : 0L;
                if (s == 0L) {
                    sb.append("\u001b[0m");
                    background = (foreground = 0L);
                }
                else {
                    sb.append("\u001b[");
                    boolean first = true;
                    if ((d & 0x4L) != 0x0L) {
                        first = attr(sb, ((s & 0x4L) != 0x0L) ? "3" : "23", first);
                    }
                    if ((d & 0x8L) != 0x0L) {
                        first = attr(sb, ((s & 0x8L) != 0x0L) ? "4" : "24", first);
                    }
                    if ((d & 0x10L) != 0x0L) {
                        first = attr(sb, ((s & 0x10L) != 0x0L) ? "5" : "25", first);
                    }
                    if ((d & 0x20L) != 0x0L) {
                        first = attr(sb, ((s & 0x20L) != 0x0L) ? "7" : "27", first);
                    }
                    if ((d & 0x40L) != 0x0L) {
                        first = attr(sb, ((s & 0x40L) != 0x0L) ? "8" : "28", first);
                    }
                    if ((d & 0x80L) != 0x0L) {
                        first = attr(sb, ((s & 0x80L) != 0x0L) ? "9" : "29", first);
                    }
                    if (foreground != fg) {
                        if (fg > 0L) {
                            int rounded = -1;
                            if ((fg & 0x200L) != 0x0L) {
                                final int r = (int)(fg >> 31) & 0xFF;
                                final int g = (int)(fg >> 23) & 0xFF;
                                final int b = (int)(fg >> 15) & 0xFF;
                                if (colors >= 32767) {
                                    first = attr(sb, "38;2;" + r + ";" + g + ";" + b, first);
                                }
                                else {
                                    rounded = palette.round(r, g, b);
                                }
                            }
                            else if ((fg & 0x100L) != 0x0L) {
                                rounded = palette.round((int)(fg >> 15) & 0xFF);
                            }
                            if (rounded >= 0) {
                                if (colors >= 32767 && force == ForceMode.ForceTrueColors) {
                                    final int col = palette.getColor(rounded);
                                    final int r2 = col >> 16 & 0xFF;
                                    final int g2 = col >> 8 & 0xFF;
                                    final int b2 = col & 0xFF;
                                    first = attr(sb, "38;2;" + r2 + ";" + g2 + ";" + b2, first);
                                }
                                else if (force == ForceMode.Force256Colors || rounded >= 16) {
                                    first = attr(sb, "38;5;" + rounded, first);
                                }
                                else if (rounded >= 8) {
                                    first = attr(sb, "9" + (rounded - 8), first);
                                    d |= (s & 0x1L);
                                }
                                else {
                                    first = attr(sb, "3" + rounded, first);
                                    d |= (s & 0x1L);
                                }
                            }
                        }
                        else {
                            first = attr(sb, "39", first);
                        }
                        foreground = fg;
                    }
                    if (background != bg) {
                        if (bg > 0L) {
                            int rounded = -1;
                            if ((bg & 0x800L) != 0x0L) {
                                final int r = (int)(bg >> 55) & 0xFF;
                                final int g = (int)(bg >> 47) & 0xFF;
                                final int b = (int)(bg >> 39) & 0xFF;
                                if (colors >= 32767) {
                                    first = attr(sb, "48;2;" + r + ";" + g + ";" + b, first);
                                }
                                else {
                                    rounded = palette.round(r, g, b);
                                }
                            }
                            else if ((bg & 0x400L) != 0x0L) {
                                rounded = palette.round((int)(bg >> 39) & 0xFF);
                            }
                            if (rounded >= 0) {
                                if (colors >= 32767 && force == ForceMode.ForceTrueColors) {
                                    final int col = palette.getColor(rounded);
                                    final int r2 = col >> 16 & 0xFF;
                                    final int g2 = col >> 8 & 0xFF;
                                    final int b2 = col & 0xFF;
                                    first = attr(sb, "48;2;" + r2 + ";" + g2 + ";" + b2, first);
                                }
                                else if (force == ForceMode.Force256Colors || rounded >= 16) {
                                    first = attr(sb, "48;5;" + rounded, first);
                                }
                                else if (rounded >= 8) {
                                    first = attr(sb, "10" + (rounded - 8), first);
                                }
                                else {
                                    first = attr(sb, "4" + rounded, first);
                                }
                            }
                        }
                        else {
                            first = attr(sb, "49", first);
                        }
                        background = bg;
                    }
                    if ((d & 0x3L) != 0x0L) {
                        if (((d & 0x1L) != 0x0L && (s & 0x1L) == 0x0L) || ((d & 0x2L) != 0x0L && (s & 0x2L) == 0x0L)) {
                            first = attr(sb, "22", first);
                        }
                        if ((d & 0x1L) != 0x0L && (s & 0x1L) != 0x0L) {
                            first = attr(sb, "1", first);
                        }
                        if ((d & 0x2L) != 0x0L && (s & 0x2L) != 0x0L) {
                            first = attr(sb, "2", first);
                        }
                    }
                    sb.append("m");
                }
                style = s;
            }
            sb.append(c);
        }
        if (alt) {
            sb.append(altOut);
        }
        if (style != 0L) {
            sb.append("\u001b[0m");
        }
        return sb.toString();
    }
    
    @Deprecated
    public static int rgbColor(final int col) {
        return Colors.rgbColor(col);
    }
    
    @Deprecated
    public static int roundColor(final int col, final int max) {
        return Colors.roundColor(col, max);
    }
    
    @Deprecated
    public static int roundRgbColor(final int r, final int g, final int b, final int max) {
        return Colors.roundRgbColor(r, g, b, max);
    }
    
    private static boolean attr(final StringBuilder sb, final String s, final boolean first) {
        if (!first) {
            sb.append(";");
        }
        sb.append(s);
        return false;
    }
    
    public abstract AttributedStyle styleAt(final int p0);
    
    long styleCodeAt(final int index) {
        return this.styleAt(index).getStyle();
    }
    
    public boolean isHidden(final int index) {
        return (this.styleCodeAt(index) & 0x1000L) != 0x0L;
    }
    
    public int runStart(int index) {
        for (AttributedStyle style = this.styleAt(index); index > 0 && this.styleAt(index - 1).equals(style); --index) {}
        return index;
    }
    
    public int runLimit(int index) {
        for (AttributedStyle style = this.styleAt(index); index < this.length() - 1 && this.styleAt(index + 1).equals(style); ++index) {}
        return index + 1;
    }
    
    @Override
    public abstract AttributedString subSequence(final int p0, final int p1);
    
    public AttributedString substring(final int start, final int end) {
        return this.subSequence(start, end);
    }
    
    protected abstract char[] buffer();
    
    protected abstract int offset();
    
    @Override
    public char charAt(final int index) {
        return this.buffer()[this.offset() + index];
    }
    
    public int codePointAt(final int index) {
        return Character.codePointAt(this.buffer(), index + this.offset());
    }
    
    public boolean contains(final char c) {
        for (int i = 0; i < this.length(); ++i) {
            if (this.charAt(i) == c) {
                return true;
            }
        }
        return false;
    }
    
    public int codePointBefore(final int index) {
        return Character.codePointBefore(this.buffer(), index + this.offset());
    }
    
    public int codePointCount(final int index, final int length) {
        return Character.codePointCount(this.buffer(), index + this.offset(), length);
    }
    
    public int columnLength() {
        int cols = 0;
        int cp;
        for (int len = this.length(), cur = 0; cur < len; cur += Character.charCount(cp)) {
            cp = this.codePointAt(cur);
            if (!this.isHidden(cur)) {
                cols += WCWidth.wcwidth(cp);
            }
        }
        return cols;
    }
    
    public AttributedString columnSubSequence(final int start, final int stop) {
        int begin;
        int col;
        int cp;
        int w;
        for (begin = 0, col = 0; begin < this.length(); begin += Character.charCount(cp), col += w) {
            cp = this.codePointAt(begin);
            w = (this.isHidden(begin) ? 0 : WCWidth.wcwidth(cp));
            if (col + w > start) {
                break;
            }
        }
        int end;
        int cp2;
        int w2;
        for (end = begin; end < this.length(); end += Character.charCount(cp2), col += w2) {
            cp2 = this.codePointAt(end);
            if (cp2 == 10) {
                break;
            }
            w2 = (this.isHidden(end) ? 0 : WCWidth.wcwidth(cp2));
            if (col + w2 > stop) {
                break;
            }
        }
        return this.subSequence(begin, end);
    }
    
    public List<AttributedString> columnSplitLength(final int columns) {
        return this.columnSplitLength(columns, false, true);
    }
    
    public List<AttributedString> columnSplitLength(final int columns, final boolean includeNewlines, final boolean delayLineWrap) {
        final List<AttributedString> strings = new ArrayList<AttributedString>();
        int beg;
        int cur = beg = 0;
        int col = 0;
        while (cur < this.length()) {
            final int cp = this.codePointAt(cur);
            final int w = this.isHidden(cur) ? 0 : WCWidth.wcwidth(cp);
            if (cp == 10) {
                strings.add(this.subSequence(beg, includeNewlines ? (cur + 1) : cur));
                beg = cur + 1;
                col = 0;
            }
            else if ((col += w) > columns) {
                strings.add(this.subSequence(beg, cur));
                beg = cur;
                col = w;
            }
            cur += Character.charCount(cp);
        }
        strings.add(this.subSequence(beg, cur));
        return strings;
    }
    
    @Override
    public String toString() {
        return new String(this.buffer(), this.offset(), this.length());
    }
    
    public AttributedString toAttributedString() {
        return this.substring(0, this.length());
    }
    
    static {
        DISABLE_ALTERNATE_CHARSET = Boolean.getBoolean("org.jline.utils.disableAlternateCharset");
    }
    
    public enum ForceMode
    {
        None, 
        Force256Colors, 
        ForceTrueColors;
    }
}
