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

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

import com.hypixel.hytale.builtin.crafting.window.BenchWindow;
import java.util.Map;
import java.util.UUID;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.Bench;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.server.core.universe.world.accessor.BlockAccessor;
import com.hypixel.hytale.server.core.entity.entities.player.windows.Window;
import com.hypixel.hytale.protocol.packets.interface_.Page;
import com.hypixel.hytale.builtin.crafting.window.ProcessingBenchWindow;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.universe.world.meta.BlockState;
import com.hypixel.hytale.math.util.ChunkUtil;
import java.util.logging.Level;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.builtin.crafting.state.ProcessingBenchState;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.interaction.interaction.CooldownHandler;
import com.hypixel.hytale.math.vector.Vector3i;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.entity.InteractionContext;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.CommandBuffer;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.client.SimpleBlockInteraction;

public class OpenProcessingBenchInteraction extends SimpleBlockInteraction
{
    public static final BuilderCodec<OpenProcessingBenchInteraction> CODEC;
    
    @Override
    protected void interactWithBlock(@Nonnull final World world, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final Vector3i pos, @Nonnull final CooldownHandler cooldownHandler) {
        final Ref<EntityStore> ref = context.getEntity();
        final Store<EntityStore> store = ref.getStore();
        final Player playerComponent = commandBuffer.getComponent(ref, Player.getComponentType());
        if (playerComponent == null) {
            return;
        }
        final BlockState state = world.getState(pos.x, pos.y, pos.z, true);
        if (!(state instanceof ProcessingBenchState)) {
            playerComponent.sendMessage(Message.translation("server.interactions.invalidBlockState").param("interaction", this.getClass().getSimpleName()).param("blockState", (state != null) ? state.getClass().getSimpleName() : "null"));
            return;
        }
        final ProcessingBenchState benchState = (ProcessingBenchState)state;
        final BlockType blockType = world.getBlockType(pos.x, pos.y, pos.z);
        final Bench blockTypeBench = blockType.getBench();
        if ((blockTypeBench == null || !blockTypeBench.equals(benchState.getBench())) && !benchState.initialize(blockType)) {
            ProcessingBenchState.LOGGER.at(Level.WARNING).log("Failed to re-initialize: %s, %s", blockType.getId(), pos);
            final int x = pos.getX();
            final int z = pos.getZ();
            world.getChunk(ChunkUtil.indexChunkFromBlock(x, z)).setState(x, pos.getY(), z, null);
            return;
        }
        final UUIDComponent uuidComponent = commandBuffer.getComponent(ref, UUIDComponent.getComponentType());
        assert uuidComponent != null;
        final UUID uuid = uuidComponent.getUuid();
        final ProcessingBenchWindow window = new ProcessingBenchWindow(benchState);
        final Map<UUID, BenchWindow> windows = benchState.getWindows();
        if (windows.putIfAbsent(uuid, window) == null) {
            benchState.updateFuelValues();
            if (playerComponent.getPageManager().setPageWithWindows(ref, store, Page.Bench, true, window)) {
                window.registerCloseEvent(event -> {
                    windows.remove(uuid, window);
                    final BlockType currentBlockType = world.getBlockType(pos);
                    final String interactionState = BlockAccessor.getCurrentInteractionState(currentBlockType);
                    if (windows.isEmpty() && !"Processing".equals(interactionState) && !"ProcessCompleted".equals(interactionState)) {
                        world.setBlockInteractionState(pos, currentBlockType, "default");
                    }
                    final int soundEventIndex2 = blockType.getBench().getLocalCloseSoundEventIndex();
                    if (soundEventIndex2 == 0) {
                        return;
                    }
                    else {
                        SoundUtil.playSoundEvent2d(ref, soundEventIndex2, SoundCategory.UI, commandBuffer);
                        return;
                    }
                });
                final int soundEventIndex = blockType.getBench().getLocalOpenSoundEventIndex();
                if (soundEventIndex == 0) {
                    return;
                }
                SoundUtil.playSoundEvent2d(ref, soundEventIndex, SoundCategory.UI, commandBuffer);
            }
            else {
                windows.remove(uuid, window);
            }
        }
    }
    
    @Override
    protected void simulateInteractWithBlock(@Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final World world, @Nonnull final Vector3i targetBlock) {
    }
    
    static {
        CODEC = BuilderCodec.builder(OpenProcessingBenchInteraction.class, OpenProcessingBenchInteraction::new, SimpleBlockInteraction.CODEC).documentation("Opens the processing bench page.").build();
    }
}
