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

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

import org.bukkit.ChatColor;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
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;
		
		LivingEntity entity = event.getEntity();
		AnimalTamer playerOwner = event.getOwner();
		if (entity instanceof Tameable) {
			Tameable tameableEntity = (Tameable) entity;
			tameableEntity.setOwner(playerOwner);
			
			if (playerOwner instanceof Player) {
				Player p = (Player) playerOwner;
				p.sendMessage(ChatColor.GREEN+"Vous venez de protéger cet animal");
			}
			
		}
	}
	
	
	@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()))
		{
			event.setCancelled(true);
			
			Player p = (Player) event.getEntered();
			
			p.sendMessage(ChatColor.RED+"Cet animal est à "+tameableEntity.getOwner().getName()+" 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)) {
			event.setCancelled(true);
			p.sendMessage(ChatColor.RED+"Cet animal est à "+animal.getOwner().getName()+" 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();
		
		if (animal.getOwner().equals(event.getPlayer())) return;
		
	}
	
	
	
	
	
	
	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;
	}
	
	
	
}
