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

import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;

import org.bukkit.ChatColor;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;

public class TamedEntityProtectManager implements Listener {
	private PandacraftUtils plugin = PandacraftUtils.getInstance();
	

	public TamedEntityProtectManager()
	{
		
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        
	}
	
	
	
	@EventHandler
	public void onEntityTame(EntityTameEvent event) {
		if (event.getEntity().getWorld().getPVP()) return;
		
		if (!(event.getEntity() instanceof Tameable)) return;
		if (!(event.getOwner() instanceof Player)) return;
		
		Tameable tameableEntity = (Tameable) event.getEntity();
		
		tameableEntity.setOwner(event.getOwner());
		
		Player p = (Player) event.getOwner();
		p.sendMessage(ChatColor.GREEN+"Vous venez de protéger "+get_thisAnimal_Name(tameableEntity));
	}
	
	
	@EventHandler
	public void onVehicleEnter(VehicleEnterEvent event) {
		if (event.getVehicle().getWorld().getPVP()) return;
		
		if (!isConcerned(event.getEntered(), event.getVehicle())) return;
		
		Tameable tameableEntity = (Tameable) event.getVehicle();
		
		if (tameableEntity.getOwner().equals(event.getEntered())) return;
		
		event.setCancelled(true);
		Player p = (Player) event.getEntered();
		p.sendMessage(ChatColor.RED+get_thisAnimal_Name(tameableEntity)+ChatColor.RED+" est à "+getOwnerDisplayName(tameableEntity)+ChatColor.RED+", vous ne pouvez pas y toucher");
		
		
	}
	
	
	@EventHandler
	public void onEntityDamage(EntityDamageByEntityEvent event) {
		if (event.getEntity().getWorld().getPVP()) return;
		
		if (!isConcerned(event.getDamager(), event.getEntity())) return;
		
		Player p = (Player) event.getDamager();
		Tameable animal = (Tameable) event.getEntity();
		
		if (animal.getOwner().equals(p)) return;
		
		event.setCancelled(true);
		p.sendMessage(ChatColor.RED+get_thisAnimal_Name(animal)+ChatColor.RED+" est à "+getOwnerDisplayName(animal)+ChatColor.RED+", vous ne pouvez pas y toucher");
		
	}
	

	
	@EventHandler
	public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
		if (event.getPlayer().getWorld().getPVP()) return;
		
		if (!isConcerned(event.getPlayer(), event.getRightClicked())) return;
		
		Tameable animal = (Tameable) event.getRightClicked();
		
		boolean selected = OnlinePlayerManager.get(event.getPlayer()).setSelectedAnimal(animal);
		if (selected) {
			event.getPlayer().sendMessage(ChatColor.GREEN+"Vous venez de sélectionner "+get_thisAnimal_Name(animal));
		}
		
		if (animal.getOwner().equals(event.getPlayer())) return;
		
		event.setCancelled(true);
		event.getPlayer().sendMessage(ChatColor.RED+get_thisAnimal_Name(animal)+ChatColor.RED+" est à "+getOwnerDisplayName(animal)+ChatColor.RED+", vous ne pouvez pas y toucher");
		
	}
	
	
	
	
	
	
	private boolean isConcerned(Object player, Object tameable) {
		if (!(player instanceof Player)) return false;
		if (!(tameable instanceof Tameable)) return false;
		if (((Tameable)tameable).getOwner() == null) return false;
		return true;
	}
	
	
	
	
	
	public String getOwnerDisplayName(Tameable animal) {
		if (animal == null || animal.getOwner() == null)
			return null;
		
		AnimalTamer owner  = animal.getOwner();
		if (owner instanceof Player) // si le joueur est en ligne
			return ((Player) owner).getDisplayName();
		
		return owner.getName();
	}
	
	
	
	
	
	/**
	 * Donne le nom personnalisé de l'animal passé en paramètre, ou son espèce, le cas échéant, de la forme
	 * "ce cheval", "ce loup" ou "ce chat".
	 * @param animal l'animal apprivoisable pour lequel il faut récupérer son nom
	 * @return
	 */
	public String get_thisAnimal_Name(Tameable animal) {
		if (animal instanceof Horse) {
			String customName = ((Horse) animal).getCustomName();
			return (customName == null) ? "ce cheval" : ChatColor.RESET+customName;
		}
		if (animal instanceof Wolf) {
			String customName = ((Wolf) animal).getCustomName();
			return (customName == null) ? "ce loup" : ChatColor.RESET+customName;
		}
		if (animal instanceof Ocelot) {
			String customName = ((Ocelot) animal).getCustomName();
			return (customName == null) ? "ce chat" : ChatColor.RESET+customName;
		}
		
		return "cet animal";
	}
	
	
	
	
	
}
