package ncrashed.warp.cores;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ncrashed.warp.CoreRegistry;
import ncrashed.warp.WarpMod;
import ncrashed.warp.api.IMarker;
import ncrashed.warp.api.IWarpCore;
import ncrashed.warp.api.volume.IVolume;
import ncrashed.warp.api.volume.SimpleVolume;
import ncrashed.warp.client.ClientProxy;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class MarkerBlock extends Block
{
	public MarkerBlock(int id, Material material) 
	{
		super(id, material);
		this.setCreativeTab(CreativeTabs.tabBlock);
	}

	@Override
	@SideOnly(Side.CLIENT)
	public String getTextureFile()
	{
		return ClientProxy.BLOCKS_TEXTURE;
	}

	@Override
	@SideOnly(Side.CLIENT)
    public int getBlockTextureFromSide(int side)
    {
		if(side == 0 || side == 1)
		{
			return 0;
		}
        return 1; 
    }
	
	@Override
    public void onBlockAdded(World world, int x, int y, int z)
    {
		
    }
	
	@Override
	public void onBlockEventReceived(World world, int x, int y, int z, int eventID, int eventParam) 
	{
		super.onBlockEventReceived(world, x, y, z, eventID, eventParam);
		
		TileEntity entity = world.getBlockTileEntity(x, y, z);
		
		if(entity != null)
		{
			entity.receiveClientEvent(eventID, eventParam);
		}
	}

	@Override
	public void onBlockDestroyedByExplosion(World world, int x, int y, int z) 
	{
		Side side = FMLCommonHandler.instance().getEffectiveSide();
		if(side == Side.SERVER)
		{
			IMarker marker = (IMarker)world.getBlockTileEntity(x, y, z);
			
			if(marker == null || !marker.isBinded()) return;		
			
			List<IWarpCore> cores = marker.getBindedCores();
			Iterator<IWarpCore> iter = cores.iterator();
			while(iter.hasNext())
			{
				IWarpCore core = iter.next();
				if(core != null)
				{
					core.getCoreVolume().removeMarker(world, x, y, z, core.getCoreX(), core.getCoreY(), core.getCoreZ());
					core.checkConstruction();
				}
			}
		}
	}
	
	@Override
	public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int meta) 
	{
		Side side = FMLCommonHandler.instance().getEffectiveSide();
		if(side == Side.SERVER)
		{
			IMarker marker = (IMarker)world.getBlockTileEntity(x, y, z);
			
			if(marker == null || !marker.isBinded()) return;
			
			List<IWarpCore> cores = marker.getBindedCores();
			Iterator<IWarpCore> iter = cores.iterator();
			while(iter.hasNext())
			{
				IWarpCore core = iter.next();
				if(core != null)
				{
					core.getCoreVolume().removeMarker(world, x, y, z, core.getCoreX(), core.getCoreY(), core.getCoreZ());
					core.checkConstruction();
				}
			}
		}
	}
	
	@Override
	@SideOnly(Side.CLIENT)
    public void randomDisplayTick(World world, int x, int y, int z, Random random)
    {
		IMarker marker = (IMarker)world.getBlockTileEntity(x, y, z);
		
		if(marker == null || !marker.isBinded()) return;

		
        for (int i = 0; i < 3; ++i)
        {
            int rnd1 = random.nextInt(2) * 2 - 1;
            int rnd2 = random.nextInt(2) * 2 - 1;
            
            double velx = (double)(random.nextFloat() * 1.0F * (float)rnd1);
            double vely = ((double)random.nextFloat() - 0.5D) * 0.125D;
            double velz = (double)(random.nextFloat() * 1.0F * (float)rnd2);
            
            double parx = (double)x + 0.5D + 0.25D * (double)rnd1;
            double pary = (double)((float)y + random.nextFloat());
            double parz = (double)z + 0.5D + 0.25D * (double)rnd2;
            
            world.spawnParticle("portal", parx, pary, parz, velx, vely, velz);
        }
    }
	
	@Override
    public boolean hasTileEntity(int metadata)
    {
        return true;
    }
	
	@Override
	public TileEntity createTileEntity(World world, int metadata)
	{
		return new TileMarker();
    }
}
