package hmggirlfront.event;

import java.util.List;

import gvclib.mod_GVCLib;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.EntityVehicleBase;
import gvclib.item.ItemGunBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.biome.CollapseBiome;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.entity.EntityDollBase;
import hmggirlfront.entity.EntityKalina;
import hmggirlfront.entity.mav.EntityMAV;
import hmggirlfront.entity.mob.EntityELIDBase;
import hmggirlfront.entity.mob.EntityIBCBase;
import hmggirlfront.entity.mob.EntitySCMBase;
import hmggirlfront.items.ItemDollCN;
import hmggirlfront.items.ItemDollGun_SD;
import hmggirlfront.network.GFMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeForest;
import net.minecraft.world.biome.BiomeTaiga;
import net.minecraftforge.event.entity.EntityMountEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GFEventLiving {

	
	@SubscribeEvent
	public void onUnloadEvent(WorldEvent.Unload event) {
		
	}
	
	
	@SubscribeEvent
	public void onUpdateEvent_Collapse(LivingUpdateEvent event) {
		EntityLivingBase entity = event.getEntityLiving();
		if(entity != null) {
			/*if(entity.world.getBiome(new BlockPos(entity.posX,entity.posY, entity.posZ)) instanceof CollapseBiome)
			{
				boolean flag = false;
	    		
	    		ItemStack head = entity.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				ItemStack chest = entity.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
				ItemStack legs = entity.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
				ItemStack feet = entity.getItemStackFromSlot(EntityEquipmentSlot.FEET);
				
				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 (entity instanceof EntityDollBase) {
					flag = true;
				}
				if (entity instanceof EntityIBCBase) {
					flag = true;
				}
				if (entity instanceof EntitySCMBase) {
					flag = true;
				}
				if (entity instanceof EntityELIDBase) {
					flag = true;
				}
				if (entity instanceof EntityKalina) {
					EntityKalina kalin = (EntityKalina) entity;
					if(kalin.getArmID_A() == 3) {
						flag = true;
					}
				}
				if (entity instanceof EntityVehicleBase) {
					flag = true;
				}
				
				if(!flag) {
					if (!entity.isPotionActive(mod_GirlFront.collapse_potion))
						entity.addPotionEffect(
								new PotionEffect(mod_GirlFront.collapse_potion, 1000, 0, true, true));
				}
			}*/
		}
		
	}
	
	@SubscribeEvent
	public void onUpdateEvent_Supplies(PlayerEvent event) {
		EntityPlayer player =  event.getEntityPlayer();
		if(player != null) {
			boolean left = mod_GVCLib.proxy.leftclick();
			boolean right = mod_GVCLib.proxy.rightclick();
			boolean kx = mod_GVCLib.proxy.keyx();
			boolean kc = mod_GVCLib.proxy.keyc();
			boolean kz = mod_GVCLib.proxy.keyz();
			ItemStack itemstack = ((EntityPlayer) (player)).getHeldItemMainhand();
			if(itemstack.getItem() == mod_GirlFront.idoll_parts) {
				if(left && right && kx && kz && kc) {
					GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(2020, player.getEntityId()));
				}
			}
		}
		
		
	}
	
	
	@SubscribeEvent
	public void onDeadEvent(LivingDeathEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		EntityLivingBase en = target.getAttackingEntity();
		if(target != null && en != null) {
			if(en instanceof EntityDoll) {
				EntityDoll doll = (EntityDoll) en;
				doll.setArmID_S(doll.getArmID_S() + 1);
				{
					if(!target.world.isRemote) {
						 int i = doll.getArmID_S() * 1;
			                while (i > 0)
			                {
			                    int j = EntityXPOrb.getXPSplit(i);
			                    i -= j;
			                    target.world.spawnEntity(new EntityXPOrb(target.world, target.posX, target.posY, target.posZ, j));
			                }
					}
				}
				if(doll.getOwner() != null && doll.getOwner() instanceof EntityPlayer) {
					EntityPlayer player = (EntityPlayer) doll.getOwner();
				}
				if(target instanceof EntityLiving) {
					EntityLiving targeten = (EntityLiving) target;
					
				}
			}
		}
		/*Entity en = event.getSource().getImmediateSource();
		if(target != null && en != null && event.getSource() != null) {
			if(target.deathTime == 1) {
				if(en instanceof EntityDoll) {
					EntityDoll doll = (EntityDoll) en;
					doll.setArmID_S(doll.getArmID_S() + 1);
				}
			}
		}*/
	}
	
	@SubscribeEvent
	public void onHurtEvent(LivingHurtEvent event) {
		EntityLivingBase target = event.getEntityLiving();
		DamageSource source = event.getSource();
		float damage = event.getAmount();
		if (target instanceof EntityPlayer && target != null) 
		{
			Entity entity = source.getTrueSource();
	    	if(entity != null && entity instanceof EntityDoll){
	    		event.setAmount(0);
	    		//amount = 0.0F;
	            event.setCanceled(true);
	    	}
		}
	}
	
	@SubscribeEvent
	  public void onLivingFromDollEvent(LivingAttackEvent event)
	  {
		EntityLivingBase target = event.getEntityLiving();
		DamageSource source = event.getSource();
		float damage = event.getAmount();
		if (target instanceof EntityPlayer && target != null) 
		{
			Entity entity = source.getTrueSource();
	    	if(entity != null && entity instanceof EntityDoll){
	    		//amount = 0.0F;
	            event.setCanceled(true);
	    	}
		}
	  }
	
	@SubscribeEvent
	public void LivingHurtEvent(LivingHurtEvent event) {
		EntityLivingBase target = event.getEntityLiving();
		ItemStack itemstack = target.getHeldItemMainhand();
		DamageSource source = event.getSource();
		if(target instanceof EntityPlayer && target != null){
			if (target != null && !itemstack.isEmpty() && itemstack.getItem() instanceof ItemDollGun_SD 
					&& itemstack.getItemDamage() != itemstack.getMaxDamage()&& itemstack.hasTagCompound()) {
				NBTTagCompound nbtgun = itemstack.getTagCompound();
				boolean right = nbtgun.getBoolean("RightClick");
				if(right || target.getActiveItemStack() == itemstack && (source != null && !source.isMagicDamage()))
				{
					ItemGunBase gun = (ItemGunBase) itemstack.getItem();
					event.setAmount(event.getAmount() * gun.Shield_defence);
					target.world.setEntityState(target, (byte)29);
				}
			}
		}
		
		ItemStack itemstackl = target.getHeldItemOffhand();
		if(target instanceof EntityPlayer && target != null){
			if (target != null &&! itemstackl.isEmpty()&& itemstackl.getItem() instanceof ItemDollGun_SD 
					&& itemstackl.getItemDamage() != itemstackl.getMaxDamage()&& itemstackl.hasTagCompound()) {
				NBTTagCompound nbtgun = itemstackl.getTagCompound();
				boolean left = nbtgun.getBoolean("LeftClick");
				if((left || target.isSneaking())  && (source != null && !source.isMagicDamage()))
				{
					ItemGunBase gun = (ItemGunBase) itemstackl.getItem();
					event.setAmount(event.getAmount() * gun.Shield_defence);
					target.world.setEntityState(target, (byte)29);
				}
			}
		}
		if(target instanceof EntityGVCLivingBase && target != null){
			if (itemstackl != null && itemstackl.getItem() instanceof ItemDollGun_SD  
					&& (source != null && !source.isMagicDamage())) {
				{
					ItemGunBase gun = (ItemGunBase) itemstackl.getItem();
					event.setAmount(event.getAmount() * gun.Shield_defence * 2);
					target.world.setEntityState(target, (byte)29);
				}
			}
		}
	}/**/
	
	
	@SubscribeEvent
	public void onUpdateEvent(PlayerEvent event) {
		if(!mod_GirlFront.cfg_hud) {
			return;
		}
		EntityPlayer player =  event.getEntityPlayer();
		
		if(player != null) {
			NBTTagCompound nbt = player.getEntityData();
			if(nbt == null) {
				return;
			}
			ItemStack itemstack = ((EntityPlayer) (player)).getHeldItemMainhand();
			ItemStack itemstackl = ((EntityPlayer) (player)).getHeldItemOffhand();
			if(itemstack.getItem() instanceof ItemDollCN) {
				this.controaldoll(player, itemstack);
			}
			if(itemstackl.getItem() instanceof ItemDollCN) {
				this.controaldoll(player, itemstackl);
			}
			
		}
		
		
	}
	
	public void controaldoll(EntityPlayer player, ItemStack itemstack) {
		NBTTagCompound nbt = player.getEntityData();
		int count = 1;
		if(itemstack.isEmpty() && !(itemstack.getItem() instanceof ItemDollCN))return;
		ItemDollCN cn = (ItemDollCN) itemstack.getItem();
		NBTTagCompound nbtcn = itemstack.getTagCompound();
		if(nbtcn == null)return;
		int squadid = nbtcn.getInteger("squadid");
		{
			List<Entity> llist = player.world.getEntitiesWithinAABBExcludingEntity(player,
					player.getEntityBoundingBox().expand(player.motionX, player.motionY, player.motionZ).grow(100.0D));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 instanceof EntityDoll && entity1 != null) {
						EntityDoll doll = (EntityDoll) entity1;
						if(doll.isOwner(player)  && doll.getSquadID() == squadid) {
							++count;
						}
					}
				}
			}
		}
		int co = nbt.getInteger("dollcount");
		int co_cool = nbt.getInteger("dollcount_cool");
		if(player.world.isRemote) {
			if(co_cool <= 30){
				++co_cool;
				nbt.setInteger("dollcount_cool", co_cool);
			}
		}
		
		if (mod_GirlFront.proxy.left() && co > 0 && co_cool >= 5) {
			if(player.world.isRemote) {
				//nbt.setInteger("dollcount", co - 1);
			}else {
				//GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(20, co - 1, false));
			}
			nbt.setInteger("dollcount", co - 1);
			nbt.setInteger("dollcount_cool", 0);
		}
		if (mod_GirlFront.proxy.right() && co < count-1 && co_cool >= 5) {
			nbt.setInteger("dollcount", co + 1);
			nbt.setInteger("dollcount_cool", 0);
		}
		
		
		
		if(co != 0){
			int cou = 1;
			EntityDoll dollbase = null;
			List<Entity> llist = player.world.getEntitiesWithinAABBExcludingEntity(player,
					player.getEntityBoundingBox().expand(player.motionX, player.motionY, player.motionZ).grow(100.0D));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 instanceof EntityDoll && entity1 != null) {
						EntityDoll doll = (EntityDoll) entity1;
						if(doll.isOwner(player)  && doll.getSquadID() == squadid) {
							if(co == cou) {
								dollbase = doll;
								break;
							}else {
								++cou;
							}
						}
					}
				}
			}
			if(dollbase != null) {
				if (mod_GirlFront.proxy.up() && co_cool >= 5) {
					GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(21, dollbase.getEntityId()));
					{
						if(dollbase.getMoveT() == 0) {
							if(player.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.stay.name", new Object[0]));
						}
						else if(dollbase.getMoveT() == 1) {
							if(player.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
						}
						else if(dollbase.getMoveT() == 3) {
							if(player.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.free.name", new Object[0]));
						}
						else if(dollbase.getMoveT() == 2) {
							if(player.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
						}
					}
					nbt.setInteger("dollcount_cool", 0);
				}
				if (mod_GirlFront.proxy.down() && co_cool >= 5) {
					GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(22, dollbase.getEntityId()));
					if(player.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.pointattack.name", new Object[0]));
					nbt.setInteger("dollcount_cool", 0);
				}
			}
		}else {
			int coun = 0;
			List<Entity> llist = player.world.getEntitiesWithinAABBExcludingEntity(player,
					player.getEntityBoundingBox().expand(player.motionX, player.motionY, player.motionZ).grow(100.0D));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 instanceof EntityDoll && entity1 != null) {
						EntityDoll doll = (EntityDoll) entity1;
						if(doll.isOwner(player)  && doll.getSquadID() == squadid) {
							if (mod_GirlFront.proxy.up() && co_cool >= 5) {
								GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(21, doll.getEntityId()));
								{
									if(doll.getMoveT() == 0) {
										if(player.world.isRemote && coun == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.stay.name", new Object[0]));
									}
									else if(doll.getMoveT() == 1) {
										if(player.world.isRemote && coun == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
									}
									else if(doll.getMoveT() == 3) {
										if(player.world.isRemote && coun == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.free.name", new Object[0]));
									}
									else if(doll.getMoveT() == 2) {
										if(player.world.isRemote && coun == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
									}
    							}
								nbt.setInteger("dollcount_cool", 0);
								++coun;
							}
							if (mod_GirlFront.proxy.down() && co_cool >= 5) {
								GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(22, doll.getEntityId()));
								if(player.world.isRemote && coun == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.order.pointattack.name", new Object[0]));
								nbt.setInteger("dollcount_cool", 0);
								++coun;
							}
						}
					}
				}
			}
		}
	}
	
	/*@SubscribeEvent
	public void onUpdateEvent(LivingUpdateEvent event) {
		World worldIn = event.getEntityLiving().world;
		EntityLivingBase target = event.getEntityLiving();
		EntityLivingBase en = target.getAttackingEntity();
		if(target != null && en != null) {
			if(en instanceof EntityDoll) {
				EntityDoll doll = (EntityDoll) en;
				doll.setArmID_S(doll.getArmID_S() + 1);
			}
		}
	}*/
}
