package bunyan.api;

import java.util.ArrayList;
import net.minecraft.server.Block;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.IBlockAccess;
import net.minecraft.server.ItemStack;
import net.minecraft.server.Material;
import net.minecraft.server.MathHelper;
import net.minecraft.server.World;

public abstract class DirectionalBlock extends Block
{
    public static int getCompositeDataAndFacing(int var0, Direction var1)
    {
        return (var1.getValue() - 2 << 2) + var0;
    }

    public static int getDataFromMetadata(int var0)
    {
        return var0 & 3;
    }

    public static Direction getFacing(IBlockAccess var0, int var1, int var2, int var3)
    {
        return getFacingFromMetadata(var0.getData(var1, var2, var3));
    }

    public static Direction getFacingFromMetadata(int var0)
    {
        return Direction.fromValue(((var0 & 12) >> 2) + 2);
    }

    public static void setData(World var0, int var1, int var2, int var3, int var4, boolean var5)
    {
        Direction var6 = getFacingFromMetadata(var0.getData(var1, var2, var3));
        setDataAndFacing(var0, var1, var2, var3, var4, var6, var5);
    }

    public static void setDataAndFacing(World var0, int var1, int var2, int var3, int var4, Direction var5, boolean var6)
    {
        var0.setRawData(var1, var2, var3, getCompositeDataAndFacing(var4, var5));

        if (var6)
        {
            int var7 = var0.getTypeId(var1, var2, var3);

            if (Block.r[var7 & 4095])
            {
                var0.update(var1, var2, var3, var7);
            }
            else
            {
                var0.applyPhysics(var1, var2, var3, var7);
            }
        }
    }

    public static void setFacing(World var0, int var1, int var2, int var3, Direction var4, boolean var5)
    {
        int var6 = getDataFromMetadata(var0.getData(var1, var2, var3));
        setDataAndFacing(var0, var1, var2, var3, var6, var4, var5);
    }

    protected DirectionalBlock(int var1, int var2, Material var3)
    {
        super(var1, var2, var3);
    }

    /**
     * Determines the damage on the item the block drops. Used in cloth and wood.
     */
    protected int getDropData(int var1)
    {
        return getDataFromMetadata(var1);
    }

    /**
     * Spawns EntityItem in the world for the given ItemStack if the world is not remote.
     */
    protected void a(World var1, int var2, int var3, int var4, ItemStack var5)
    {
        var5.setData(getDataFromMetadata(var5.getData()));
        super.a(var1, var2, var3, var4, var5);
    }

    public ArrayList getBlockDropped(World var1, int var2, int var3, int var4, int var5, int var6)
    {
        return super.getBlockDropped(var1, var2, var3, var4, var5 == 0 ? var1.getData(var2, var3, var4) : var5, var6);
    }

    /**
     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
     */
    public int a(int var1, int var2)
    {
        return this.textureId + this.getTextureOffsetFromFacingSideAndMetadata(Direction.NORTH, Direction.fromValue(var1), var2);
    }

    public abstract int getTextureOffsetFromFacingSideAndMetadata(Direction var1, Direction var2, int var3);

    /**
     * Called when the block is placed in the world.
     */
    public void postPlace(World var1, int var2, int var3, int var4, EntityLiving var5)
    {
        Direction var6 = Direction.NORTH;

        if (var5 != null)
        {
            int var7 = MathHelper.floor((double)(var5.yaw * 4.0F / 360.0F) + 0.5D) & 3;

            switch (var7)
            {
                case 0:
                    var6 = Direction.NORTH;
                    break;

                case 1:
                    var6 = Direction.EAST;
                    break;

                case 2:
                    var6 = Direction.SOUTH;
                    break;

                case 3:
                    var6 = Direction.WEST;
            }

            setFacing(var1, var2, var3, var4, var6, true);
        }
    }
}
