package common.block.tech;

import common.block.Block;
import common.block.Material;
import common.init.Blocks;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
import common.rng.Random;
import common.util.BlockPos;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockRedstoneLight extends Block
{
    private final boolean isOn;

    public BlockRedstoneLight(boolean isOn)
    {
        super(Material.LOOSE);
        this.isOn = isOn;

        if (isOn)
        {
            this.setLightLevel(1.0F);
        }
    }

    public void onBlockAdded(AWorldServer worldIn, BlockPos pos, State state)
    {
        if (!worldIn.client)
        {
            if (this.isOn && !worldIn.isBlockPowered(pos))
            {
                worldIn.setState(pos, Blocks.redstone_lamp.getState(), 2);
            }
            else if (!this.isOn && worldIn.isBlockPowered(pos))
            {
                worldIn.setState(pos, Blocks.lit_redstone_lamp.getState(), 2);
            }
        }
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        if (!worldIn.client)
        {
            if (this.isOn && !worldIn.isBlockPowered(pos))
            {
                worldIn.scheduleUpdate(pos, this, 4);
            }
            else if (!this.isOn && worldIn.isBlockPowered(pos))
            {
                worldIn.setState(pos, Blocks.lit_redstone_lamp.getState(), 2);
            }
        }
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
//        if (!worldIn.client)
//        {
        if (this.isOn && !worldIn.isBlockPowered(pos))
        {
            worldIn.setState(pos, Blocks.redstone_lamp.getState(), 2);
        }
//        }
    }

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return Items.redstone_lamp;
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.redstone_lamp;
    }

    public ItemStack createStackedBlock(State state)
    {
        return new ItemStack(Items.redstone_lamp);
    }
    
    public boolean isMagnetic() {
    	return true;
    }
	
	protected Item getItemToRegister() {
		return this.isOn ? null : super.getItemToRegister();
	}
}
