package ncrashed.warp.cores;

import java.util.List;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ncrashed.warp.WarpMod;
import ncrashed.warp.api.IWarpCore;
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.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;

public class LegacyCoreBlock extends Block 
{
	public LegacyCoreBlock(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 == 1)
		{
			return 2;
		} else if(side == 0) 
		{
			return 4;
		}
        return 3;
    }
	
	@Override
    public void onBlockAdded(World world, int x, int y, int z)
    {

		
    }
	
	@Override
	public void onNeighborBlockChange(World world, int x, int y, int z, int changedID) 
	{
		Side side = FMLCommonHandler.instance().getEffectiveSide();
		if(changedID == WarpMod.mConfig.mRPLighting && side == Side.SERVER)
		{
			IWarpCore core = (IWarpCore)world.getBlockTileEntity(x, y, z);
			if(core == null) return;
			
			core.activate();
		}
	}
	
	@Override
	public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
	{
		Side side = FMLCommonHandler.instance().getEffectiveSide();
		if(side == Side.SERVER)
		{
			IWarpCore core = (IWarpCore)world.getBlockTileEntity(x, y, z);
			if(core != null)
			{
				if(!player.isSneaking())
				{
					if(core.addPlayerCrew((EntityPlayerMP)player))
					{
						player.attackEntityFrom(DamageSource.generic, core.getDamageOnCrewSuccess());
					} else
					{
						player.attackEntityFrom(DamageSource.generic, core.getDamageOnCrewFail());
					}
				} else
				{
					List<String> crew = core.getCrew();
					player.sendChatToPlayer("Core " + core.getCoreID() + " crew:");
					for(int i = 0; i<crew.size(); ++i)
					{
						player.sendChatToPlayer(i + ": " + crew.get(i));
					}
				}
			}
		}
		
		return false;
	}
	
	@Override
    public boolean hasTileEntity(int metadata)
    {
        return true;
    }
	
	@Override
	public TileEntity createTileEntity(World world, int metadata)
	{
		return new TileLegacyCore(new SimpleVolume(WarpMod.mConfig.mMarkerBlockID), new LegacyCoreStructure(), metadata);
    }
}
