package com.publicuhc.ultrahardcore.features.playerfreeze;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.publicuhc.ultrahardcore.commands.FreezeCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import com.publicuhc.configuration.ConfigManager;
import com.publicuhc.ultrahardcore.features.UHCFeature;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.*;

@Singleton
public class PlayerFreezeFeature extends UHCFeature {

    private final Map<String, Entity> m_entityMap = new HashMap<String,Entity>();
    private boolean m_globalMode = false;

    /**
     * handles frozen players
     * @param plugin the plugin
     * @param configManager the config manager
     */
    @Inject
    private PlayerFreezeFeature(Plugin plugin, ConfigManager configManager) {
        super(plugin, configManager);
    }

    /**
     * @param player the entity to freeze
     */
    public void addPlayer(Player player){
        LivingEntity chicken = (LivingEntity) player.getWorld().spawnEntity(player.getLocation(), EntityType.CHICKEN);
        chicken.setPassenger(player);
        chicken.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 0, true));
        chicken.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, Integer.MAX_VALUE, 5, true));
        chicken.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 5, true));
        m_entityMap.put(player.getName(),chicken);
    }

    /**
     * @param name the player name
     */
    public void removePlayer(String name){
        removeChicken(name);
        m_entityMap.remove(name);
    }

    /**
     * remove the chicken for the name
     * @param name the player name
     */
    private void removeChicken(String name){
        if(m_entityMap.containsKey(name)){
            Entity chicken = m_entityMap.get(name);
            chicken.setPassenger(null);
            chicken.remove();
            m_entityMap.put(name,null);
        }
    }

    /**
     * Remove all from the frozen list and sets global off
     */
    public void unfreezeAll(){
        m_globalMode = false;
        for(String s : m_entityMap.keySet()){
            removePlayer(s);
        }
    }

    /**
     * Adds all to the list and sets global on
     */
    public void freezeAll(){
        m_globalMode = true;
        for(Player p : Bukkit.getOnlinePlayers()){
            addPlayer(p);
        }
    }

    /**
     * Whenever a player joins
     * @param pje the player join event
     */
    @EventHandler( priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPlayerJoinEvent(PlayerJoinEvent pje){
        if((m_globalMode || m_entityMap.keySet().contains(pje.getPlayer().getName()))
             && !pje.getPlayer().hasPermission(FreezeCommand.ANTIFREEZE_PERMISSION)){
            addPlayer(pje.getPlayer());
        }else{
            removePlayer(pje.getPlayer().getName());
        }
    }

    /**
     * When a player logs out remove the chicken
     * @param pqe the quit event
     */
    @EventHandler
    public void onPlayerLogout(PlayerQuitEvent pqe){
        if(m_entityMap.containsKey(pqe.getPlayer().getName())){
            removeChicken(pqe.getPlayer().getName());
        }
    }

    /**
     * Called when the feature is being disabled
     */
    @Override
    protected void disableCallback(){
        unfreezeAll();
    }

    @Override
    public String getFeatureID() {
        return "PlayerFreeze";
    }

    @Override
    public String getDescription() {
        return "Allows for freezing players in place";
    }
}

@@ -401,13 +401,12 @@
 ayer
-Login
+Quit
 Even
@@ -428,39 +428,41 @@
 ukkit.event.
-player.PlayerQu
+vehicle.VehicleEx
 itEvent;%0Aimp
@@ -1273,23 +1273,19 @@
 gEntity 
-chicken
+pig
  = (Livi
@@ -1361,15 +1361,11 @@
 ype.
-CHICKEN
+PIG
 );%0A 
@@ -1363,39 +1363,35 @@
 e.PIG);%0A        
-chicken
+pig
 .setPassenger(pl
@@ -1397,39 +1397,35 @@
 layer);%0A        
-chicken
+pig
 .addPotionEffect
@@ -1503,39 +1503,35 @@
 true));%0A        
-chicken
+pig
 .addPotionEffect
@@ -1622,23 +1622,19 @@
         
-chicken
+pig
 .addPoti
@@ -1753,23 +1753,20 @@
 tName(),
-chicken
+ pig
 );%0A    %7D
@@ -1875,23 +1875,19 @@
   remove
-Chicken
+Pig
 (name);%0A
@@ -2040,23 +2040,19 @@
 d remove
-Chicken
+Pig
 (String 
@@ -2120,23 +2120,19 @@
  Entity 
-chicken
+pig
  = m_ent
@@ -2161,23 +2161,19 @@
         
-chicken
+pig
 .setPass
@@ -2197,23 +2197,19 @@
         
-chicken
+pig
 .remove(
@@ -2260,32 +2260,453 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Called when a living entity tries to exit a vehicle%0A     * @param vee the vechile exit event%0A     */%0A    @EventHandler%0A    public void onVehicleDismountEvent(VehicleExitEvent vee)%7B%0A        LivingEntity entity = vee.getExited();%0A        if(entity instanceof Player)%7B%0A            if(m_entityMap.containsKey(((Player)entity).getName()))%7B%0A                vee.setCancelled(true);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     /**%0A     * R
@@ -3904,15 +3904,11 @@
 move
-Chicken
+Pig
 (pqe

use pigs so we can cancel the dismount