package common.color;

import java.util.regex.Pattern;

import common.util.Util;

public enum TextColor {
	NULL(0x00),
	RESET(0x01),
	BROWN(0x02, 0x8c5606),
	DBROWN(0x03, 0x56250b),
	DGREEN(0x04, 0x007f00),
	DRED(0x05, 0x7f0000),
	DMAGENTA(0x06, 0x6f006f),
	DVIOLET(0x07, 0x5400be),
	ORK(0x08, 0x487c00),
	ACID(0x09, 0x80ff00),
	NEWLINE(0x0a),
	COL1(0x0b, 0x000000),
	COL2(0x0c, 0x000000),
	COL3(0x0d, 0x000000),
	COL4(0x0e, 0x000000),
	COL5(0x0f, 0x000000),
	BLACK(0x10, 0x000000),
	DGRAY(0x11, 0x585858),
	GRAY(0x12, 0x808080),
	LGRAY(0x13, 0xc0c0c0),
	WHITE(0x14, 0xffffff),
	RED(0x15, 0xcf0000),
	GREEN(0x16, 0x00cf00),
	BLUE(0x17, 0x0000cf),
	YELLOW(0x18, 0xbfbf00),
	MAGENTA(0x19, 0xbf00bf),
	CYAN(0x1a, 0x00bfbf),
	VIOLET(0x1b, 0x6000ff),
	ORANGE(0x1c, 0xff7000),
	CRIMSON(0x1d, 0x601010),
	MIDNIGHT(0x1e, 0x000080),
	NEON(0x1f, 0x80c0f0),
	SPACE(0x20),
	UNKNOWN(0x7f),
	SGA_A(0x80),
	SGA_Z(0x99),
	BUG(0x9a),
	INVALID(0x9b),
	DEMON(0x9c),
	IMP(0x9d),
	HORNS(0x9e),
	BLKHEART(0x9f);
	
	private static final Pattern STRIP_PATTERN = Pattern.compile("[\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000b\u000c\r\u000e\u000f"
			+ "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f]");

	private static final int[] COLORS = new int[32];
	private static final int[] SHADOW = new int[32];
	
	private final String format;
	public final char code;
	public final int color;
	public final int shadow;
	
	public static String stripCodes(String text) {
		return text == null ? null : STRIP_PATTERN.matcher(text).replaceAll("");
	}
	
	public static int getColor(char code) {
		return code < COLORS.length ? COLORS[code] : 0x000000;
	}
	
	public static int getShadow(char code) {
		return code < SHADOW.length ? SHADOW[code] : 0x000000;
	}

	private TextColor(int code) {
		this(code, 0x000000);
	}

	private TextColor(int code, int color) {
		this.format = Character.toString(this.code = (char)code);
		this.color = color;
		this.shadow = Util.mixColor(Util.mixColor(this.color, 0x000000), 0x000000);
	}

	public String toString() {
		return this.format;
	}
	
	static {
		for(TextColor color : values()) {
			if(color.code < COLORS.length) {
				COLORS[color.code] = color.color;
				SHADOW[color.code] = color.shadow;
			}
		}
	}
}
