package common.block;

import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.inventory.Container;
import common.inventory.InventoryHelper;
import common.item.CheatTab;
import common.material.Material;
import common.properties.IProperty;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityMachine;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public abstract class BlockMachine extends BlockDirectional implements ITileEntityProvider {
	public BlockMachine() {
		super(Material.iron);
        this.hasTile = true;
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH));
		this.setTab(CheatTab.tabTech);
	}

	public abstract TileEntity createNewTileEntity(World worldIn, int meta);

	public void onBlockAdded(AWorldServer worldIn, BlockPos pos, State state) {
		this.updateState(worldIn, pos, state);
	}

	public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY,
			float hitZ) {
		if(worldIn.client)
			return true;
		TileEntity tileentity = worldIn.getTileEntity(pos);
		if(tileentity instanceof TileEntityMachine) {
			playerIn.displayGUIChest((TileEntityMachine)tileentity);
//                playerIn.triggerAchievement(StatList.hopperStat);
		}
		return true;
	}

	public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock) {
		this.updateState(worldIn, pos, state);
	}

	private void updateState(World worldIn, BlockPos pos, State state) {
//        boolean flag = !worldIn.isBlockPowered(pos);
//
//        if (flag != ((Boolean)state.getValue(ENABLED)).booleanValue())
//        {
//            worldIn.setBlockState(pos, state.withProperty(ENABLED, Boolean.valueOf(flag)), 4);
//        }
	}

	public void onBlockRemoved(AWorldServer worldIn, BlockPos pos, State state) {
		TileEntity tileentity = worldIn.getTileEntity(pos);
		if(tileentity instanceof TileEntityMachine) {
			InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityMachine)tileentity);
			worldIn.updateComparatorOutputLevel(pos, this);
		}
		super.onBlockRemoved(worldIn, pos, state);
		worldIn.removeTileEntity(pos);
	}

	public boolean hasComparatorInputOverride() {
		return true;
	}

	public int getComparatorInputOverride(World worldIn, BlockPos pos) {
		return Container.calcRedstone(worldIn.getTileEntity(pos));
	}

	public boolean onBlockEventReceived(World worldIn, BlockPos pos, State state, int eventID, int eventParam) {
		super.onBlockEventReceived(worldIn, pos, state, eventID, eventParam);
		TileEntity tileentity = worldIn.getTileEntity(pos);
		return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
	}

	public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, int meta,
			EntityLiving placer) {
		return this.getState().withProperty(FACING, placer.getHorizontalFacing());
	}

	public State getStateFromMeta(int meta) {
		return this.getState().withProperty(FACING, Facing.getHorizontal(meta));
	}

	public int getMetaFromState(State state) {
		return ((Facing)state.getValue(FACING)).getHorizontalIndex();
	}

	protected IProperty[] getProperties() {
		return new IProperty[] {FACING};
	}
    
    public boolean isMagnetic() {
    	return true;
    }
}
