package common.item.tool;

import common.block.artificial.BlockPortalFrame;
import common.color.TextColor;
import common.entity.item.EntityOrb;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.Items;
import common.init.SoundEvent;
import common.item.CheatTab;
import common.item.ItemFragile;
import common.item.ItemStack;
import common.model.ParticleType;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemChargedOrb extends ItemFragile
{
    public ItemChargedOrb()
    {
        this.setTab(CheatTab.TOOLS);
        this.setMaxDamage(16);
        this.setColor(TextColor.DMAGENTA);
    }
    
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn)
    {
//    	if (!playerIn.capabilities.isCreativeMode)
//        {
//            --itemStackIn.stackSize;
//        }
    	if(itemStackIn.getItemDamage() >= this.getMaxDamage())
    		return itemStackIn;
    	itemStackIn.damageItem(1, playerIn);
        worldIn.playSoundAtEntity(playerIn, SoundEvent.THROW, 0.5F);

        if (!worldIn.client)
        {
            worldIn.spawnEntityInWorld(new EntityOrb(worldIn, playerIn));
        }

//        playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
        return itemStackIn.getItemDamage() >= this.getMaxDamage() ? new ItemStack(Items.orb) : itemStackIn;
    }

    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 (stack.getItemDamage() == 0 && playerIn.canPlayerEdit(pos.offset(side), side, stack) /* && worldIn.dimension.getDimensionId() == 0 || worldIn.dimension.getDimensionId() == 1) */ && iblockstate.getBlock() == Blocks.portal_frame && !((Boolean)iblockstate.getValue(BlockPortalFrame.ORB)).booleanValue())
        {
            if (worldIn.client)
            {
                return true;
            }
            else
            {
                worldIn.setState(pos, iblockstate.withProperty(BlockPortalFrame.ORB, Boolean.valueOf(true)), 2);
                worldIn.updateComparatorOutputLevel(pos, Blocks.portal_frame);
                --stack.size;

                for (int i = 0; i < 16; ++i)
                {
                    double d0 = (double)((float)pos.getX() + (5.0F + worldIn.rand.floatv() * 6.0F) / 16.0F);
                    double d1 = (double)((float)pos.getY() + 0.8125F);
                    double d2 = (double)((float)pos.getZ() + (5.0F + worldIn.rand.floatv() * 6.0F) / 16.0F);
                    double d3 = 0.0D;
                    double d4 = 0.0D;
                    double d5 = 0.0D;
                    worldIn.spawnParticle(ParticleType.SMOKE_NORMAL, d0, d1, d2, d3, d4, d5);
                }

                Facing enumfacing = (Facing)iblockstate.getValue(BlockPortalFrame.FACING);
                int l = 0;
                int j = 0;
                boolean flag1 = false;
                boolean flag = true;
                Facing enumfacing1 = enumfacing.rotateY();

                for (int k = -2; k <= 2; ++k)
                {
                    BlockPos blockpos1 = pos.offset(enumfacing1, k);
                    State iblockstate1 = worldIn.getState(blockpos1);

                    if (iblockstate1.getBlock() == Blocks.portal_frame)
                    {
                        if (!((Boolean)iblockstate1.getValue(BlockPortalFrame.ORB)).booleanValue())
                        {
                            flag = false;
                            break;
                        }

                        j = k;

                        if (!flag1)
                        {
                            l = k;
                            flag1 = true;
                        }
                    }
                }

                if (flag && j == l + 2)
                {
                    BlockPos blockpos = pos.offset(enumfacing, 4);

                    for (int i1 = l; i1 <= j; ++i1)
                    {
                        BlockPos blockpos2 = blockpos.offset(enumfacing1, i1);
                        State iblockstate3 = worldIn.getState(blockpos2);

                        if (iblockstate3.getBlock() != Blocks.portal_frame || !((Boolean)iblockstate3.getValue(BlockPortalFrame.ORB)).booleanValue())
                        {
                            flag = false;
                            break;
                        }
                    }

                    for (int j1 = l - 1; j1 <= j + 1; j1 += 4)
                    {
                        blockpos = pos.offset(enumfacing1, j1);

                        for (int l1 = 1; l1 <= 3; ++l1)
                        {
                            BlockPos blockpos3 = blockpos.offset(enumfacing, l1);
                            State iblockstate2 = worldIn.getState(blockpos3);

                            if (iblockstate2.getBlock() != Blocks.portal_frame || !((Boolean)iblockstate2.getValue(BlockPortalFrame.ORB)).booleanValue())
                            {
                                flag = false;
                                break;
                            }
                        }
                    }

                    if (flag)
                    {
                        for (int k1 = l; k1 <= j; ++k1)
                        {
                            blockpos = pos.offset(enumfacing1, k1);

                            for (int i2 = 1; i2 <= 3; ++i2)
                            {
                                BlockPos blockpos4 = blockpos.offset(enumfacing, i2);
                                worldIn.setState(blockpos4, Blocks.floor_portal.getState(), 2);
                            }
                        }
                    }
                }

                return true;
            }
        }
        else
        {
            return false;
        }
    }
}
