// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.entity.entities.player.pages.itemrepair;

import java.util.List;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.server.core.entity.entities.player.pages.choices.ChoiceElement;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.inventory.ItemContext;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.entity.entities.player.pages.choices.ChoiceBasePage;

public class ItemRepairPage extends ChoiceBasePage
{
    public ItemRepairPage(@Nonnull final PlayerRef playerRef, @Nonnull final ItemContainer itemContainer, final double repairPenalty, final ItemContext heldItemContext) {
        super(playerRef, getItemElements(itemContainer, repairPenalty, heldItemContext), "Pages/ItemRepairPage.ui");
    }
    
    @Override
    public void build(@Nonnull final Ref<EntityStore> ref, @Nonnull final UICommandBuilder commandBuilder, @Nonnull final UIEventBuilder eventBuilder, @Nonnull final Store<EntityStore> store) {
        if (this.getElements().length > 0) {
            super.build(ref, commandBuilder, eventBuilder, store);
            return;
        }
        commandBuilder.append(this.getPageLayout());
        commandBuilder.clear("#ElementList");
        commandBuilder.appendInline("#ElementList", "Label { Text: %customUI.itemRepairPage.noItems; Style: (Alignment: Center); }");
    }
    
    @Nonnull
    protected static ChoiceElement[] getItemElements(@Nonnull final ItemContainer itemContainer, final double repairPenalty, final ItemContext heldItemContext) {
        final List<ChoiceElement> elements = new ObjectArrayList<ChoiceElement>();
        for (short slot = 0; slot < itemContainer.getCapacity(); ++slot) {
            final ItemStack itemStack = itemContainer.getItemStack(slot);
            if (!ItemStack.isEmpty(itemStack) && !itemStack.isUnbreakable()) {
                if (itemStack.getDurability() < itemStack.getMaxDurability()) {
                    final ItemContext itemContext = new ItemContext(itemContainer, slot, itemStack);
                    elements.add(new ItemRepairElement(itemStack, new RepairItemInteraction(itemContext, repairPenalty, heldItemContext)));
                }
            }
        }
        return elements.toArray(ChoiceElement[]::new);
    }
}
