package common.item;

import common.block.BlockDispenser;
import common.block.BlockRailBase;
import common.dispenser.BehaviorDefaultDispenseItem;
import common.dispenser.IBehaviorDispenseItem;
import common.dispenser.IBlockSource;
import common.entity.item.EntityCart;
import common.entity.npc.EntityNPC;
import common.init.DispenserRegistry;
import common.material.Material;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemMinecart extends Item
{
    private static final IBehaviorDispenseItem dispenserMinecartBehavior = new BehaviorDefaultDispenseItem()
    {
        private final BehaviorDefaultDispenseItem behaviourDefaultDispenseItem = new BehaviorDefaultDispenseItem();
        public ItemStack dispenseStack(IBlockSource source, ItemStack stack)
        {
            Facing enumfacing = BlockDispenser.getFacing(source.getBlockMetadata());
            World world = source.getWorld();
            double d0 = source.getX() + (double)enumfacing.getFrontOffsetX() * 1.125D;
            double d1 = Math.floor(source.getY()) + (double)enumfacing.getFrontOffsetY();
            double d2 = source.getZ() + (double)enumfacing.getFrontOffsetZ() * 1.125D;
            BlockPos blockpos = source.getBlockPos().offset(enumfacing);
            State iblockstate = world.getState(blockpos);
            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().getMaterial() != Material.air || !BlockRailBase.isRailBlock(world.getState(blockpos.down())))
                {
                    return this.behaviourDefaultDispenseItem.dispense(source, stack);
                }

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

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

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

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

            world.spawnEntityInWorld(entityminecart);
            stack.splitStack(1);
            return stack;
        }
        protected void playDispenseSound(IBlockSource source)
        {
            source.getWorld().playAuxSFX(1000, source.getBlockPos(), 0);
        }
    };
    private final EntityCart.EnumMinecartType minecartType;

    public ItemMinecart(EntityCart.EnumMinecartType type)
    {
        this.maxStackSize = 1;
        this.minecartType = type;
        this.setTab(CheatTab.tabSpawners);
//        if(type != EntityMinecart.EnumMinecartType.COMMAND_BLOCK)
        DispenserRegistry.REGISTRY.putObject(this, dispenserMinecartBehavior);
    }

    /**
     * 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.stackSize;
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean isMagnetic() {
    	return true;
    }
}
