package net.mc_pandacraft.java.plugin.pandacraftutils.modules;

import java.util.List;

import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;
import net.mc_pandacraft.java.util.JArithmeticInterpreter;
import net.mc_pandacraft.java.util.StringUtil;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public class CalculatorManager implements Listener {
	
	private PandacraftUtils plugin = PandacraftUtils.getInstance();
	
	
	
	public CalculatorManager() {
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
	}
	
	
	
	
	@EventHandler(priority=EventPriority.LOWEST)
	public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
	{
		String message = event.getMessage();
		if (!message.startsWith("="))
			return;
		
		OnlinePlayer op = OnlinePlayerManager.get(event.getPlayer());
		List<HistoryElement> history = op.getCalculator_history();
		
		if (message.length() == 1)
		{	// affichage de l'historique
			event.getPlayer().sendMessage(ChatColor.GRAY+"Historique d'utilisation de la calculatrice");
			int i = history.size();
			for(HistoryElement el : history)
			{
				event.getPlayer().sendMessage(ChatColor.GRAY+"res"+i+ChatColor.RESET+" : "+ChatColor.GRAY+el.expression+ChatColor.RESET+"="+ChatColor.GRAY+el.value);
				i--;
			}
			if (history.size() == 0)
			{
				event.getPlayer().sendMessage(ChatColor.RED+"Utilisez le symbole "+ChatColor.GRAY+"="+ChatColor.RED+" dans le chat pour faire un calcul mathématique");
			}
		}
		else
		{
			try {
				message = message.substring(1);

				int i = history.size();
				for(HistoryElement el : history)
				{
					message = message.replace("res"+i, el.value);
					i--;
				}
				
				
				
				StringBuffer expression = new StringBuffer(); // permet de stocker la version reformaté de l'expression mathématique
				
				HistoryElement calcul = new HistoryElement();
				calcul.value = StringUtil.formatDouble(JArithmeticInterpreter.getResultFromExpression(message, expression));
				calcul.expression = expression.toString();
				
				op.addToCalculatorHistory(calcul);
				
				event.getPlayer().sendMessage(ChatColor.GRAY+calcul.expression+ChatColor.RESET+"="+ChatColor.GRAY+calcul.value);
				
			}
			catch (IllegalArgumentException e) {
				event.getPlayer().sendMessage(ChatColor.RED+e.getMessage());
				event.getPlayer().sendMessage(ChatColor.RED+"Utilisez le symbole "+ChatColor.GRAY+"="+ChatColor.RED+" dans le chat pour faire un calcul mathématique");
			}
			catch (NullPointerException e) {
				event.getPlayer().sendMessage(ChatColor.RED+e.getMessage());
				event.getPlayer().sendMessage(ChatColor.RED+"Utilisez le symbole "+ChatColor.GRAY+"="+ChatColor.RED+" dans le chat pour faire un calcul mathématique");
			}
			
		}
		
		
		
		
		event.setCancelled(true);
	}
	
	
	
	public class HistoryElement
	{
		public String expression;
		public String value;
	}
	
	
}
