package hmggirlfront.network;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.tile.TileEntityM_Crafting;
import hmggirlfront.block.tile.TileEntity_BaseBase;
import hmggirlfront.block.tile.TileEntity_GKGunRack;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.entity.mob.EntityIBCBase;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
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 GFClientMessageKeyPressedHandler implements IMessageHandler<GFClientMessageKeyPressed, IMessage> {
 
    @Override
    public IMessage onMessage(GFClientMessageKeyPressed message, MessageContext ctx) {
        EntityPlayer player = mod_GirlFront.proxy.getEntityPlayerInstance();
        
        if (message.key == 1) {
        	mod_GirlFront.difficult_level = message.fre;
        }
        if (message.key == 2) {
        	mod_GirlFront.difficult_doll_kazu = message.fre;
        }
        
        if (message.key == 3) {
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_GKGunRack) {
        		TileEntity_GKGunRack tile  = (TileEntity_GKGunRack) player.world.getTileEntity(pos);
				Item item = new Item().getItemById(message.item_id);
				if(item != null) {
					tile.furnaceItemStacks.set(0, new ItemStack(item));
				}
        	}
		}
        if (message.key == 4) {
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_GKGunRack) {
        		TileEntity_GKGunRack tile  = (TileEntity_GKGunRack) player.world.getTileEntity(pos);
				Item item = new Item().getItemById(message.item_id);
				if(item != null) {
					tile.furnaceItemStacks.set(1, new ItemStack(item));
				}
        	}
		}
        if (message.key == 5) {
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_GKGunRack) {
        		TileEntity_GKGunRack tile  = (TileEntity_GKGunRack) player.world.getTileEntity(pos);
				Item item = new Item().getItemById(message.item_id);
				if(item != null) {
					tile.furnaceItemStacks.set(2, new ItemStack(item));
				}
        	}
		}
        
        if (message.key == 330) {
        	NBTTagCompound nbt = player.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 = player.getEntityData();
        	nbt.setInteger("jumpcount", 0);
        	nbt.setInteger("jumpcootime", 0);
        	//System.out.println(String.format("set0"));
        }
        if(message.key == 100){
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_BaseBase) {
        		TileEntity_BaseBase tile  = (TileEntity_BaseBase) player.world.getTileEntity(pos);
				tile.invcount = message.enid;
        	}
    	}
        if(message.key == 101){
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_BaseBase) {
        		TileEntity_BaseBase tile  = (TileEntity_BaseBase) player.world.getTileEntity(pos);
				tile.invmaxcount = message.enid;
        	}
    	}
        if(message.key == 102){
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_BaseBase) {
        		TileEntity_BaseBase tile  = (TileEntity_BaseBase) player.world.getTileEntity(pos);
				tile.player_count = message.enid;
        	}
    	}
        if(message.key == 103){
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntity_BaseBase) {
        		TileEntity_BaseBase tile  = (TileEntity_BaseBase) player.world.getTileEntity(pos);
				tile.mob_count = message.enid;
        	}
    	}
        
        if(message.key == 201){
        	BlockPos pos = new BlockPos(message.posx, message.posy, message.posz);
        	if(pos != null && player.world.getTileEntity(pos) instanceof TileEntityM_Crafting) {
        		TileEntityM_Crafting tile  = (TileEntityM_Crafting) player.world.getTileEntity(pos);
				tile.crafttime = message.enid;
        	}
    	}
        
        if(message.key == 801){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityDoll) {// 1
        		EntityDoll doll = (EntityDoll) en;
        		for (int k = 0; k < 5; ++k) {
					double d2 = doll.world.rand.nextGaussian() * 0.02D;
					double d0 = doll.world.rand.nextGaussian() * 0.02D;
					double d1 = doll.world.rand.nextGaussian() * 0.02D;
					doll.world.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY,
							doll.posX + (double) (doll.world.rand.nextFloat() * doll.width * 2.0F) - (double) doll.width,
							doll.posY + (double) (doll.world.rand.nextFloat() * doll.height),
							doll.posZ + (double) (doll.world.rand.nextFloat() * doll.width * 2.0F) - (double) doll.width,
							d2, d0, d1);
				}
        	}
        }
        if(message.key == 802){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityDoll) {// 1
        		EntityDoll doll = (EntityDoll) en;
        		for (int k = 0; k < 5; ++k) {
					double d2 = doll.world.rand.nextGaussian() * 0.02D;
					double d0 = doll.world.rand.nextGaussian() * 0.02D;
					double d1 = doll.world.rand.nextGaussian() * 0.02D;
					doll.world.spawnParticle(EnumParticleTypes.CRIT_MAGIC,
							doll.posX + (double) (doll.world.rand.nextFloat() * doll.width * 2.0F) - (double) doll.width,
							doll.posY + (double) (doll.world.rand.nextFloat() * doll.height),
							doll.posZ + (double) (doll.world.rand.nextFloat() * doll.width * 2.0F) - (double) doll.width,
							d2, d0, d1);
				}
        	}
        }
        
        
        if(message.key == 30){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityDoll) {// 1
        		EntityDoll doll = (EntityDoll) en;
        		doll.setParachute(1);
        	}
        }
        
        
        
        if(message.key == 500){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityIBCBase) {// 1
        		EntityIBCBase entity = (EntityIBCBase) en;
        		entity.setRemain_L(message.enid);
        	}
        }
        
        if(message.key == 501){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityIBCBase) {// 1
        		EntityIBCBase entity = (EntityIBCBase) en;
        		entity.gun_count1 = message.enid;
        	}
        }
        
        if(message.key == 502){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityIBCBase) {// 1
        		EntityIBCBase entity = (EntityIBCBase) en;
        		entity.cooltime = message.enid;
        	}
        }
        
        if(message.key == 503){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityIBCBase) {// 1
        		EntityIBCBase entity = (EntityIBCBase) en;
        		entity.cooltime2 = message.enid;
        	}
        }
        
        if(message.key == 600){
        	EntityLivingBase en = (EntityLivingBase) player.world.getEntityByID(message.fre);
        	if (en != null && en instanceof EntityIBCBase) {// 1
        		EntityIBCBase entity = (EntityIBCBase) en;
        		entity.anti_bullet_tate = message.enid;
        	}
        }
        
        
		return null;
    }
}