/*
 * This file is part of AlmuraSDK, licensed under the MIT License (MIT).
 *
 * Copyright (c) AlmuraDev <http://beta.almuramc.com/>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.almuradev.almurasdk.util;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import net.minecraft.util.EnumChatFormatting;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.List;

public final class Colors {

    public static final Color BLACK = new Color("black", EnumChatFormatting.BLACK.getFormattingCode(), 0, 0);
    public static final Color DARK_BLUE = new Color("dark_blue", EnumChatFormatting.DARK_BLUE.getFormattingCode(), 1, 170);
    public static final Color DARK_GREEN = new Color("dark_green", EnumChatFormatting.DARK_GREEN.getFormattingCode(), 2, 43520);
    public static final Color DARK_AQUA = new Color("dark_aqua", EnumChatFormatting.DARK_AQUA.getFormattingCode(), 3, 43690);
    public static final Color DARK_RED = new Color("dark_red", EnumChatFormatting.DARK_RED.getFormattingCode(), 4, 11141120);
    public static final Color DARK_PURPLE = new Color("dark_purple", EnumChatFormatting.DARK_PURPLE.getFormattingCode(), 5, 11141290);
    public static final Color GOLD = new Color("gold", EnumChatFormatting.GOLD.getFormattingCode(), 6, 16755200);
    public static final Color GRAY = new Color("gray", EnumChatFormatting.GRAY.getFormattingCode(), 7, 11184810);
    public static final Color DARK_GRAY = new Color("dark_gray", EnumChatFormatting.DARK_GRAY.getFormattingCode(), 8, 5592405);
    public static final Color BLUE = new Color("blue", EnumChatFormatting.BLUE.getFormattingCode(), 9, 5592575);
    public static final Color GREEN = new Color("green", EnumChatFormatting.GREEN.getFormattingCode(), 10, 5635925);
    public static final Color AQUA = new Color("aqua", EnumChatFormatting.AQUA.getFormattingCode(), 11, 5636095);
    public static final Color RED = new Color("red", EnumChatFormatting.RED.getFormattingCode(), 12, 16733525);
    public static final Color LIGHT_PURPLE = new Color("light_purple", EnumChatFormatting.LIGHT_PURPLE.getFormattingCode(), 13, 16733695);
    public static final Color YELLOW = new Color("yellow", EnumChatFormatting.YELLOW.getFormattingCode(), 14, 16777045);
    public static final Color WHITE = new Color("white", EnumChatFormatting.WHITE.getFormattingCode(), 15, 16777215);
    public static final Color OBFUSCATED = new Color("obfuscated", EnumChatFormatting.OBFUSCATED.getFormattingCode(), 16, true);
    public static final Color BOLD = new Color("bold", EnumChatFormatting.BOLD.getFormattingCode(), 17, true);
    public static final Color STRIKETHROUGH = new Color("strikethrough", EnumChatFormatting.STRIKETHROUGH.getFormattingCode(), 18, true);
    public static final Color UNDERLINE = new Color("underline", EnumChatFormatting.UNDERLINE.getFormattingCode(), 19, true);
    public static final Color ITALIC = new Color("italic", EnumChatFormatting.ITALIC.getFormattingCode(), 20, true);
    public static final Color RESET = new Color("reset", EnumChatFormatting.RESET.getFormattingCode(), 21);
    private static final List<Color> COLORS;

    static {
        COLORS = Lists.newArrayList();

        final Field[] declaredFields = Colors.class.getDeclaredFields();
        for (Field field : declaredFields) {
            if (Modifier.isStatic(field.getModifiers())) {
                try {
                    COLORS.add((Color) field.get(null));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<Color> getBuiltinColors() {
        return Collections.unmodifiableList(COLORS);
    }

    public static Optional<Color> getByCode(char code) {
        return getByCode(code);
    }

    public static Optional<Color> getByCode(int code) {
        for (Color c : COLORS) {
            if (c.getChatCode() == code) {
                return Optional.of(c);
            }
        }

        return Optional.absent();
    }
}

@@ -1521,16 +1521,48 @@
 il.List;
+%0Aimport java.util.regex.Pattern;
 %0A%0Apublic
@@ -1583,17 +1583,16 @@
 olors %7B%0A
-%0A
     publ
@@ -4297,16 +4297,155 @@
 COLORS;%0A
+    private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile(%22(?i)%22 + String.valueOf(Color.CHAR_COLOR_BEGIN) + %22%5B0-9A-FK-OR%5D%22);%0A%0A
 %0A    sta
@@ -5331,11 +5331,214 @@
 );%0A    %7D
+%0A%0A    public static String stripColors(String colorized) %7B%0A        if (colorized == null) %7B%0A            return null;%0A        %7D%0A%0A        return STRIP_COLOR_PATTERN.matcher(colorized).replaceAll(%22%22);%0A    %7D
 %0A%7D%0A

Add util to strip color codes.