package common.inventory;

import common.entity.item.EntityXp;
import common.entity.npc.EntityNPC;
import common.init.Config;
import common.init.SmeltingRegistry;
import common.item.ItemStack;
import common.util.ExtMath;

public class SlotFurnaceOutput extends Slot
{
    /** The player that is using the GUI where this slot resides. */
    private EntityNPC thePlayer;
    private int smelted;

    public SlotFurnaceOutput(EntityNPC player, IInventory inventoryIn, int slotIndex, int xPosition, int yPosition)
    {
        super(inventoryIn, slotIndex, xPosition, yPosition);
        this.thePlayer = player;
    }

    /**
     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
     */
    public boolean isItemValid(ItemStack stack)
    {
        return false;
    }

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    public ItemStack decrStackSize(int amount)
    {
        if (this.getHasStack())
        {
            this.smelted += Math.min(amount, this.getStack().stackSize);
        }

        return super.decrStackSize(amount);
    }

    public void onPickupFromSlot(EntityNPC playerIn, ItemStack stack)
    {
        this.onCrafting(stack);
        super.onPickupFromSlot(playerIn, stack);
    }

    /**
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
     * internal count then calls onCrafting(item).
     */
    protected void onCrafting(ItemStack stack, int amount)
    {
        this.smelted += amount;
        this.onCrafting(stack);
    }

    /**
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
     */
    protected void onCrafting(ItemStack stack)
    {
//        stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.smelted);

        if (!this.thePlayer.worldObj.client && Config.smeltingXP)
        {
            int xp = this.smelted;
            float smeltXp = SmeltingRegistry.getExperience(stack);

            if (smeltXp == 0.0F)
            {
                xp = 0;
            }
            else if (smeltXp < 1.0F)
            {
                int mxp = ExtMath.floorf((float)xp * smeltXp);

                if (mxp < ExtMath.ceilf((float)xp * smeltXp) && Math.random() < (double)((float)xp * smeltXp - (float)mxp))
                {
                    ++mxp;
                }

                xp = mxp;
            }

            while (xp > 0)
            {
                int split = EntityXp.getXPSplit(xp);
                xp -= split;
                this.thePlayer.worldObj.spawnEntityInWorld(new EntityXp(this.thePlayer.worldObj, this.thePlayer.posX, this.thePlayer.posY + 0.5D, this.thePlayer.posZ + 0.5D, split));
            }
        }

        this.smelted = 0;

//        if (stack.getItem() == Items.iron_ingot)
//        {
//            this.thePlayer.triggerAchievement(AchievementList.acquireIron);
//        }
//
//        if (stack.getItem() == Items.cooked_fish)
//        {
//            this.thePlayer.triggerAchievement(AchievementList.cookFish);
//        }
    }
}
