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

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

import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.asset.type.gameplay.CraftingConfig;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.builtin.adventure.memories.MemoriesPlugin;
import com.hypixel.hytale.builtin.crafting.component.CraftingManager;
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.asset.type.item.config.Item;
import com.hypixel.hytale.protocol.packets.window.WindowType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.entity.entities.player.windows.MaterialExtraResourcesSection;
import com.google.gson.JsonObject;
import com.hypixel.hytale.builtin.crafting.state.BenchState;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.bench.Bench;
import com.hypixel.hytale.server.core.entity.entities.player.windows.MaterialContainerWindow;
import com.hypixel.hytale.server.core.entity.entities.player.windows.BlockWindow;

public abstract class BenchWindow extends BlockWindow implements MaterialContainerWindow
{
    private static final float CRAFTING_UPDATE_MIN_PERCENT = 0.05f;
    private static final long CRAFTING_UPDATE_INTERVAL_MS = 500L;
    protected static final String BENCH_UPGRADING = "BenchUpgrading";
    private float lastUpdatePercent;
    private long lastUpdateTimeMs;
    protected final Bench bench;
    protected final BenchState benchState;
    protected final JsonObject windowData;
    @Nonnull
    private final MaterialExtraResourcesSection extraResourcesSection;
    
    public BenchWindow(@Nonnull final WindowType windowType, @Nonnull final BenchState benchState) {
        super(windowType, benchState.getBlockX(), benchState.getBlockY(), benchState.getBlockZ(), benchState.getRotationIndex(), benchState.getBlockType());
        this.windowData = new JsonObject();
        this.extraResourcesSection = new MaterialExtraResourcesSection();
        this.bench = this.blockType.getBench();
        this.benchState = benchState;
        final Item item = this.blockType.getItem();
        if (item == null) {
            throw new IllegalStateException("Bench block type " + this.blockType.getId() + " does not have an associated item!");
        }
        this.windowData.addProperty("type", this.bench.getType().ordinal());
        this.windowData.addProperty("id", this.bench.getId());
        this.windowData.addProperty("name", item.getTranslationKey());
        this.windowData.addProperty("blockItemId", item.getId());
        this.windowData.addProperty("tierLevel", this.getBenchTierLevel());
    }
    
    @Nonnull
    @Override
    public JsonObject getData() {
        return this.windowData;
    }
    
    @Override
    protected boolean onOpen0(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store) {
        final CraftingManager craftingManagerComponent = store.getComponent(ref, CraftingManager.getComponentType());
        if (craftingManagerComponent == null) {
            return false;
        }
        craftingManagerComponent.setBench(this.x, this.y, this.z, this.blockType);
        final World world = store.getExternalData().getWorld();
        final int memoriesLevel = MemoriesPlugin.get().getMemoriesLevel(world.getGameplayConfig());
        this.windowData.addProperty("worldMemoriesLevel", memoriesLevel);
        final int chestCount = CraftingManager.feedExtraResourcesSection(this.benchState, this.extraResourcesSection);
        final CraftingConfig craftingConfig = world.getGameplayConfig().getCraftingConfig();
        final int maxChestCount = craftingConfig.getBenchMaterialChestLimit();
        final int horizontalRadius = craftingConfig.getBenchMaterialHorizontalChestSearchRadius();
        final int verticalRadius = craftingConfig.getBenchMaterialVerticalChestSearchRadius();
        this.windowData.addProperty("nearbyChestCount", chestCount);
        this.windowData.addProperty("maxChestCount", maxChestCount);
        this.windowData.addProperty("chestHorizontalRadius", horizontalRadius);
        this.windowData.addProperty("chestVerticalRadius", verticalRadius);
        return true;
    }
    
    protected int getBenchTierLevel() {
        return (this.benchState != null) ? this.benchState.getTierLevel() : 1;
    }
    
    public void onClose0(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final CraftingManager craftingManagerComponent = componentAccessor.getComponent(ref, CraftingManager.getComponentType());
        if (craftingManagerComponent == null) {
            return;
        }
        if (craftingManagerComponent.clearBench(ref, componentAccessor) && this.bench.getFailedSoundEventIndex() != 0) {
            SoundUtil.playSoundEvent2d(ref, this.bench.getFailedSoundEventIndex(), SoundCategory.UI, componentAccessor);
        }
    }
    
    public void updateCraftingJob(final float percent) {
        this.windowData.addProperty("progress", percent);
        this.checkProgressInvalidate(percent);
    }
    
    public void updateBenchUpgradeJob(final float percent) {
        this.windowData.addProperty("tierUpgradeProgress", percent);
        this.checkProgressInvalidate(percent);
    }
    
    private void checkProgressInvalidate(final float percent) {
        if (this.lastUpdatePercent != percent) {
            final long time = System.currentTimeMillis();
            if (percent >= 1.0f || percent < this.lastUpdatePercent || percent - this.lastUpdatePercent > 0.05f || time - this.lastUpdateTimeMs > 500L || this.lastUpdateTimeMs == 0L) {
                this.lastUpdatePercent = percent;
                this.lastUpdateTimeMs = time;
                this.invalidate();
            }
        }
    }
    
    public void updateBenchTierLevel(final int newValue) {
        this.windowData.addProperty("tierLevel", newValue);
        this.updateBenchUpgradeJob(0.0f);
        this.setNeedRebuild();
        this.invalidate();
    }
    
    @Nonnull
    @Override
    public MaterialExtraResourcesSection getExtraResourcesSection() {
        if (!this.extraResourcesSection.isValid()) {
            CraftingManager.feedExtraResourcesSection(this.benchState, this.extraResourcesSection);
        }
        return this.extraResourcesSection;
    }
    
    @Override
    public void invalidateExtraResources() {
        this.extraResourcesSection.setValid(false);
        this.invalidate();
    }
    
    @Override
    public boolean isValid() {
        return this.extraResourcesSection.isValid();
    }
}
