package common.block;

import java.util.List;

import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.ItemRegistry;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.Transforms;
import common.properties.IProperty;
import common.properties.PropertyInteger;
import common.rng.Random;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityDaylightDetector;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Facing;
import common.world.IWorldAccess;
import common.world.LightType;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockDaylightDetector extends BlockContainer
{
    public static final PropertyInteger POWER = PropertyInteger.create("power", 0, 15);
    private final boolean inverted;

    public BlockDaylightDetector(boolean inverted)
    {
        super(Material.wood);
        this.inverted = inverted;
        this.setDefaultState(this.getBaseState().withProperty(POWER, Integer.valueOf(0)));
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.375F, 1.0F);
        this.setTab(CheatTab.tabTech);
        this.setHardness(0.2F);
        this.setStepSound(SoundType.WOOD);
//        this.setDisplay("daylightDetector");
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.375F, 1.0F);
    }

    public int getWeakPower(IWorldAccess worldIn, BlockPos pos, State state, Facing side)
    {
        return ((Integer)state.getValue(POWER)).intValue();
    }

    public void updatePower(AWorldServer worldIn, BlockPos pos)
    {
        if (!worldIn.dimension.hasNoLight())
        {
            State iblockstate = worldIn.getState(pos);
            int i = worldIn.getLightFor(LightType.SKY, pos) - worldIn.getSkylightSubtracted();
            float f = worldIn.getCelestialAngleRadians(1.0F);
            float f1 = f < (float)Math.PI ? 0.0F : ((float)Math.PI * 2F);
            f = f + (f1 - f) * 0.2F;
            i = Math.round((float)i * ExtMath.cos(f));
            i = ExtMath.clampi(i, 0, 15);

            if (this.inverted)
            {
                i = 15 - i;
            }

            if (((Integer)iblockstate.getValue(POWER)).intValue() != i)
            {
                worldIn.setState(pos, iblockstate.withProperty(POWER, Integer.valueOf(i)), 3);
            }
        }
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
//        if (playerIn.isAllowEdit())
//        {
        if (worldIn.client)
        {
            return true;
        }
        else
        {
            if (this.inverted)
            {
                worldIn.setState(pos, Blocks.daylight_detector.getState().withProperty(POWER, state.getValue(POWER)), 4);
                Blocks.daylight_detector.updatePower((AWorldServer)worldIn, pos);
            }
            else
            {
                worldIn.setState(pos, Blocks.daylight_detector_inverted.getState().withProperty(POWER, state.getValue(POWER)), 4);
                Blocks.daylight_detector_inverted.updatePower((AWorldServer)worldIn, pos);
            }

            return true;
        }
//        }
//        else
//        {
//            return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);
//        }
    }

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

    public Item getItem(World worldIn, BlockPos pos)
    {
        return ItemRegistry.getItemFromBlock(Blocks.daylight_detector);
    }

    public boolean isFullCube()
    {
        return false;
    }

    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    /**
     * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
     */
    public int getRenderType()
    {
        return 3;
    }

    /**
     * Can this block provide power. Only wire currently seems to have this change based on its state.
     */
    public boolean canProvidePower()
    {
        return true;
    }

    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new TileEntityDaylightDetector();
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(POWER, Integer.valueOf(meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Integer)state.getValue(POWER)).intValue();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {POWER};
    }

    /**
     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
     */
    public void getSubBlocks(Item itemIn, CheatTab tab, List<ItemStack> list)
    {
        if (!this.inverted)
        {
            super.getSubBlocks(itemIn, tab, list);
        }
    }

    public Transforms getTransform() {
    	return Transforms.FLAT;
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel(this.inverted ? "daylight_detector_inverted_top" : "daylight_detector_top")
			.add(0, 0, 0, 16, 6, 16)
				.d("daylight_detector_side").uv(0, 0, 16, 16)
				.u().uv(0, 0, 16, 16).noCull()
				.n("daylight_detector_side").uv(0, 10, 16, 16)
				.s("daylight_detector_side").uv(0, 10, 16, 16)
				.w("daylight_detector_side").uv(0, 10, 16, 16)
				.e("daylight_detector_side").uv(0, 10, 16, 16);
    }
}
