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

package com.hypixel.hytale.builtin.crafting.window;

import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.protocol.packets.window.TierUpgradeAction;
import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.inventory.container.CombinedItemContainer;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.asset.type.item.config.CraftingRecipe;
import com.hypixel.hytale.protocol.packets.window.CraftRecipeAction;
import com.hypixel.hytale.builtin.crafting.component.CraftingManager;
import com.hypixel.hytale.protocol.packets.window.WindowAction;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.entity.entities.player.windows.WindowManager;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.protocol.packets.window.WindowType;
import com.hypixel.hytale.builtin.crafting.state.BenchState;
import com.hypixel.hytale.server.core.entity.entities.player.windows.MaterialContainerWindow;

public class SimpleCraftingWindow extends CraftingWindow implements MaterialContainerWindow
{
    public SimpleCraftingWindow(final BenchState benchState) {
        super(WindowType.BasicCrafting, benchState);
    }
    
    @Override
    public void init(@Nonnull final PlayerRef playerRef, @Nonnull final WindowManager manager) {
        super.init(playerRef, manager);
    }
    
    @Override
    public void handleAction(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final WindowAction action) {
        final CraftingManager craftingManager = store.getComponent(ref, CraftingManager.getComponentType());
        if (craftingManager == null) {
            return;
        }
        final World world = store.getExternalData().getWorld();
        if (action instanceof final CraftRecipeAction craftAction) {
            final String recipeId = craftAction.recipeId;
            final int quantity = craftAction.quantity;
            final CraftingRecipe craftRecipe = CraftingRecipe.getAssetMap().getAsset(recipeId);
            if (craftRecipe == null) {
                final PlayerRef playerRef = store.getComponent(ref, PlayerRef.getComponentType());
                playerRef.getPacketHandler().disconnect("Attempted to craft unknown recipe!");
                return;
            }
            final Player player = store.getComponent(ref, Player.getComponentType());
            final CombinedItemContainer combined = player.getInventory().getCombinedBackpackStorageHotbar();
            final CombinedItemContainer playerAndContainerInventory = new CombinedItemContainer(new ItemContainer[] { combined, this.getExtraResourcesSection().getItemContainer() });
            boolean accepted;
            if (craftRecipe.getTimeSeconds() > 0.0f) {
                accepted = craftingManager.queueCraft(ref, store, this, 0, craftRecipe, quantity, playerAndContainerInventory, CraftingManager.InputRemovalType.NORMAL);
            }
            else {
                accepted = craftingManager.craftItem(ref, store, craftRecipe, quantity, playerAndContainerInventory);
            }
            this.invalidateExtraResources();
            if (accepted) {
                final String completedState = (craftRecipe.getTimeSeconds() > 0.0f) ? "CraftCompleted" : "CraftCompletedInstant";
                this.setBlockInteractionState(completedState, world);
                if (this.bench.getCompletedSoundEventIndex() != 0) {
                    final Vector3d pos = new Vector3d();
                    this.blockType.getBlockCenter(this.rotationIndex, pos);
                    pos.add(this.x, this.y, this.z);
                    SoundUtil.playSoundEvent3d(this.bench.getCompletedSoundEventIndex(), SoundCategory.SFX, pos, store);
                }
            }
        }
        else if (action instanceof TierUpgradeAction && craftingManager.startTierUpgrade(ref, store, this)) {
            this.setBlockInteractionState("BenchUpgrading", world);
            if (this.bench.getBenchUpgradeSoundEventIndex() != 0) {
                final Vector3d pos2 = new Vector3d();
                this.blockType.getBlockCenter(this.rotationIndex, pos2);
                pos2.add(this.x, this.y, this.z);
                SoundUtil.playSoundEvent3d(this.bench.getBenchUpgradeSoundEventIndex(), SoundCategory.SFX, pos2, store);
            }
        }
    }
}
