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

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

import com.hypixel.hytale.protocol.packets.interface_.Page;
import javax.annotation.Nullable;
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 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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.PlayerRef;

public abstract class CustomUIPage
{
    @Nonnull
    protected final PlayerRef playerRef;
    @Nonnull
    protected CustomPageLifetime lifetime;
    
    public CustomUIPage(@Nonnull final PlayerRef playerRef, @Nonnull final CustomPageLifetime lifetime) {
        this.playerRef = playerRef;
        this.lifetime = lifetime;
    }
    
    public void setLifetime(@Nonnull final CustomPageLifetime lifetime) {
        this.lifetime = lifetime;
    }
    
    @Nonnull
    public CustomPageLifetime getLifetime() {
        return this.lifetime;
    }
    
    public void handleDataEvent(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, final String rawData) {
        throw new UnsupportedOperationException("CustomUIPage doesn't support events! " + String.valueOf(this) + ": " + rawData);
    }
    
    public abstract void build(@Nonnull final Ref<EntityStore> p0, @Nonnull final UICommandBuilder p1, @Nonnull final UIEventBuilder p2, @Nonnull final Store<EntityStore> p3);
    
    protected void rebuild() {
        final Ref<EntityStore> ref = this.playerRef.getReference();
        if (ref == null) {
            return;
        }
        final Store<EntityStore> store = ref.getStore();
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        final UICommandBuilder commandBuilder = new UICommandBuilder();
        final UIEventBuilder eventBuilder = new UIEventBuilder();
        this.build(ref, commandBuilder, eventBuilder, ref.getStore());
        playerComponent.getPageManager().updateCustomPage(new CustomPage(this.getClass().getName(), false, true, this.lifetime, commandBuilder.getCommands(), eventBuilder.getEvents()));
    }
    
    protected void sendUpdate() {
        this.sendUpdate(null, false);
    }
    
    protected void sendUpdate(@Nullable final UICommandBuilder commandBuilder) {
        this.sendUpdate(commandBuilder, false);
    }
    
    protected void sendUpdate(@Nullable final UICommandBuilder commandBuilder, final boolean clear) {
        final Ref<EntityStore> ref = this.playerRef.getReference();
        if (ref == null) {
            return;
        }
        final Store<EntityStore> store = ref.getStore();
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        playerComponent.getPageManager().updateCustomPage(new CustomPage(this.getClass().getName(), false, clear, this.lifetime, (commandBuilder != null) ? commandBuilder.getCommands() : UICommandBuilder.EMPTY_COMMAND_ARRAY, UIEventBuilder.EMPTY_EVENT_BINDING_ARRAY));
    }
    
    protected void close() {
        final Ref<EntityStore> ref = this.playerRef.getReference();
        if (ref == null) {
            return;
        }
        final Store<EntityStore> store = ref.getStore();
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        playerComponent.getPageManager().setPage(ref, store, Page.None);
    }
    
    public void onDismiss(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store) {
    }
}
