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

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

import java.io.IOException;
import com.hypixel.hytale.codec.util.RawJsonReader;
import com.hypixel.hytale.codec.ExtraInfo;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.protocol.packets.interface_.CustomPage;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
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.protocol.packets.interface_.CustomPageLifetime;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import javax.annotation.Nonnull;
import com.hypixel.hytale.logger.HytaleLogger;

public abstract class InteractiveCustomUIPage<T> extends CustomUIPage
{
    @Nonnull
    private static final HytaleLogger LOGGER;
    @Nonnull
    protected final BuilderCodec<T> eventDataCodec;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public InteractiveCustomUIPage(@Nonnull final PlayerRef playerRef, @Nonnull final CustomPageLifetime lifetime, @Nonnull final BuilderCodec<T> eventDataCodec) {
        super(playerRef, lifetime);
        this.eventDataCodec = eventDataCodec;
    }
    
    public void handleDataEvent(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final T data) {
    }
    
    protected void sendUpdate(@Nullable final UICommandBuilder commandBuilder, @Nullable final UIEventBuilder eventBuilder, final boolean clear) {
        final Ref<EntityStore> ref = this.playerRef.getReference();
        if (ref == null) {
            return;
        }
        final Store<EntityStore> store = ref.getStore();
        final World world = store.getExternalData().getWorld();
        world.execute(() -> {
            if (!(!ref.isValid())) {
                final Player playerComponent = store.getComponent(ref, Player.getComponentType());
                if (!InteractiveCustomUIPage.$assertionsDisabled && playerComponent == null) {
                    throw new AssertionError();
                }
                else {
                    playerComponent.getPageManager();
                    new CustomPage(this.getClass().getName(), false, clear, this.lifetime, (commandBuilder != null) ? commandBuilder.getCommands() : UICommandBuilder.EMPTY_COMMAND_ARRAY, (eventBuilder != null) ? eventBuilder.getEvents() : UIEventBuilder.EMPTY_EVENT_BINDING_ARRAY);
                    final CustomPage page;
                    final Object o;
                    ((PageManager)o).updateCustomPage(page);
                }
            }
        });
    }
    
    @Override
    public void handleDataEvent(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, final String rawData) {
        final ExtraInfo extraInfo = ExtraInfo.THREAD_LOCAL.get();
        T data;
        try {
            data = this.eventDataCodec.decodeJson(new RawJsonReader(rawData.toCharArray()), extraInfo);
        }
        catch (final IOException e) {
            throw new RuntimeException(e);
        }
        extraInfo.getValidationResults().logOrThrowValidatorExceptions(InteractiveCustomUIPage.LOGGER);
        this.handleDataEvent(ref, store, data);
    }
    
    @Override
    protected void sendUpdate(@Nullable final UICommandBuilder commandBuilder, final boolean clear) {
        this.sendUpdate(commandBuilder, null, clear);
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
