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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class CalculatorManager implements Listener {
	
	private PandacraftUtils plugin;
	
	private Map<Player, List<HistoryElement>> history = new HashMap<Player, List<HistoryElement>>();
	
	
	
	public CalculatorManager(PandacraftUtils pl) {
		plugin = pl;
		
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
		
		// analyse des joueurs déjà en ligne (/reload)
		for (Player p : plugin.getServer().getOnlinePlayers())
			onPlayerJoin(new PlayerJoinEvent(p, ""));	// simule l'évènement d'arrivé d'un joueur, pour le rajouter
	}
	
	
	
	
	@EventHandler(priority=EventPriority.LOWEST)
	public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
	{
		String message = event.getMessage();
		if (!message.startsWith("="))
			return;
		
		if (message.length() == 1)
		{	// affichage de l'historique
			event.getPlayer().sendMessage(ChatColor.GRAY+"Historique d'utilisation de la calculatrice");
			int i = history.get(event.getPlayer()).size();
			for(HistoryElement el : history.get(event.getPlayer()))
			{
				event.getPlayer().sendMessage(ChatColor.GRAY+"res"+i+ChatColor.RESET+" : "+ChatColor.GRAY+el.expression+ChatColor.RESET+"="+ChatColor.GRAY+el.value);
				i--;
			}
			if (history.get(event.getPlayer()).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);
				
				// TODO prendre en compte les variables "resX"
				
				
				
				StringBuffer expression = new StringBuffer();
				
				HistoryElement calcul = new HistoryElement();
				calcul.value = StringUtil.formatDouble(JArithmeticInterpreter.getResultFromExpression(message, expression));
				calcul.expression = expression.toString();
				
				history.get(event.getPlayer()).add(calcul);
				if (history.get(event.getPlayer()).size() > 5)
					history.get(event.getPlayer()).remove(0);
				
				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");
			}
			
		}
		
		
		
		
		event.setCancelled(true);
	}
	
	

	
	
	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event)
	{
		history.put(event.getPlayer(), new ArrayList<HistoryElement>());
	}
	
	
	
	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent event)
	{
		history.remove(event.getPlayer());
	}
	
	
	
	private class HistoryElement
	{
		public String expression;
		public String value;
	}
	
	
}
