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

package org.fusesource.jansi.io;

import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.OutputStream;
import org.fusesource.jansi.AnsiColors;

public class ColorsAnsiProcessor extends AnsiProcessor
{
    private final AnsiColors colors;
    
    public ColorsAnsiProcessor(final OutputStream os, final AnsiColors colors) {
        super(os);
        this.colors = colors;
    }
    
    @Override
    protected boolean processEscapeCommand(final ArrayList<Object> options, final int command) throws IOException {
        if (command != 109 || (this.colors != AnsiColors.Colors256 && this.colors != AnsiColors.Colors16)) {
            return false;
        }
        boolean has38or48 = false;
        for (final Object next : options) {
            if (next != null && next.getClass() != Integer.class) {
                throw new IllegalArgumentException();
            }
            final Integer value = (Integer)next;
            has38or48 |= (value == 38 || value == 48);
        }
        if (!has38or48) {
            return false;
        }
        final StringBuilder sb = new StringBuilder(32);
        sb.append('\u001b').append('[');
        boolean first = true;
        final Iterator<Object> optionsIterator = options.iterator();
        while (optionsIterator.hasNext()) {
            final Object next2 = optionsIterator.next();
            if (next2 != null) {
                final int value2 = (int)next2;
                if (value2 == 38 || value2 == 48) {
                    final int arg2or5 = this.getNextOptionInt(optionsIterator);
                    if (arg2or5 == 2) {
                        final int r = this.getNextOptionInt(optionsIterator);
                        final int g = this.getNextOptionInt(optionsIterator);
                        final int b = this.getNextOptionInt(optionsIterator);
                        if (this.colors == AnsiColors.Colors256) {
                            final int col = Colors.roundRgbColor(r, g, b, 256);
                            if (!first) {
                                sb.append(';');
                            }
                            first = false;
                            sb.append(value2);
                            sb.append(';');
                            sb.append(5);
                            sb.append(';');
                            sb.append(col);
                        }
                        else {
                            final int col = Colors.roundRgbColor(r, g, b, 16);
                            if (!first) {
                                sb.append(';');
                            }
                            first = false;
                            sb.append((value2 == 38) ? ((col >= 8) ? (90 + col - 8) : (30 + col)) : ((col >= 8) ? (100 + col - 8) : (40 + col)));
                        }
                    }
                    else {
                        if (arg2or5 != 5) {
                            throw new IllegalArgumentException();
                        }
                        final int paletteIndex = this.getNextOptionInt(optionsIterator);
                        if (this.colors == AnsiColors.Colors256) {
                            if (!first) {
                                sb.append(';');
                            }
                            first = false;
                            sb.append(value2);
                            sb.append(';');
                            sb.append(5);
                            sb.append(';');
                            sb.append(paletteIndex);
                        }
                        else {
                            final int col2 = Colors.roundColor(paletteIndex, 16);
                            if (!first) {
                                sb.append(';');
                            }
                            first = false;
                            sb.append((value2 == 38) ? ((col2 >= 8) ? (90 + col2 - 8) : (30 + col2)) : ((col2 >= 8) ? (100 + col2 - 8) : (40 + col2)));
                        }
                    }
                }
                else {
                    if (!first) {
                        sb.append(';');
                    }
                    first = false;
                    sb.append(value2);
                }
            }
        }
        sb.append('m');
        this.os.write(sb.toString().getBytes());
        return true;
    }
    
    @Override
    protected boolean processOperatingSystemCommand(final ArrayList<Object> options) {
        return false;
    }
    
    @Override
    protected boolean processCharsetSelect(final ArrayList<Object> options) {
        return false;
    }
}
