package hmggirlfront.network;
import java.util.List;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.BlockM_Crafting;
import hmggirlfront.block.BlockM_Restoring;
import hmggirlfront.block.tile.TileEntityM_Crafting;
import hmggirlfront.block.tile.TileEntityM_Restoring;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.items.ItemDollCore;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
 
public class GFMessageKeyPressedHandler implements IMessageHandler<GFMessageKeyPressed, IMessage> {
 
    @Override
    public IMessage onMessage(GFMessageKeyPressed message, MessageContext ctx) {
        EntityPlayer entityPlayer = ctx.getServerHandler().player;
        if (message.key == 1) {
			ItemStack itemstack = ((EntityPlayer)(entityPlayer)).getHeldItemMainhand();
			if(!itemstack.isEmpty() && itemstack.getItem() instanceof ItemDollCore){
			entityPlayer.openGui(mod_GirlFront.INSTANCE, 1, 
					entityPlayer.world, (int)entityPlayer.posX, (int)entityPlayer.posY, (int)entityPlayer.posZ);
		//	entityPlayer.addChatComponentMessage(new ChatComponentText("aaa"));
			}
		}
        
        if (message.key == 20) {
        	NBTTagCompound nbt = entityPlayer.getEntityData();
        	nbt.setInteger("dollcount", message.count);
        }
        if (message.key == 330) {
        	NBTTagCompound nbt = entityPlayer.getEntityData();
        	nbt.setInteger("jumpcount", message.fre + 1);
			nbt.setInteger("jumpcootime", 0);
			//String dd = String.format("%1$3d", (int)message.fre + 1);
			//System.out.println(String.format(dd));
        }
        if (message.key == 331) {
        	NBTTagCompound nbt = entityPlayer.getEntityData();
        	nbt.setInteger("jumpcount", 0);
        	nbt.setInteger("jumpcootime", 0);
        	//System.out.println(String.format("set0"));
        }
        if(message.key == 51) {
        	{
        		ItemStack head = entityPlayer.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
        		if(!head.isEmpty()) {
            		NBTTagCompound nbt = head.getTagCompound();
            		if(nbt != null) {
            			int squadid = nbt.getInteger("squadid");
            			{
            				if(squadid < 4) {
                				++squadid;
                			}else {
                				squadid = 1;
                			}
                			nbt.setInteger("squadid", squadid);
                			
                			String id = "First";
                			if(squadid == 2)id = "Second";
                			if(squadid == 3)id = "Third";
                			if(squadid == 4)id = "Fourth";
                			
            			}
            		}
        		}
        	}
        }
        
        if (message.key == 21) {
			{
	    		{
	    			List<Entity> llist = entityPlayer.world.getEntitiesWithinAABBExcludingEntity(entityPlayer,
	        				entityPlayer.getEntityBoundingBox()
	        						.expand(entityPlayer.motionX, entityPlayer.motionY, entityPlayer.motionZ)
	        						.grow(100.0D));
	        		if(entityPlayer != null){
	        			if (llist != null) {
	        				for (int lj = 0; lj < llist.size(); lj++) {
	        					Entity entity1 = (Entity) llist.get(lj);
	        					if (entity1.canBeCollidedWith()) {
	        						if (entity1 != null)
	        						{
	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
	        	                        {
	        								EntityDoll doll = (EntityDoll) entity1;
	        								if (doll.isOwner(entityPlayer) && entity1 ==  entityPlayer.world.getEntityByID(message.fre)) 
	        								{
	        									if(doll.getMoveT() == 0) {
	        										doll.setMoveT(3);
	        										//entityPlayer.sendMessage(new TextComponentTranslation("hmggirlfront.order.stay.name", new Object[0]));
	        									}
	        									else if(doll.getMoveT() == 1) {
	        										doll.setMoveT(0);
	        										//entityPlayer.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
	        									}
	        									else if(doll.getMoveT() == 3) {
	        										doll.setMoveT(1);
	        										//entityPlayer.sendMessage(new TextComponentTranslation("hmggirlfront.order.free.name", new Object[0]));
	        									}
	        									else if(doll.getMoveT() == 2) {
	        										doll.setMoveT(0);
	        										//entityPlayer.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
	        									}
	            							}
	        	                        }
	        						}
	        					}
	        				}
	        			}
	        		}
	    		}
			}
        }
        if (message.key == 22) {
			{
				if(entityPlayer != null){
	    			List<Entity> llist = entityPlayer.world.getEntitiesWithinAABBExcludingEntity(entityPlayer,
	        				entityPlayer.getEntityBoundingBox()
	        						.expand(entityPlayer.motionX, entityPlayer.motionY, entityPlayer.motionZ)
	        						.grow(100.0D));
	        		{
	        			if (llist != null) {
	        				for (int lj = 0; lj < llist.size(); lj++) {
	        					Entity entity1 = (Entity) llist.get(lj);
	        					if (entity1.canBeCollidedWith()) {
	        						if (entity1 != null)
	        						{
	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
	        	                        {
	        								EntityDoll doll = (EntityDoll) entity1;
	        								if (doll.isOwner(entityPlayer) && entity1 ==  entityPlayer.world.getEntityByID(message.fre)) 
	        								{
	        									Vec3d lock = Vec3d.fromPitchYaw(entityPlayer.rotationPitch, entityPlayer.rotationYaw);
												int ix = 0;
												int iy = 0;
												int iz = 0;
												for(int x = 0; x < 60; ++x) {
													if (entityPlayer.world
															.getBlockState(new BlockPos(entityPlayer.posX + lock.x * x,
																	entityPlayer.posY + 1.5 + lock.y * x,
																	entityPlayer.posZ + lock.z * x))
															.getBlock() != Blocks.AIR) {
														ix = (int) (entityPlayer.posX + lock.x * x);
														iy = (int) (entityPlayer.posY + 1.5 + lock.y * x);
														iz = (int) (entityPlayer.posZ + lock.z * x);

														break;
													}
												}
												if(ix != 0 && iz != 0 && iy != 0) {
													doll.setMoveX(ix);
													doll.setMoveY(iy);
													doll.setMoveZ(iz);
													doll.setMoveT(2);
												//	entityPlayer.sendMessage(new TextComponentTranslation("hmggirlfront.order.pointattack.name", new Object[0]));
													if (!doll.world.isRemote) {
														if(doll.world.getBlockState(new BlockPos(ix, iy + 1, iz)).getBlock() == Blocks.AIR
																|| doll.world.getBlockState(new BlockPos(ix, iy + 1, iz)).getBlock() == mod_GirlFront.bdoll_point){
															doll.world.setBlockState(new BlockPos(ix, iy + 1, iz), mod_GirlFront.bdoll_point.getDefaultState(), 2);
															
														}else {
															boolean flag = false;
															for(int x1 = 0; x1 < 3; ++x1) {
																for(int z1 = 0; z1 < 3; ++z1) {
																	if((doll.world.getBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1)).getBlock() == Blocks.AIR
																			|| doll.world.getBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1)).getBlock() == mod_GirlFront.bdoll_point) 
																			&& !flag){
																		doll.world.setBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1), mod_GirlFront.bdoll_point.getDefaultState(), 2);
																		
																		flag = true;
																		break;
																	}
																}
																if(flag) {
																	break;
																}
															}
														}
														//doll.world.setBlockState(new BlockPos(ix, iy + 1, iz), mod_GirlFront.bdoll_point.getDefaultState(), 2);
														/*boolean flag = false;
														for(int y1 = 0; y1 < 3; ++y1) {
															for(int x1 = 0; x1 < 3; ++x1) {
																for(int z1 = 0; z1 < 3; ++z1) {
																	if(doll.world.getBlockState(new BlockPos(ix + x1 - 1, iy + y1 - 1, iz + z1 - 1)).getBlock() == Blocks.AIR){
																		doll.world.setBlockState(new BlockPos(ix + x1 - 1, iy + y1 - 1, iz + z1 - 1), mod_GirlFront.bdoll_point.getDefaultState(), 2);
																		flag = true;
																		break;
																	}
																}
																if(flag) {
																	break;
																}
															}
															if(flag) {
																break;
															}
														}*/
													}
												}
	            							}
	        	                        }
	        						}
	        					}
	        				}
	        			}
	        		}
	    		}
			}
        }
        
        if (message.key == 1011) {
			ItemStack itemstack = ((EntityPlayer)(entityPlayer)).getHeldItemMainhand();
			if(!itemstack.isEmpty() && itemstack.getItem() instanceof ItemDollCore){
				NBTTagCompound nbt = itemstack.getTagCompound();
	    		String id = nbt.getString("name_id");
	    		
	    		{
	    			List<Entity> llist = entityPlayer.world.getEntitiesWithinAABBExcludingEntity(entityPlayer,
	        				entityPlayer.getEntityBoundingBox()
	        						.expand(entityPlayer.motionX, entityPlayer.motionY, entityPlayer.motionZ)
	        						.grow(120.0D));
	        		if(entityPlayer != null){
	        			if (llist != null) {
	        				for (int lj = 0; lj < llist.size(); lj++) {
	        					Entity entity1 = (Entity) llist.get(lj);
	        					if (entity1.canBeCollidedWith()) {
	        						if (entity1 != null)
	        						{
	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
	        	                        {
	        								EntityDoll pmc = (EntityDoll) entity1;
	        								if (pmc.isOwner(entityPlayer)) 
	        								{
	        									Vec3d lock = Vec3d.fromPitchYaw(entityPlayer.rotationPitch, entityPlayer.rotationYaw);
												int ix = 0;
												int iy = 0;
												int iz = 0;
												for(int x = 0; x < 60; ++x) {
	
															if(entityPlayer.world.getBlockState(new BlockPos(entityPlayer.posX +lock.x * x,  entityPlayer.posY + 1.5 +lock.y * x, entityPlayer.posZ +lock.z * x)).getBlock()
																	!= Blocks.AIR) {
																ix = (int) (entityPlayer.posX +lock.x * x);
																iy = (int) (entityPlayer.posY + 1.5 +lock.y * x);
																iz = (int) (entityPlayer.posZ +lock.z * x);
																{
																	double d2 = entityPlayer.world.rand.nextGaussian() * 0.02D;
													                double d0 = entityPlayer.world.rand.nextGaussian() * 0.02D;
													                double d1 = entityPlayer.world.rand.nextGaussian() * 0.02D;
													                entityPlayer.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, 
													                		entityPlayer.posX +lock.x * x,  entityPlayer.posY + 1.5 +lock.y * x, entityPlayer.posZ +lock.z * x, d2, d0, d1);
																}
																break;
													}
												}
												if(ix != 0 && iz != 0 && iy != 0) {
													pmc.setMoveX(ix);
													pmc.setMoveY(iy);
													pmc.setMoveZ(iz);
													pmc.setMoveT(5);
												}
	            								break;
	            							}
	        	                        }
	        						}
	        					}
	        				}
	        			}
	        		}
	    		}
			}
		}
        if (message.key == 1012) {
			//ItemStack itemstack = ((EntityPlayer)(entityPlayer)).getHeldItemMainhand();
			//if(!itemstack.isEmpty() && itemstack.getItem() instanceof ItemSquad)
			{
	    		{
	    			List<Entity> llist = entityPlayer.world.getEntitiesWithinAABBExcludingEntity(entityPlayer,
	        				entityPlayer.getEntityBoundingBox()
	        						.expand(entityPlayer.motionX, entityPlayer.motionY, entityPlayer.motionZ)
	        						.grow(120.0D));
	        		if(entityPlayer != null){
	        			if (llist != null) {
	        				for (int lj = 0; lj < llist.size(); lj++) {
	        					Entity entity1 = (Entity) llist.get(lj);
	        					if (entity1.canBeCollidedWith()) {
	        						if (entity1 != null)
	        						{
	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
	        	                        {
	        								EntityDoll pmc = (EntityDoll) entity1;
	        								if (pmc.isOwner(entityPlayer)) 
	        								{
	        									Vec3d lock = Vec3d.fromPitchYaw(entityPlayer.rotationPitch, entityPlayer.rotationYaw);
												int ix = 0;
												int iy = 0;
												int iz = 0;
												for(int x = 0; x < 60; ++x) {
													if (entityPlayer.world
															.getBlockState(new BlockPos(entityPlayer.posX + lock.x * x,
																	entityPlayer.posY + 1.5 + lock.y * x,
																	entityPlayer.posZ + lock.z * x))
															.getBlock() != Blocks.AIR) {
														ix = (int) (entityPlayer.posX + lock.x * x);
														iy = (int) (entityPlayer.posY + 1.5 + lock.y * x);
														iz = (int) (entityPlayer.posZ + lock.z * x);

														break;
													}
												}
												if(ix != 0 && iz != 0 && iy != 0) {
													pmc.setMoveX(ix);
													pmc.setMoveY(iy);
													pmc.setMoveZ(iz);
													pmc.setMoveT(5);
												}
	            							}
	        	                        }
	        						}
	        					}
	        				}
	        			}
	        		}
	    		}
			}
		}
        
		if (message.key == 401) {
			Block block = entityPlayer.world.getBlockState(new BlockPos(message.posx, message.posy, message.posz))
					.getBlock();
			if (block instanceof BlockM_Crafting) {
				TileEntity tileentity = entityPlayer.world
						.getTileEntity(new BlockPos(message.posx, message.posy, message.posz));
				if (tileentity instanceof TileEntityM_Crafting) {
					TileEntityM_Crafting tile = (TileEntityM_Crafting) tileentity;
					if (tile.gui_id < 6) {
						++tile.gui_id;
					} else {
						tile.gui_id = 0;
					}
				}
			}
		}
		if (message.key == 404) {
			InventoryPlayer playerInventory = entityPlayer.inventory;
			Block block = entityPlayer.world.getBlockState(new BlockPos(message.posx, message.posy, message.posz))
					.getBlock();
			if (block instanceof BlockM_Crafting) {
				TileEntity tileentity = entityPlayer.world
						.getTileEntity(new BlockPos(message.posx, message.posy, message.posz));
				if (tileentity instanceof TileEntityM_Crafting) {
					TileEntityM_Crafting tile = (TileEntityM_Crafting) tileentity;
					 int hp = 1;
		    		 int bullet = 1;
		    		 int food = 1;
		    		 int parts = 1;
		    		 if (tile.gui_id == 1) {
		    			 hp = 10;
		        		 bullet = 10;
		        		 food = 10;
		        		 parts = 1;
		    		 }
		    		 if (tile.gui_id == 2) {
		    			 hp = 10;
		        		 bullet = 10;
		        		 food = 10;
		        		 parts = 10;
		    		 }
		    		 if (tile.gui_id == 3) {
		    			 hp = 10;
		        		 bullet = 10;
		        		 food = 10;
		        		 parts = 22;
		    		 }
		    		 if (tile.gui_id == 4) {
		    			 hp = 41;
		        		 bullet = 51;
		        		 food = 1;
		        		 parts = 1;
		    		 }
		    		 if (tile.gui_id == 5) {
		    			 hp = 41;
		        		 bullet = 31;
		        		 food = 1;
		        		 parts = 1;
		    		 }
		    		 if (tile.gui_id == 6) {
		    			 hp = 41;
		        		 bullet = 51;
		        		 food = 1;
		        		 parts = 64;
		    		 }
		    		 for(int is = 0; is < playerInventory.mainInventory.size(); ++is){
		    			 ItemStack itemi = playerInventory.mainInventory.get(is);
		    			 if(!itemi.isEmpty()) {
		    				 if(itemi.getItem() == mod_GirlFront.idoll_hp) {
		    					 if(tile.furnaceItemStacks.get(0).getCount() < hp && tile.furnaceItemStacks.get(0).getCount() == 0) {
		    						 if(itemi.getCount() < hp) {
		    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
		    							 tile.furnaceItemStacks.set(0, itemi);
		    						 }else {
		    							 itemi.shrink(hp);
		    							 tile.furnaceItemStacks.set(0, new ItemStack(mod_GirlFront.idoll_hp, hp));
		    						 }
		    					 }
		    				 }
		    				 if(itemi.getItem() == mod_GirlFront.idoll_bullet) {
		    					 if(tile.furnaceItemStacks.get(1).getCount() < bullet && tile.furnaceItemStacks.get(1).getCount() == 0) {
		    						 if(itemi.getCount() < bullet) {
		    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
		    							 tile.furnaceItemStacks.set(1, itemi);
		    						 }else {
		    							 itemi.shrink(bullet);
		    							 tile.furnaceItemStacks.set(1, new ItemStack(mod_GirlFront.idoll_bullet, bullet));
		    						 }
		    					 }
		    				 }
		    				 if(itemi.getItem() == mod_GirlFront.idoll_food) {
		    					 if(tile.furnaceItemStacks.get(2).getCount() < food && tile.furnaceItemStacks.get(2).getCount() == 0) {
		    						 if(itemi.getCount() < food) {
		    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
		    							 tile.furnaceItemStacks.set(2, itemi);
		    						 }else {
		    							 itemi.shrink(food);
		    							 tile.furnaceItemStacks.set(2, new ItemStack(mod_GirlFront.idoll_food, food));
		    						 }
		    					 }
		    				 }
		    				 if(itemi.getItem() == mod_GirlFront.idoll_parts) {
		    					 if(tile.furnaceItemStacks.get(3).getCount() < parts && tile.furnaceItemStacks.get(3).getCount() == 0) {
		    						 if(itemi.getCount() < parts) {
		    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
		    							 tile.furnaceItemStacks.set(3, itemi);
		    						 }else {
		    							 itemi.shrink(parts);
		    							 tile.furnaceItemStacks.set(3, new ItemStack(mod_GirlFront.idoll_parts, parts));
		    						 }
		    					 }
		    				 }
		    			 }
		    		 }
				}
			}
		}
		 if(message.key == 408){
	        	EntityLivingBase en = (EntityLivingBase) entityPlayer.world.getEntityByID(message.fre);
	        	if (en != null && en instanceof EntityDoll) {// 1
	        		EntityDoll doll = (EntityDoll) en;
	        		 if(doll.doll_skill_can) {
	        			 doll.doll_skill_can = false;
	        		 }else {
	        			 doll.doll_skill_can = true;
	        		 }
	        	}
	        }
		 if (message.key == 411) {
				Block block = entityPlayer.world.getBlockState(new BlockPos(message.posx, message.posy, message.posz))
						.getBlock();
				if (block instanceof BlockM_Restoring) {
					TileEntity tileentity = entityPlayer.world
							.getTileEntity(new BlockPos(message.posx, message.posy, message.posz));
					if (tileentity instanceof TileEntityM_Restoring) {
						TileEntityM_Restoring tile = (TileEntityM_Restoring) tileentity;
						tile.go_craft = true;
					}
				}
			}
		 
		 
		 
		 if (message.key == 2020) {
			 if(entityPlayer != null) {
				 BlockPos spawn = entityPlayer.world.getSpawnPoint();
				 BlockPos bp = entityPlayer.world.getHeight(new BlockPos(spawn.getX(), 60 + entityPlayer.world.rand.nextInt(100), spawn.getZ()));
					int genY = bp.getY(); //world.getChunksLowestHorizon(genX, genZ);
				 entityPlayer.world.setBlockState(new BlockPos(spawn.getX() + 0, genY + 0, spawn.getZ() + 0),
							mod_GirlFront.bdoll_supplies.getDefaultState(), 2);
			 }
			 
	     }
		 
        return null;
    }
}