package com.hyperfresh.mchyperchat;

import com.hyperfresh.mchyperchat.field.Field;
import com.hyperfresh.mchyperchat.field.FieldFlag;
import com.hyperfresh.mchyperchat.field.FieldManager;

import java.util.EnumSet;
import java.util.Map;
import java.util.regex.Matcher;

public class HyperChat
{
	private HyperChat()
	{

	}

	/**
	 * The current instance of HyperChat
	 */
	private static final HyperChat instance = new HyperChat();

	private HyperChatPlugin plugin = null;

	private final FieldManager fieldManager = new FieldManager();
	private final ThemeManager themeManager = new ThemeManager();
	private final HyperChatEventPoster eventPoster = new HyperChatEventPoster(this);

	/**
	 * Gets the current instance of HyperChat
	 *
	 * @return the current instance of HyperChat.
	 */
	public static HyperChat getInstance()
	{
		return instance;
	}

	/**
	 * This will run when this plugin is enabled.
	 */
	public void setPlugin(HyperChatPlugin plugin)
	{
		this.plugin = plugin;
		//READ "field" FOLDER HERE
	}

	/**
	 * This will run when this plugin is disabled.
	 */
	public void onDisable()
	{
		this.plugin = null;
		this.fieldManager.clear();
	}

	/**
	 * Gets the current console.
	 *
	 * @return the current console
	 */
	public HyperChatPlugin getPlugin()
	{
		return this.plugin;
	}

	public ThemeManager getThemeManager()
	{
		return this.themeManager;
	}

	/**
	 * Get the main list of field.
	 *
	 * @return the main list of field
	 */
	public FieldManager getFieldManager()
	{
		return this.fieldManager;
	}

	public HyperChatEventPoster getEventPoster()
	{
		return this.eventPoster;
	}

	/**
	 * Replaces the existing FieldList with a new one,
	 * effectively deleting all existing field.
	 */
	public void resetFields()
	{
		this.fieldManager.clear();
	}

	private static final String FIELD_REGEX_L = "(?i)\\$\\{", FIELD_REGEX_R = "\\}";

	private static String processFields(Map<String, Field> fields, String str, User user)
	{
		for (Map.Entry<String, Field> e : fields.entrySet())
		{
			str = str.replaceAll(FIELD_REGEX_L + e.getKey() + FIELD_REGEX_R, e.getValue().getFieldValue(user));
		}

		return str;
	}

	/**
	 * Replaces all dynamic fields in a string with their values.
	 * Fields are detected by the pattern <code>"${<field name>}"</code>
	 *
	 * @param str
	 * @param user
	 * @return
	 */
	public String processDynamicFields(String str, User user)
	{
		return processFields(fieldManager.getFields(EnumSet.of(FieldFlag.CONSTANT), true), str, user);
	}

	/**
	 * Replaces all static fields in a string with their values.
	 * Fields are detected by the pattern <code>"${<field name>}"</code>
	 *
	 * @param str
	 * @return
	 */
	public String processStaticFields(String str)
	{
		return processFields(fieldManager.getFields(EnumSet.of(FieldFlag.CONSTANT)), str, null);
	}

	/**
	 * Replaces all inlinable fields in a string with their values.
	 * Fields are detected by the pattern <code>"${<field name>}"</code>
	 *
	 * @param str
	 * @return
	 */
	public String processInlineFields(String str, User user)
	{
		return processFields(fieldManager.getFields(EnumSet.of(FieldFlag.INLINEABLE)), str, user);
	}


}
@@ -30,16 +30,74 @@
 rchat;%0A%0A
+import com.hyperfresh.mchyperchat.command.CommandManager;%0A
 import c
@@ -135,16 +135,16 @@
 .Field;%0A
-
 import c
@@ -239,16 +239,45 @@
 Manager;
+%0Aimport org.bukkit.ChatColor;
 %0A%0Aimport
@@ -673,32 +673,106 @@
 ThemeManager();%0A
+%09private final CommandManager commandManager = new CommandManager(this);%0A%0A
 %09private final H
@@ -1678,24 +1678,104 @@
 anager;%0A%09%7D%0A%0A
+%09public CommandManager getCommandManager()%0A%09%7B%0A%09%09return this.commandManager;%0A%09%7D%0A%0A
 %09public Hype
@@ -2012,16 +2012,16 @@
 ();%0A%09%7D%0A%0A
-
 %09private
@@ -2095,16 +2095,691 @@
 %22%5C%5C%7D%22;%0A%0A
+%09/**%0A%09 * Converts & codes into section signs in order for Minecraft to display colored text.%0A%09 *%0A%09 * @param string%0A%09 * @return%0A%09 */%0A%09public String colorize(String string)%0A%09%7B%0A%09%09if(string == null) return null;%0A%0A%09%09//TODO: use our own implementation of ChatColor%0A%09%09return ChatColor.translateAlternateColorCodes('&', string);%0A%09%7D%0A%0A%09/**%0A%09 * Preprocesses formats read from themes, which is a combination of %7B@code colorize()%7D%0A%09 * and %7B@code processStaticFields()%7D.%0A%09 *%0A%09 * @param string%0A%09 * @return%0A%09 */%0A%09public String preprocessThemeFormat(String string)%0A%09%7B%0A%09%09if(string == null) return null;%0A%09%09string = colorize(string);%0A%09%09string = processStaticFields(string);%0A%09%09return string;%0A%09%7D%0A%0A
 %09private

add commandManager getter in HyperChat, added colorize() and preprocess methods