package common.color;

import java.util.Map;

import common.collect.Maps;
import common.util.Identifyable;

public enum DyeColor implements Identifyable
{
    WHITE(0, 15, "white", "Weiß", "Weißes", "Weißer", "Weiße", "Knochenmehl", 16777215, TextColor.WHITE),
    ORANGE(1, 14, "orange", "Orange", "Oranges", "Oranger", "Orange", "Oranger Farbstoff", 14188339, TextColor.ORANGE),
    MAGENTA(2, 13, "magenta", "Magenta", "Magenta", "Magenta", "Magenta", "Magenta Farbstoff", 11685080, TextColor.NEON),
    LIGHT_BLUE(3, 12, "light_blue", "Hellblau", "Hellblaues", "Hellblauer", "Hellblaue", "Hellblauer Farbstoff", 6724056, TextColor.BLUE),
    YELLOW(4, 11, "yellow", "Gelb", "Gelbes", "Gelber", "Gelbe", "Gelber Farbstoff", 15066419, TextColor.YELLOW),
    LIME(5, 10, "lime", "Hellgrün", "Hellgrünes", "Hellgrüner", "Hellgrüne", "Hellgrüner Farbstoff", 8375321, TextColor.GREEN),
    PINK(6, 9, "pink", "Rosa", "Rosa", "Rosa", "Rosa", "Rosa Farbstoff", 15892389, TextColor.MAGENTA),
    GRAY(7, 8, "gray", "Grau", "Graues", "Grauer", "Graue", "Grauer Farbstoff", 5000268, TextColor.GRAY),
    SILVER(8, 7, "silver", "Hellgrau", "Hellgraues", "Hellgrauer", "Hellgraue", "Hellgrauer Farbstoff", 10066329, TextColor.LGRAY),
    CYAN(9, 6, "cyan", "Türkis", "Türkises", "Türkiser", "Türkise", "Türkiser Farbstoff", 5013401, TextColor.CYAN),
    PURPLE(10, 5, "purple", "Violett", "Violettes", "Violetter", "Violette", "Violetter Farbstoff", 8339378, TextColor.DMAGENTA),
    BLUE(11, 4, "blue", "Blau", "Blaues", "Blauer", "Blaue", "Lapislazuli", 3361970, TextColor.MIDNIGHT),
    BROWN(12, 3, "brown", "Braun", "Braunes", "Brauner", "Braune", "Kakaobohnen", 6704179, TextColor.ORANGE),
    GREEN(13, 2, "green", "Grün", "Grünes", "Grüner", "Grüne", "Kaktusgrün", 6717235, TextColor.DGREEN),
    RED(14, 1, "red", "Rot", "Rotes", "Roter", "Rote", "Roter Farbstoff", 10040115, TextColor.DRED),
    BLACK(15, 0, "black", "Schwarz", "Schwarzes", "Schwarzer", "Schwarze", "Tintenbeutel", 1644825, TextColor.BLACK);

	private static final Map<String, DyeColor> LOOKUP = Maps.newHashMap();
    private static final DyeColor[] META_LOOKUP = new DyeColor[values().length];
    private static final DyeColor[] DYE_DMG_LOOKUP = new DyeColor[values().length];
    
    private final int meta;
    private final int dyeDamage;
    private final String name;
    private final String display;
    private final String subject;
    private final String msubject;
    private final String fsubject;
    private final String dye;
    private final int color;
    private final TextColor chatColor;

    private DyeColor(int meta, int dyeDamage, String name, String display, String subject, String msubject, 
    		String fsubject, String dye, int colorIn, TextColor chatColor)
    {
        this.meta = meta;
        this.dyeDamage = dyeDamage;
        this.name = name;
        this.display = display;
        this.subject = subject;
        this.msubject = msubject;
        this.fsubject = fsubject;
        this.dye = dye;
        this.color = colorIn;
        this.chatColor = chatColor;
    }

    public int getMetadata()
    {
        return this.meta;
    }

    public int getDyeDamage()
    {
        return this.dyeDamage;
    }

    public int getColor()
    {
        return this.color;
    }

    public static DyeColor byDyeDamage(int damage)
    {
        if (damage < 0 || damage >= DYE_DMG_LOOKUP.length)
        {
            damage = 0;
        }

        return DYE_DMG_LOOKUP[damage];
    }

    public static DyeColor byMetadata(int meta)
    {
        if (meta < 0 || meta >= META_LOOKUP.length)
        {
            meta = 0;
        }

        return META_LOOKUP[meta];
    }
	
	public static DyeColor getByName(String name) {
		DyeColor type = LOOKUP.get(name.toLowerCase());
		return type == null ? WHITE : type;
	}
	
	public static DyeColor getByString(String name) {
		DyeColor type = LOOKUP.get(name.toLowerCase());
		if(type != null)
			return type;
		int meta;
		try {
			meta = Integer.parseInt(name);
		}
		catch(NumberFormatException e) {
			return null;
		}
		if(meta < 0 || meta >= META_LOOKUP.length)
			return null;
		return META_LOOKUP[meta];
	}

    public String toString()
    {
        return this.name;
    }

    public String getName()
    {
        return this.name;
    }

    public String getDisplay()
    {
        return this.display;
    }

    public String getSubject(Integer type)
    {
        return type == null ? this.display : (type < 0 ? this.fsubject : (type > 0 ? this.msubject : this.subject));
    }

    public String getDye()
    {
        return this.dye;
    }

    static {
        for (DyeColor color : values())
        {
			LOOKUP.put(color.name, color);
            META_LOOKUP[color.getMetadata()] = color;
            DYE_DMG_LOOKUP[color.getDyeDamage()] = color;
        }
    }
}
