package hmggirlfront.entity;

import java.util.List;

import gvclib.entity.living.ISoldier;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.Block_Spawn;
import hmggirlfront.items.ItemDollAttachment;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.EnumSkyBlock;

public class DollAI_Class {

	public static void load(EntityDoll doll) {
		ItemStack feet = doll.getItemStackFromSlot(EntityEquipmentSlot.FEET);
		if(!feet.isEmpty() &&  feet.getItem() instanceof ItemDollAttachment) {
			ItemDollAttachment am = (ItemDollAttachment) feet.getItem();
			if(feet.getItem() == mod_GirlFront.dollam_class_assalt && doll.getMoveT() != 2) {
				{
					int i = 16;
					int i2 = 8;
			    		for(int x = 0; x < i; ++x) {
			    			for(int y = 0; y < i; ++y) {
			    				for(int z = 0; z < i; ++z) {
			    					if(doll.world.getBlockState(new BlockPos(doll.posX -i2 + x, doll.posY - i2 + y, doll.posZ - i2 + z)).getBlock() instanceof Block_Spawn) {
			    						doll.setMoveX((int) (doll.posX -i2 + x));
			    						doll.setMoveY((int) (doll.posY -i2 + y));
			    						doll.setMoveZ((int) (doll.posZ -i2 + z));
			    						doll.setMoveT(2);
			    						break;
			    					}
			    				}
			    			}
			    		}
		    	}
				{
					int i = 8;
					int i2 = 4;
			    		for(int x = 0; x < i; ++x) {
			    			for(int y = 0; y < i; ++y) {
			    				for(int z = 0; z < i; ++z) {
			    					if(doll.world.getBlockState(new BlockPos(doll.posX -i2 + x, doll.posY - i2 + y, doll.posZ - i2 + z)).getBlock() instanceof Block_Spawn) {
			    						if(!doll.world.isRemote){
			    			            	//doll.world.setBlockToAir(new BlockPos(doll.posX -i2 + x, doll.posY - i2 + y, doll.posZ - i2 + z));
			    							BlockPos pos = new BlockPos(doll.posX -i2 + x, doll.posY - i2 + y, doll.posZ - i2 + z);
			    							IBlockState iblockstate = doll.world.getBlockState(pos);
			        		                Block block = iblockstate.getBlock();
			        		                if (iblockstate.getMaterial() != Material.AIR)
			        		                {
			        		                    block.dropBlockAsItemWithChance(doll.world, pos, doll.world.getBlockState(pos), 1.0F, 0);
			        		                    doll.world.setBlockToAir(pos);
			        		                }
			    			            }
			    						doll.setMoveT(3);
			    						break;
			    					}
			    				}
			    			}
			    		}
		    	}
			}
			if(feet.getItem() == mod_GirlFront.dollam_class_medic) {
				boolean flag = false;
				{
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox().grow(3.0D));
					if (llist != null) {
						for (int lj = 0; lj < llist.size(); lj++) {
							Entity entity1 = (Entity) llist.get(lj);
							if (entity1.canBeCollidedWith() && entity1 != null) {
								if (entity1 instanceof EntityDoll && entity1 != doll)
		                        {
									EntityDoll em = (EntityDoll) entity1;
									/*if(em.getRemain_A() < em.getMaxHealth() && doll.getRemain_A() > doll.getMaxHealth()) {
										em.setRemain_A((int) em.getMaxHealth());
										doll.setRemain_A((int) (doll.getRemain_A() - em.getMaxHealth()));
										doll.playSound(SoundEvents.ENTITY_ITEM_PICKUP, 1.0F, 1.0F);
										break;
									}*/
									if(em.getHealth() <= 0.0F) {
										flag = true;
										++doll.mediccount;
										if(doll.mediccount > 40){
											em.deathTime = 0;
											em.deathTicks = 0;
											em.setHealth(4);
											em.playSound(SoundEvents.ITEM_SHIELD_BREAK, 1.0F, 1.0F);
											doll.swingArm(EnumHand.MAIN_HAND);
										}
									}
		                        }
							}
						}
					}
				}
				if(!flag) {
					if(doll.mediccount > 0)--doll.mediccount;
				}
			}
			if (feet.getItem() == mod_GirlFront.dollam_class_support) {
				{
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox().grow(20.0D));
					if (llist != null) {
						for (int lj = 0; lj < llist.size(); lj++) {
							Entity entity1 = (Entity) llist.get(lj);
							if (entity1.canBeCollidedWith() && entity1 != null) {
								if (entity1 instanceof EntityDoll && entity1 != doll)
		                        {
									EntityDoll em = (EntityDoll) entity1;
									if(em.getRemain_S() < em.magazine && doll.getRemain_S() > doll.magazine) {
										em.setRemain_S((int) em.magazine);
										doll.setRemain_S((int) (doll.getRemain_S() - em.magazine));
										doll.playSound(SoundEvents.ENTITY_ITEM_PICKUP, 1.0F, 1.0F);
										break;
									}
		                        }
							}
						}
					}
				}
			}
			if (feet.getItem() == mod_GirlFront.dollam_class_scout) {
				if(doll.targetentity != null) {
					{
						List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox().grow(60.0D));
						if (llist != null) {
							for (int lj = 0; lj < llist.size(); lj++) {
								Entity entity1 = (Entity) llist.get(lj);
								if (entity1.canBeCollidedWith() && entity1 != null) {
									if (entity1 instanceof EntityLivingBase && entity1 == doll.targetentity)
			                        {
										EntityLivingBase em = (EntityLivingBase) entity1;
										em.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 10));
			                        }
								}
							}
						}
					}
				}
			}
			if (feet.getItem() == mod_GirlFront.dollam_class_torcher) {
				boolean re = false;
				BlockPos blockpos = new BlockPos(doll.posX, doll.getEntityBoundingBox().minY, doll.posZ);
				BlockPos blockpos1 = new BlockPos(doll.posX, doll.getEntityBoundingBox().minY - 1, doll.posZ);
				if (doll.world.getLightFor(EnumSkyBlock.SKY, blockpos) > doll.world.rand.nextInt(32)) {
					re = false;
				} else {
					int i = doll.world.getLightFromNeighbors(blockpos);

					if (doll.world.isThundering()) {
						int j = doll.world.getSkylightSubtracted();
						doll.world.setSkylightSubtracted(10);
						i = doll.world.getLightFromNeighbors(blockpos);
						doll.world.setSkylightSubtracted(j);
					}
					if (i <= doll.world.rand.nextInt(8)) {
						re = true;
					} else {
						re = false;
					}
				}
				Block block = doll.world.getBlockState(blockpos1).getBlock();
				if(!block.isOpaqueCube(doll.world.getBlockState(blockpos1))) {
					re = false;
				}
				if(!block.isFullCube(doll.world.getBlockState(blockpos1))) {
					re = false;
				}
				if(block.getRenderType(doll.world.getBlockState(blockpos1)) != EnumBlockRenderType.MODEL) {
					re = false;
				}
				if (re) {
					if (doll.getRemain_L() > 0) {
						if (!doll.world.isRemote) {
							doll.world.setBlockState(blockpos, Blocks.TORCH.getDefaultState(), 2);
							doll.playSound(SoundEvents.BLOCK_WOOD_PLACE, 1.0F, 1.0F);
						}
						doll.setRemain_L(doll.getRemain_L() - 1);
					}
				}
			}
		}
	}
	
}
