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

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

import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.PlayerRef;
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.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.asset.type.gameplay.GameplayConfig;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.builtin.adventure.memories.MemoriesGameplayConfig;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import java.util.Iterator;
import java.util.Set;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.Bench;
import com.hypixel.hytale.builtin.crafting.CraftingPlugin;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.CraftingBench;
import com.google.gson.JsonArray;
import com.hypixel.hytale.builtin.crafting.state.BenchState;
import com.hypixel.hytale.protocol.packets.window.WindowType;
import javax.annotation.Nonnull;

public abstract class CraftingWindow extends BenchWindow
{
    @Nonnull
    protected static final String CRAFT_COMPLETED = "CraftCompleted";
    @Nonnull
    protected static final String CRAFT_COMPLETED_INSTANT = "CraftCompletedInstant";
    
    public CraftingWindow(@Nonnull final WindowType windowType, @Nonnull final BenchState benchState) {
        super(windowType, benchState);
        final JsonArray categories = new JsonArray();
        final Bench bench = this.bench;
        if (bench instanceof final CraftingBench craftingBench) {
            for (final CraftingBench.BenchCategory benchCategory : craftingBench.getCategories()) {
                final JsonObject category = new JsonObject();
                categories.add(category);
                category.addProperty("id", benchCategory.getId());
                category.addProperty("name", benchCategory.getName());
                category.addProperty("icon", benchCategory.getIcon());
                final Set<String> recipes = CraftingPlugin.getAvailableRecipesForCategory(this.bench.getId(), benchCategory.getId());
                if (recipes != null) {
                    final JsonArray recipesArray = new JsonArray();
                    for (final String recipeId : recipes) {
                        recipesArray.add(recipeId);
                    }
                    category.add("craftableRecipes", recipesArray);
                }
                if (benchCategory.getItemCategories() != null) {
                    final JsonArray itemCategories = new JsonArray();
                    for (final CraftingBench.BenchItemCategory benchItemCategory : benchCategory.getItemCategories()) {
                        final JsonObject itemCategory = new JsonObject();
                        itemCategory.addProperty("id", benchItemCategory.getId());
                        itemCategory.addProperty("icon", benchItemCategory.getIcon());
                        itemCategory.addProperty("diagram", benchItemCategory.getDiagram());
                        itemCategory.addProperty("slots", benchItemCategory.getSlots());
                        itemCategory.addProperty("specialSlot", benchItemCategory.isSpecialSlot());
                        itemCategories.add(itemCategory);
                    }
                    category.add("itemCategories", itemCategories);
                }
            }
            this.windowData.add("categories", categories);
        }
    }
    
    @Override
    protected boolean onOpen0(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store) {
        super.onOpen0(ref, store);
        final GameplayConfig gameplayConfig = store.getExternalData().getWorld().getGameplayConfig();
        final MemoriesGameplayConfig memoriesConfig = MemoriesGameplayConfig.get(gameplayConfig);
        if (memoriesConfig != null) {
            final int[] memoriesAmountPerLevel = memoriesConfig.getMemoriesAmountPerLevel();
            if (memoriesAmountPerLevel != null && memoriesAmountPerLevel.length > 1) {
                final JsonArray memoriesPerLevel = new JsonArray();
                for (int i = 0; i < memoriesAmountPerLevel.length; ++i) {
                    memoriesPerLevel.add(memoriesAmountPerLevel[i]);
                }
                this.windowData.add("memoriesPerLevel", memoriesPerLevel);
            }
        }
        if (this.bench.getLocalOpenSoundEventIndex() != 0) {
            SoundUtil.playSoundEvent2d(ref, this.bench.getLocalOpenSoundEventIndex(), SoundCategory.UI, store);
        }
        return true;
    }
    
    @Override
    public void onClose0(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        super.onClose0(ref, componentAccessor);
        final World world = componentAccessor.getExternalData().getWorld();
        this.setBlockInteractionState(this.benchState.getTierStateName(), world);
        if (this.bench.getLocalCloseSoundEventIndex() != 0) {
            SoundUtil.playSoundEvent2d(ref, this.bench.getLocalCloseSoundEventIndex(), SoundCategory.UI, componentAccessor);
        }
    }
    
    public void setBlockInteractionState(@Nonnull final String state, @Nonnull final World world) {
        final WorldChunk worldChunk = world.getChunk(ChunkUtil.indexChunkFromBlock(this.x, this.z));
        if (worldChunk == null) {
            return;
        }
        final BlockType blockType = worldChunk.getBlockType(this.x, this.y, this.z);
        if (blockType == null) {
            return;
        }
        worldChunk.setBlockInteractionState(this.x, this.y, this.z, blockType, state, true);
    }
    
    public static boolean craftSimpleItem(@Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final CraftingManager craftingManager, @Nonnull final CraftRecipeAction action) {
        final String recipeId = action.recipeId;
        final int quantity = action.quantity;
        if (recipeId == null) {
            return false;
        }
        final CraftingRecipe recipe = CraftingRecipe.getAssetMap().getAsset(recipeId);
        if (recipe == null) {
            final PlayerRef playerRef = store.getComponent(ref, PlayerRef.getComponentType());
            assert playerRef != null;
            playerRef.getPacketHandler().disconnect("Attempted to craft unknown recipe!");
            return false;
        }
        else {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            assert playerComponent != null;
            craftingManager.craftItem(ref, store, recipe, quantity, playerComponent.getInventory().getCombinedBackpackStorageHotbar());
            return true;
        }
    }
}
