package hmggirlfront.event;

import gvclib.mod_GVCLib;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GFEventArmor {
	
	@SubscribeEvent
	public void OnEvent(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		if(mod_GirlFront.difficult_level == -1) {
			//WorldDifficultyLevel.setDefaultLevel(mod_GirlFront.INSTANCE, worldIn);
		}
	}
	
	@SubscribeEvent
	public void OnEventArmor(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		if (target != null && target instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer) target;
			if(player != null) {
				ItemStack head = player.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_gk_uniform_head) {
					NBTTagCompound nbtcn = head.getTagCompound();
					if(nbtcn == null) {
						head.setTagCompound(new NBTTagCompound());
						NBTTagCompound nbtcn2 = head.getTagCompound();
						nbtcn2.setInteger("squadid", 1);
					}
					boolean key_g = mod_GirlFront.proxy.keyg();
					if(key_g) {
						GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(51));
						if(nbtcn != null) {
							int squadid = nbtcn.getInteger("squadid");
							if(worldIn.isRemote)
								player.sendMessage(new TextComponentTranslation(squadid + "Squad!", new Object[0]));
						}
						
					}
				}
			}
		}
	}
	
	
	@SubscribeEvent
	public void JumpEvent(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		
		//
		if (target != null && target instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer) target;
			if(player != null) {
				ItemStack head = player.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				ItemStack chest = player.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
				ItemStack legs = player.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
				ItemStack feet = player.getItemStackFromSlot(EntityEquipmentSlot.FEET);
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_m_head
						&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_m_chest
						&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_m_legs
						&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_m_feet) {
					NBTTagCompound nbt = target.getEntityData();
					if(nbt != null) {
						boolean jump = mod_GVCLib.proxy.jumped();
						int jumpcootime = nbt.getInteger("jumpcootime");
						int jumpcount = nbt.getInteger("jumpcount");
						if(jumpcount == 2) {
							target.motionY = 0.7D;
							//nbt.setInteger("jumpcount", 3);
							{
								target.world.spawnParticle(EnumParticleTypes.CLOUD, 
										target.posX, target.posY + 0.5D, target.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
								target.world.spawnParticle(EnumParticleTypes.FLAME, 
										target.posX, target.posY + 0.5D, target.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
							}
							if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
								for (EntityPlayerMP player2 : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
					            {
									GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(330, 2), player2);
									GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(330, 2));
					            }
							}
						}else if (jump && jumpcootime > 2 && (jumpcount < 2)) 
						//if (jump) 
						{
							if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
								for (EntityPlayerMP player2 : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
					            {
									GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(330, jumpcount), player2);
									GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(330, jumpcount));
					            }
							}
						}
						if(jumpcootime < 200) {
							nbt.setInteger("jumpcootime", ++jumpcootime);
						}
						if(target.onGround && jumpcount > 2) {
							if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
								for (EntityPlayerMP player2 : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
					            {
									GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(331), player2);
									GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(331));
					            }
							}
						}
					}
				}
			}
		}
	}
	
	/*@SubscribeEvent
	public void UpdateEvent(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		
		//
		if (target != null && target instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer) target;
			if(player != null) {
				ItemStack head = player.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				ItemStack chest = player.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
				ItemStack legs = player.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
				ItemStack feet = player.getItemStackFromSlot(EntityEquipmentSlot.FEET);
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_m_head
						&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_m_chest
						&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_m_legs
						&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_m_feet) {
					NBTTagCompound nbt = target.getEntityData();
					if(nbt != null) {
						int jumpcount = nbt.getInteger("jumpcount");
						if(target.onGround && jumpcount > 1) {
							GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(331));
							GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(331));
						}
					}
				}
			}
		}
	}*/
	
	@SubscribeEvent
	public void ArmorLiving(LivingFallEvent event) {
		// if(eevent.getSide().isClient())
		{
			EntityLivingBase entityLiving = event.getEntityLiving();
			NBTTagCompound nbt = entityLiving.getEntityData();
			if (entityLiving != null) {
				{
					ItemStack head = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
					ItemStack chest = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
					ItemStack legs = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
					ItemStack feet = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.FEET);
					if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_m_head
							&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_m_chest
							&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_m_legs
							&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_m_feet) {
						event.setDistance(0.0F);
					}
				}
				{
					ItemStack head = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
					ItemStack chest = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
					ItemStack legs = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
					ItemStack feet = entityLiving.getItemStackFromSlot(EntityEquipmentSlot.FEET);
					if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_h_head
							&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_h_chest
							&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_h_legs
							&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_h_feet) {
						event.setDistance(0.0F);
					}
				}
			}
		}
	}
	
	@SubscribeEvent
	public void AntiCollapse(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		
		//
		if (target != null) {
				ItemStack head = target.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				ItemStack chest = target.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
				ItemStack legs = target.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
				ItemStack feet = target.getItemStackFromSlot(EntityEquipmentSlot.FEET);
				boolean flag = false;
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_anticollapse_head
						&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_anticollapse_chest
						&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_anticollapse_legs
						&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_anticollapse_feet) {
					flag = true;
				}
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_m_head
						&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_m_chest
						&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_m_legs
						&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_m_feet) {
					flag = true;
				}
				if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_h_head
						&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_h_chest
						&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_h_legs
						&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_h_feet) {
					flag = true;
				}
				
				if(flag) {
					//if (!worldIn.isRemote)
			        {
						if (target.isPotionActive(MobEffects.POISON))target.removePotionEffect(MobEffects.POISON);
						if (target.isPotionActive(MobEffects.WITHER))target.removePotionEffect(MobEffects.WITHER);
						if (target.isPotionActive(mod_GirlFront.collapse_potion))target.removePotionEffect(mod_GirlFront.collapse_potion);
			        }
				}
		}
	}
	
	
}
