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

package com.hypixel.hytale.builtin.asseteditor;

import java.util.logging.Level;
import java.util.concurrent.Executor;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorAuthorization;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorUpdateJsonAsset;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorInitialize;
import com.hypixel.hytale.server.core.io.handlers.IPacketHandler;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.io.handlers.SubPacketHandler;

public class AssetEditorGamePacketHandler implements SubPacketHandler
{
    private static final HytaleLogger LOGGER;
    private final IPacketHandler packetHandler;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public AssetEditorGamePacketHandler(final IPacketHandler packetHandler) {
        this.packetHandler = packetHandler;
    }
    
    @Override
    public void registerHandlers() {
        if (AssetEditorPlugin.get().isDisabled()) {
            this.packetHandler.registerNoOpHandlers(302);
            this.packetHandler.registerNoOpHandlers(325);
            return;
        }
        this.packetHandler.registerHandler(302, p -> this.handle((AssetEditorInitialize)p));
        this.packetHandler.registerHandler(323, p -> this.handle((AssetEditorUpdateJsonAsset)p));
    }
    
    public void handle(final AssetEditorInitialize packet) {
        final PlayerRef playerRef = this.packetHandler.getPlayerRef();
        final Ref<EntityStore> ref = playerRef.getReference();
        if (ref == null || !ref.isValid()) {
            throw new RuntimeException("Unable to process AssetEditorInitialize packet. Player ref is invalid!");
        }
        final Store<EntityStore> store = ref.getStore();
        final World world = store.getExternalData().getWorld();
        world.execute(() -> {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            if (!AssetEditorGamePacketHandler.$assertionsDisabled && playerComponent == null) {
                throw new AssertionError();
            }
            else if (this.lacksPermission(playerComponent, false)) {
                this.packetHandler.getPlayerRef().getPacketHandler().write(new AssetEditorAuthorization(false));
            }
            else {
                this.packetHandler.getPlayerRef().getPacketHandler().write(new AssetEditorAuthorization(true));
                AssetEditorPlugin.get().handleInitializeEditor(ref, store);
            }
        });
    }
    
    @Deprecated
    public void handle(@Nonnull final AssetEditorUpdateJsonAsset packet) {
        final PlayerRef playerRef = this.packetHandler.getPlayerRef();
        final Ref<EntityStore> ref = playerRef.getReference();
        if (ref == null || !ref.isValid()) {
            throw new RuntimeException("Unable to process AssetEditorUpdateJsonAsset packet. Player ref is invalid!");
        }
        final Store<EntityStore> store = ref.getStore();
        final World world = store.getExternalData().getWorld();
        CompletableFuture.runAsync(() -> {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            if (this.lacksPermission(playerComponent, true)) {
                return;
            }
        }, world).thenRunAsync(() -> {
            AssetEditorGamePacketHandler.LOGGER.at(Level.INFO).log("%s updating json asset at %s", this.packetHandler.getPlayerRef().getUsername(), packet.path);
            final EditorClient mockClient = new EditorClient(playerRef);
            AssetEditorPlugin.get();
            AssetPath assetPath = null;
            if (packet.path != null) {
                new(com.hypixel.hytale.builtin.asseteditor.AssetPath.class)();
                new AssetPath(packet.path);
            }
            else {
                assetPath = null;
            }
            final AssetEditorPlugin assetEditorPlugin;
            final EditorClient editorClient;
            assetEditorPlugin.handleJsonAssetUpdate(editorClient, assetPath, packet.assetType, packet.assetIndex, packet.commands, packet.token);
        });
    }
    
    private boolean lacksPermission(@Nonnull final Player player, final boolean shouldShowDenialMessage) {
        if (!player.hasPermission("hytale.editor.asset")) {
            if (shouldShowDenialMessage) {
                player.sendMessage(Messages.USAGE_DENIED_MESSAGE);
            }
            return true;
        }
        return false;
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
