package hmggirlfront.block.tile;
 
import java.util.List;

import gvclib.network.GVCLClientMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.entity.mob.EntityIBCBase;
import hmggirlfront.items.ItemMissonCard;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
 
public class TileEntity_BaseBase extends TileEntity implements ITickable
{
	public int count = 0;
	public int invcount = 0;
	
	public int invmaxcount = 200;
	
	public boolean enemyblock = false;
	
	public Block invclearblock = Blocks.AIR;
	
	public String inv_tex = "inv_m.png";
	
	public String inv_name = "";
	
	public int player_count = 0;
	public int mob_count = 0;
	
	public boolean base_boss = false;
	
	public boolean enemy = false;
	
	public boolean world_level = true;
    
	@SideOnly(Side.CLIENT)
    public double getMaxRenderDistanceSquared()
    {
        return 65536.0D;
    }
	
	@SideOnly(Side.CLIENT)
    public static int getClientInvCount(TileEntity_BaseBase inv)
    {
        return inv.invcount;
    }
	@SideOnly(Side.CLIENT)
    public static int getClientInvMaxCount(TileEntity_BaseBase inv)
    {
        return inv.invmaxcount;
    }
	@SideOnly(Side.CLIENT)
    public static int getClientPlayerCount(TileEntity_BaseBase inv)
    {
        return inv.player_count;
    }
	@SideOnly(Side.CLIENT)
    public static int getClientMobCount(TileEntity_BaseBase inv)
    {
        return inv.mob_count;
    }
	
    @Override
	public void update() {
    	if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
			for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
            {
				GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(100, invcount,
						this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()), player);
		    	GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(101, invmaxcount,
						this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()), player);
		    	GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(102, player_count,
						this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()), player);
		    	GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(103, mob_count,
						this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()), player);
            }
		}
    	
    	
    	Block block = this.world.getBlockState(this.pos).getBlock();
        Entity entity = null;
        int k = this.pos.getX();
        int l = this.pos.getY();
        int i1 = this.pos.getZ();
        int x = this.pos.getX();
        int y= this.pos.getY();
        int z = this.pos.getZ();
        
        
        /*++count;
        if(count > 1000) {
			if (this.canspawn(x, y, z, 20, 20) && !this.world.isRemote) {
			}
			count = 0;
        }*/
        if(enemyblock) {
        	if(world_level)this.getInvCount(x, y, z, 20);
        }else {
        	if(world_level)this.getInvCountPlayer(x, y, z, 20);
        }
        
        if(invcount >= invmaxcount) {
        	if(this.enemyblock) {
        		//this.sendmessage(x, y, z, 20, "Retrieve base!", true);
        		this.sendmessage(x, y, z, 20, "hmggirlfront.base.retrieve.name", true);
        		this.Invation(pos);
        	}else {
        		//this.sendmessage(x, y, z, 20, "The base was taken!", false);
        		this.sendmessage(x, y, z, 20, "hmggirlfront.base.invation.name", false);
        	}
        	if(!this.world.isRemote){
            	//this.world.setBlockToAir(pos);
        		this.world.setBlockState(pos, invclearblock.getDefaultState(), 2);
            }
        }
        
	}
    
    protected void Invation(BlockPos pos) {
    	
    }
    
    public void getInvCount(double x, double y, double z, int han) {
    	Entity entity = null;
    	int playercount = 0;
    	int mobcount = 0;
    	{
    		AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
    				(double)(x + han), (double)(y + han), (double)(z+ han)))
            		.grow(1);
            List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
            if(llist2!=null){
                for (int lj = 0; lj < llist2.size(); lj++) {
                	
                	Entity entity1 = (Entity)llist2.get(lj);
                	if (entity1.canBeCollidedWith() && entity1 != null)
                    {
                		if (entity1 instanceof EntityPlayer) {
                			EntityPlayer ss = (EntityPlayer) entity1;
                			ItemStack itemstack = ss.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
                    		if(!itemstack.isEmpty() && itemstack.getItem() == Items.GOLDEN_HELMET){
                    		}else {
                    			++playercount;
                    		}
    					}
                		if (entity1 instanceof EntityDoll) {
    						++playercount;
    					}
                		if (entity1 instanceof EntityTameable) {
                			EntityTameable maid = (EntityTameable) entity1;
                			if(maid.isTamed()) 
                			{
                				++playercount;
                			}
    					}
                		if (entity1 instanceof IMob && entity1 instanceof EntityIBCBase) {
    						++mobcount;
    					}
                    }
                }
            }
    	}
    	if(playercount > mobcount) {
    		//invcount = invcount + 2;
    		++invcount;
    	}else if(playercount == mobcount){
    		
    	}else if(invcount > 0){
    		--invcount;
    	}
    	this.player_count = playercount;
    	this.mob_count = mobcount;
    }
    
    public void getInvCountPlayer(double x, double y, double z, int han) {
    	Entity entity = null;
    	int playercount = 0;
    	int mobcount = 0;
    	{
    		AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
    				(double)(x + han), (double)(y + han), (double)(z+ han)))
            		.grow(1);
            List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
            if(llist2!=null){
                for (int lj = 0; lj < llist2.size(); lj++) {
                	
                	Entity entity1 = (Entity)llist2.get(lj);
                	if (entity1.canBeCollidedWith() && entity1 != null)
                    {
                		if (entity1 instanceof EntityPlayer || entity1 instanceof EntityDoll) {
    						++playercount;
    					}
                		if (entity1 instanceof EntityTameable) {
                			EntityTameable maid = (EntityTameable) entity1;
                			if(maid.isTamed()) 
                			{
                				++playercount;
                			}
    					}
                		if (entity1 instanceof IMob && entity1 instanceof EntityIBCBase) {
    						++mobcount;
    					}
                    }
                }
            }
    	}
    	if(mobcount > playercount) {
    		//invcount = invcount + 2;
    		++invcount;
    	}else if(playercount == mobcount){
    		
    	}else if(invcount > 0){
    		--invcount;
    	}
    	this.player_count = playercount;
    	this.mob_count = mobcount;
    }
    
        public boolean canspawn(double x, double y, double z, int han) {
        	boolean spawn = false;
        	Entity entity = null;
        	int mobkazu = 0;
        	int mobkazu2 = 0;
        	{
        		AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), (double)(x + han), (double)(y + han), (double)(z+ han)))
                		.grow(1);
                List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
                if(llist2!=null){
                    for (int lj = 0; lj < llist2.size(); lj++) {
                    	
                    	Entity entity1 = (Entity)llist2.get(lj);
                    	if (entity1.canBeCollidedWith())
                        {
                    		if (entity1 instanceof EntityLivingBase && entity1 != null) {
        						++mobkazu;
        						if (entity1 instanceof IMob) {
        							++mobkazu2;
        						}
        					}
                        }
                    }
                }
        	}
        	if(mobkazu <= 20 && mobkazu2 <= 4) {
        		spawn = true;
        	}
        	return spawn;
        }
        
        public void sendmessage(double x, double y, double z, int han, String message, boolean mission) {
        	boolean spawn = false;
        	Entity entity = null;
        	int mobkazu = 0;
        	int mobkazu2 = 0;
        	{
        		AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
        				(double)(x + han), (double)(y + han), (double)(z+ han)))
                		.grow(1);
                List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
                if(llist2!=null){
                    for (int lj = 0; lj < llist2.size(); lj++) {
                    	
                    	Entity entity1 = (Entity)llist2.get(lj);
                    	if (entity1.canBeCollidedWith())
                        {
                    		if (entity1 != null && entity1 instanceof EntityPlayer) {
                    			EntityPlayer player = (EntityPlayer) entity1;
                    			if(this.world.isRemote) {
									player.sendMessage(new TextComponentTranslation(message, new Object[0]));
								}
                    			if(mission){
                    				InventoryPlayer playerInv = player.inventory;
                        			for(int is = 0; is < playerInv.mainInventory.size(); ++is){
                        				ItemStack itemi = playerInv.mainInventory.get(is);
                        				if(!itemi.isEmpty() && itemi.getItem() instanceof ItemMissonCard) {
                        					ItemMissonCard mcard = (ItemMissonCard) itemi.getItem();
                        					if(mcard.id == 4 && !base_boss) {
                        						NBTTagCompound nbt = itemi.getTagCompound();
                        						if(nbt != null){
                        							int d = nbt.getInteger("destroyed");
                        							++d;
    					            				nbt.setInteger("destroyed", d);
    					            				player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
                        						}
                        					}
                        					if(mcard.id == 5 && base_boss) {
                        						NBTTagCompound nbt = itemi.getTagCompound();
                        						if(nbt != null){
                        							int d = nbt.getInteger("destroyed");
                        							++d;
    					            				nbt.setInteger("destroyed", d);
    					            				player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
                        						}
                        					}
                        				}
                        			}
                    			}
                    			
        					}
                        }
                    }
                }
        	}
        }
        
}