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

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

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.codecs.array.ArrayCodec;
import java.util.Objects;
import com.hypixel.hytale.protocol.GameMode;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.Message;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class HotbarManager
{
    public static final int HOTBARS_MAX = 10;
    @Nonnull
    public static final BuilderCodec<HotbarManager> CODEC;
    private static final Message MESSAGE_GENERAL_HOTBAR_INVALID_SLOT;
    private static final Message MESSAGE_GENERAL_HOTBAR_INVALID_GAME_MODE;
    @Nonnull
    private ItemContainer[] savedHotbars;
    private int currentHotbar;
    private boolean currentlyLoadingHotbar;
    
    public HotbarManager() {
        this.savedHotbars = new ItemContainer[10];
        this.currentHotbar = 0;
    }
    
    public void saveHotbar(@Nonnull final Ref<EntityStore> playerRef, final short hotbarIndex, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final PlayerRef playerRefComponent = componentAccessor.getComponent(playerRef, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        if (hotbarIndex < 0 || hotbarIndex > 9) {
            playerRefComponent.sendMessage(HotbarManager.MESSAGE_GENERAL_HOTBAR_INVALID_SLOT);
            return;
        }
        final Player playerComponent = componentAccessor.getComponent(playerRef, Player.getComponentType());
        assert playerComponent != null;
        if (!playerComponent.getGameMode().equals(GameMode.Creative)) {
            playerRefComponent.sendMessage(HotbarManager.MESSAGE_GENERAL_HOTBAR_INVALID_GAME_MODE);
            return;
        }
        this.currentlyLoadingHotbar = true;
        this.savedHotbars[hotbarIndex] = playerComponent.getInventory().getHotbar().clone();
        this.currentHotbar = hotbarIndex;
        this.currentlyLoadingHotbar = false;
    }
    
    public void loadHotbar(@Nonnull final Ref<EntityStore> playerRef, final short hotbarIndex, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final PlayerRef playerRefComponent = componentAccessor.getComponent(playerRef, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        if (hotbarIndex < 0 || hotbarIndex > 9) {
            playerRefComponent.sendMessage(HotbarManager.MESSAGE_GENERAL_HOTBAR_INVALID_SLOT);
            return;
        }
        final Player playerComponent = componentAccessor.getComponent(playerRef, Player.getComponentType());
        assert playerComponent != null;
        if (!playerComponent.getGameMode().equals(GameMode.Creative)) {
            playerRefComponent.sendMessage(HotbarManager.MESSAGE_GENERAL_HOTBAR_INVALID_GAME_MODE);
            return;
        }
        this.currentlyLoadingHotbar = true;
        final ItemContainer hotbar = playerComponent.getInventory().getHotbar();
        hotbar.removeAllItemStacks();
        if (this.savedHotbars[hotbarIndex] != null) {
            final ItemContainer clone;
            final ItemContainer savedHotbar = clone = this.savedHotbars[hotbarIndex].clone();
            final ItemContainer obj = hotbar;
            Objects.requireNonNull(obj);
            clone.forEach(obj::setItemStackForSlot);
        }
        this.currentHotbar = hotbarIndex;
        this.currentlyLoadingHotbar = false;
        playerRefComponent.sendMessage(Message.translation("server.general.hotbar.loaded").param("id", hotbarIndex + 1));
    }
    
    public int getCurrentHotbarIndex() {
        return this.currentHotbar;
    }
    
    public boolean getIsCurrentlyLoadingHotbar() {
        return this.currentlyLoadingHotbar;
    }
    
    static {
        CODEC = BuilderCodec.builder(HotbarManager.class, HotbarManager::new).append(new KeyedCodec<ItemContainer[]>("SavedHotbars", new ArrayCodec<ItemContainer>((Codec<ItemContainer>)ItemContainer.CODEC, ItemContainer[]::new)), (player, savedHotbars) -> player.savedHotbars = savedHotbars, player -> player.savedHotbars).documentation("An array of item containers that represent the saved hotbars.").add().append(new KeyedCodec("CurrentHotbar", Codec.INTEGER), (player, currentHotbar) -> player.currentHotbar = currentHotbar, player -> player.currentHotbar).documentation("The current hotbar that the player has active.").add().build();
        MESSAGE_GENERAL_HOTBAR_INVALID_SLOT = Message.translation("server.general.hotbar.invalidSlot");
        MESSAGE_GENERAL_HOTBAR_INVALID_GAME_MODE = Message.translation("server.general.hotbar.invalidGameMode");
    }
}
