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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager;
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 org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class AutoMessagesManager extends BukkitRunnable {
	private PandacraftUtils plugin = PandacraftUtils.getInstance();

	
	
	public AutoMessagesManager()
	{
        plugin.getServer().getScheduler().runTaskTimer(plugin, this, 0L, 20L);
	}
	
	
	
	
	
	@Override
	public void run() {
		List<AutoMessage> messages = getMessagesFromConfig();
		int interval_default = ConfigManager.getInstance().Automessage_interval_default,
				interval_players = ConfigManager.getInstance().Automessage_interval_players;
		// message
		// permissions
		
		Collection<OnlinePlayer> oPlayers = OnlinePlayerManager.getAll();
		
		
		for (OnlinePlayer op : oPlayers) {
			
			int interval_sec = op.isGuest() ? interval_default : interval_players;
			
			op.firstAutomessageCkeck(interval_sec);
			
			// on vérifie si il est temps, ou non, d'afficher un message aux joueurs
			if (op.getAutomessage_last_message_time() + (interval_sec*1000) > System.currentTimeMillis())
				continue;
			
			// ici, il se peut que id == -1
			int id = op.getAutomessage_last_message_id();
			boolean atLeastOnePermitted = true;
			int c = 0;
			
			// on cherche le prochain message à afficher
			do {
				id++;
				c++;
				if (id >= messages.size()) id = 0;
				if (c > messages.size()) { // il parcouru au moins tout le tableau, sans trouver de message qu'il peut afficher
					atLeastOnePermitted = false;
					break;
				}
			} while(!messages.get(id).hasPlayerPermission(op.getPlayer()));
			
			// affichage du message
			if (atLeastOnePermitted) {
				op.setAutomessage_last_message_id(id);
				op.updateAutomessage_last_message_time();
				
				op.sendMessageFromServer(messages.get(id).getMessage());
			}
			
		}

	}
	
	
	
	
	public List<AutoMessage> getMessagesFromConfig() {
		ArrayList<AutoMessage> ret = new ArrayList<AutoMessage>();
		
		for (Map<String, String> message : ConfigManager.getInstance().Automessages_messages)
			ret.add(new AutoMessage(ChatColor.translateAlternateColorCodes('&', message.get("message").replace("%n", "\n")), message.get("permissions")));
		
		return ret;
	}
	
	
	
	
	public class AutoMessage {
		private String message;
		private List<String> permissions;
		
		public AutoMessage(String m, String perm) {
			setMessage(m);
			if (perm == null || perm.equals("*"))
				permissions = null;
			else
				permissions = Arrays.asList(perm.split(";"));
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			if (message == null) throw new IllegalArgumentException("message ne doit pas être null");
			this.message = message;
		}
		
		public boolean hasPlayerPermission(Player p) {
			if (p == null) throw new IllegalArgumentException("le joueur ne doit pas être null");
			if (permissions == null)
				return true;
			
			for (String pe : permissions) {
				if (p.hasPermission(pe)) return true;
			}
			return false;
		}
	}

}
