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

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

import com.hypixel.hytale.codec.codecs.array.ArrayCodec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.server.core.entity.entities.player.windows.WindowManager;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.BenchUpgradeRequirement;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent;
import com.hypixel.hytale.math.vector.Vector3f;
import java.util.Collection;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import java.util.concurrent.ConcurrentHashMap;
import com.hypixel.hytale.builtin.crafting.window.BenchWindow;
import java.util.UUID;
import java.util.Map;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.Bench;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.universe.world.meta.state.DestroyableBlockState;
import com.hypixel.hytale.server.core.universe.world.meta.BlockState;

public class BenchState extends BlockState implements DestroyableBlockState
{
    public static BuilderCodec<BenchState> CODEC;
    private int tierLevel;
    protected ItemStack[] upgradeItems;
    protected Bench bench;
    protected final Map<UUID, BenchWindow> windows;
    
    public BenchState() {
        this.tierLevel = 1;
        this.upgradeItems = ItemStack.EMPTY_ARRAY;
        this.windows = new ConcurrentHashMap<UUID, BenchWindow>();
    }
    
    public int getTierLevel() {
        return this.tierLevel;
    }
    
    @Override
    public boolean initialize(@Nonnull final BlockType blockType) {
        if (!super.initialize(blockType)) {
            return false;
        }
        this.bench = blockType.getBench();
        if (this.bench == null) {
            if (this.upgradeItems.length > 0) {
                this.dropUpgradeItems();
            }
            return false;
        }
        return true;
    }
    
    public void addUpgradeItems(final List<ItemStack> consumed) {
        consumed.addAll(Arrays.asList(this.upgradeItems));
        this.upgradeItems = consumed.toArray(ItemStack[]::new);
        this.markNeedsSave();
    }
    
    private void dropUpgradeItems() {
        if (this.upgradeItems.length == 0) {
            return;
        }
        final World world = this.getChunk().getWorld();
        final Store<EntityStore> entityStore = world.getEntityStore().getStore();
        final Vector3d dropPosition = this.getBlockPosition().toVector3d().add(0.5, 0.0, 0.5);
        final Holder<EntityStore>[] itemEntityHolders = ItemComponent.generateItemDrops(entityStore, List.of(this.upgradeItems), dropPosition, Vector3f.ZERO);
        if (itemEntityHolders.length > 0) {
            world.execute(() -> entityStore.addEntities(itemEntityHolders, AddReason.SPAWN));
        }
        this.upgradeItems = ItemStack.EMPTY_ARRAY;
    }
    
    public Bench getBench() {
        return this.bench;
    }
    
    public void setTierLevel(final int newTierLevel) {
        if (this.tierLevel != newTierLevel) {
            this.tierLevel = newTierLevel;
            this.onTierLevelChange();
            this.markNeedsSave();
        }
    }
    
    public BenchUpgradeRequirement getNextLevelUpgradeMaterials() {
        return this.bench.getUpgradeRequirement(this.tierLevel);
    }
    
    protected void onTierLevelChange() {
        this.getChunk().setBlockInteractionState(this.getBlockPosition(), this.getBaseBlockType(), this.getTierStateName());
    }
    
    public BlockType getBaseBlockType() {
        final BlockType currentBlockType = this.getBlockType();
        final String baseBlockKey = currentBlockType.getDefaultStateKey();
        BlockType baseBlockType = BlockType.getAssetMap().getAsset(baseBlockKey);
        if (baseBlockType == null) {
            baseBlockType = currentBlockType;
        }
        return baseBlockType;
    }
    
    public String getTierStateName() {
        return (this.tierLevel > 1) ? ("Tier" + this.tierLevel) : "default";
    }
    
    @Override
    public void onDestroy() {
        WindowManager.closeAndRemoveAll(this.windows);
        this.dropUpgradeItems();
    }
    
    @Nonnull
    public Map<UUID, BenchWindow> getWindows() {
        return this.windows;
    }
    
    static {
        BenchState.CODEC = BuilderCodec.builder(BenchState.class, BenchState::new, BlockState.BASE_CODEC).appendInherited(new KeyedCodec<Integer>("TierLevel", Codec.INTEGER), (state, o) -> state.tierLevel = o, state -> state.tierLevel, (state, parent) -> state.tierLevel = parent.tierLevel).add().appendInherited(new KeyedCodec("UpgradeItems", new ArrayCodec(ItemStack.CODEC, ItemStack[]::new)), (state, o) -> state.upgradeItems = o, state -> state.upgradeItems, (state, parent) -> state.upgradeItems = parent.upgradeItems).add().build();
    }
}
