package common.item;

import java.util.List;

import common.entity.Entity;
import common.entity.item.EntityBoat;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.ExtMath;
import common.util.HitPosition;
import common.util.Vec3;
import common.world.World;

public class ItemBoat extends Item
{
    public ItemBoat()
    {
        this.maxStackSize = 1;
        this.setTab(CheatTab.tabSpawners);
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn)
    {
        float f = 1.0F;
        float f1 = playerIn.prevPitch + (playerIn.rotPitch - playerIn.prevPitch) * f;
        float f2 = playerIn.prevYaw + (playerIn.rotYaw - playerIn.prevYaw) * f;
        double d0 = playerIn.prevX + (playerIn.posX - playerIn.prevX) * (double)f;
        double d1 = playerIn.prevY + (playerIn.posY - playerIn.prevY) * (double)f + (double)playerIn.getEyeHeight();
        double d2 = playerIn.prevZ + (playerIn.posZ - playerIn.prevZ) * (double)f;
        Vec3 vec3 = new Vec3(d0, d1, d2);
        float f3 = ExtMath.cos(-f2 * 0.017453292F - (float)Math.PI);
        float f4 = ExtMath.sin(-f2 * 0.017453292F - (float)Math.PI);
        float f5 = -ExtMath.cos(-f1 * 0.017453292F);
        float f6 = ExtMath.sin(-f1 * 0.017453292F);
        float f7 = f4 * f5;
        float f8 = f3 * f5;
        double d3 = 5.0D;
        Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
        HitPosition movingobjectposition = worldIn.rayTraceBlocks(vec3, vec31, true);

        if (movingobjectposition == null)
        {
            return itemStackIn;
        }
        else
        {
            Vec3 vec32 = playerIn.getLook(f);
            boolean flag = false;
            float f9 = 1.0F;
            List<Entity> list = worldIn.getEntitiesWithinAABBExcludingEntity(playerIn, playerIn.getEntityBoundingBox().addCoord(vec32.xCoord * d3, vec32.yCoord * d3, vec32.zCoord * d3).expand((double)f9, (double)f9, (double)f9));

            for (int i = 0; i < list.size(); ++i)
            {
                Entity entity = (Entity)list.get(i);

                if (entity.canBeCollidedWith())
                {
                    float f10 = entity.getCollisionBorderSize();
                    BoundingBox axisalignedbb = entity.getEntityBoundingBox().expand((double)f10, (double)f10, (double)f10);

                    if (axisalignedbb.isVecInside(vec3))
                    {
                        flag = true;
                    }
                }
            }

            if (flag)
            {
                return itemStackIn;
            }
            else
            {
                if (movingobjectposition.type == HitPosition.ObjectType.BLOCK)
                {
                    BlockPos blockpos = movingobjectposition.block;

                    if (worldIn.getState(blockpos).getBlock() == Blocks.snow_layer)
                    {
                        blockpos = blockpos.down();
                    }

                    EntityBoat entityboat = new EntityBoat(worldIn, (double)((float)blockpos.getX() + 0.5F), (double)((float)blockpos.getY() + 1.0F), (double)((float)blockpos.getZ() + 0.5F));
                    entityboat.rotYaw = (float)(((ExtMath.floord((double)(playerIn.rotYaw * 4.0F / 360.0F) + 0.5D) & 3) - 1) * 90);

                    if (!worldIn.getCollidingBoundingBoxes(entityboat, entityboat.getEntityBoundingBox().expand(-0.1D, -0.1D, -0.1D)).isEmpty())
                    {
                        return itemStackIn;
                    }

                    if (!worldIn.client)
                    {
                        worldIn.spawnEntityInWorld(entityboat);
                    }

//                    if (!playerIn.creative)
//                    {
                    --itemStackIn.stackSize;
//                    }

//                    playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
                }

                return itemStackIn;
            }
        }
    }
}
