package common.item.spawner;

import common.block.tech.BlockRailBase;
import common.entity.item.EntityCart;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Vec3;
import common.world.State;
import common.world.World;

public class ItemMinecart extends Item
{
    private final EntityCart.EnumMinecartType minecartType;

    public ItemMinecart(EntityCart.EnumMinecartType type)
    {
        this.setMaxAmount(1);
        this.minecartType = type;
        this.setTab(CheatTab.SPAWNERS);
//        if(type != EntityMinecart.EnumMinecartType.COMMAND_BLOCK)
    }

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        State iblockstate = worldIn.getState(pos);

        if (BlockRailBase.isRailBlock(iblockstate))
        {
            if (!worldIn.client)
            {
//            	EnumMinecartType type = this.minecartType;
//            	if(this.minecartType == EnumMinecartType.COMMAND_BLOCK && !((EntityNPCMP)playerIn).canUse(Permissions.CMDBLOCK)) {
//            		type = EnumMinecartType.RIDEABLE;
//        			EntityItem entityitem = playerIn.dropPlayerItemWithRandomChoice(new ItemStack(Blocks.command_block), false);
//        			if(entityitem != null) {
//        				entityitem.setNoPickupDelay();
//        				entityitem.setOwner(playerIn.getUser());
//        			}
//            	}
                BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = iblockstate.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;
                double d0 = 0.0D;

                if (blockrailbase$enumraildirection.isAscending())
                {
                    d0 = 0.5D;
                }

                EntityCart entityminecart = EntityCart.getMinecart(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.0625D + d0, (double)pos.getZ() + 0.5D, this.minecartType);

                if (stack.hasDisplayName())
                {
                    entityminecart.setCustomNameTag(stack.getDisplayName());
                }

                worldIn.spawnEntityInWorld(entityminecart);
//                if(this.minecartType == EnumMinecartType.COMMAND_BLOCK)
//            		((EntityMinecartCommandBlock)entityminecart).getCommandBlockLogic().setEnabled(((EntityNPCMP)playerIn).canUse(Permissions.CMDBLOCK));
            }

            --stack.size;
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean isMagnetic() {
    	return true;
    }
    
    public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
        double d0 = blockpos.getX() + 0.5 + (double)facing.getFrontOffsetX() * 1.125D;
        double d1 = Math.floor(blockpos.getY() + 0.5) + (double)facing.getFrontOffsetY();
        double d2 = blockpos.getZ() + 0.5 + (double)facing.getFrontOffsetZ() * 1.125D;
        BlockPos pos = blockpos.offset(facing);
        State iblockstate = world.getState(pos);
        BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = iblockstate.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;
        double d3;

        if (BlockRailBase.isRailBlock(iblockstate))
        {
            if (blockrailbase$enumraildirection.isAscending())
            {
                d3 = 0.6D;
            }
            else
            {
                d3 = 0.1D;
            }
        }
        else
        {
            if (iblockstate.getBlock() != Blocks.air || !BlockRailBase.isRailBlock(world.getState(pos.down())))
            {
                return super.dispenseStack(world, source, position, blockpos, facing, stack);
            }

            State iblockstate1 = world.getState(pos.down());
            BlockRailBase.EnumRailDirection blockrailbase$enumraildirection1 = iblockstate1.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate1.getValue(((BlockRailBase)iblockstate1.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;

            if (facing != Facing.DOWN && blockrailbase$enumraildirection1.isAscending())
            {
                d3 = -0.4D;
            }
            else
            {
                d3 = -0.9D;
            }
        }

        EntityCart entityminecart = EntityCart.getMinecart(world, d0, d1 + d3, d2, this.minecartType);

        if (stack.hasDisplayName())
        {
            entityminecart.setCustomNameTag(stack.getDisplayName());
        }

        world.spawnEntityInWorld(entityminecart);
        stack.splitStack(1);
        return stack;
    }
}
