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

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

import com.hypixel.hytale.protocol.packets.interface_.CustomPageEvent;
import java.util.Iterator;
import java.util.List;
import com.hypixel.hytale.protocol.packets.window.OpenWindow;
import com.hypixel.hytale.server.core.entity.entities.player.windows.Window;
import com.hypixel.hytale.protocol.packets.interface_.CustomPage;
import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.interface_.SetPage;
import com.hypixel.hytale.protocol.packets.interface_.Page;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import javax.annotation.Nonnull;
import java.util.concurrent.atomic.AtomicInteger;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.entity.entities.player.windows.WindowManager;

public class PageManager
{
    @Nullable
    private WindowManager windowManager;
    private PlayerRef playerRef;
    @Nullable
    private CustomUIPage customPage;
    @Nonnull
    private final AtomicInteger customPageRequiredAcknowledgments;
    
    public PageManager() {
        this.customPageRequiredAcknowledgments = new AtomicInteger();
    }
    
    public void init(@Nonnull final PlayerRef playerRef, @Nonnull final WindowManager windowManager) {
        this.windowManager = windowManager;
        this.playerRef = playerRef;
    }
    
    public void clearCustomPageAcknowledgements() {
        this.customPageRequiredAcknowledgments.set(0);
    }
    
    @Nullable
    public CustomUIPage getCustomPage() {
        return this.customPage;
    }
    
    public void setPage(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final Page page) {
        this.setPage(ref, store, page, false);
    }
    
    public void setPage(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final Page page, final boolean canCloseThroughInteraction) {
        if (this.customPage != null) {
            this.customPage.onDismiss(ref, store);
            this.customPage = null;
            this.customPageRequiredAcknowledgments.incrementAndGet();
        }
        this.playerRef.getPacketHandler().writeNoCache(new SetPage(page, canCloseThroughInteraction));
    }
    
    public void openCustomPage(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final CustomUIPage page) {
        final UICommandBuilder commandBuilder = new UICommandBuilder();
        final UIEventBuilder eventBuilder = new UIEventBuilder();
        if (this.customPage != null) {
            this.customPage.onDismiss(ref, ref.getStore());
        }
        page.build(ref, commandBuilder, eventBuilder, store);
        this.updateCustomPage(new CustomPage(page.getClass().getName(), true, true, page.getLifetime(), commandBuilder.getCommands(), eventBuilder.getEvents()));
        this.customPage = page;
    }
    
    public boolean setPageWithWindows(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final Page page, final boolean canCloseThroughInteraction, @Nonnull final Window... windows) {
        if (this.windowManager == null) {
            return false;
        }
        final List<OpenWindow> windowPackets = this.windowManager.openWindows(ref, store, windows);
        if (windowPackets == null) {
            return false;
        }
        this.setPage(ref, store, page, canCloseThroughInteraction);
        for (final OpenWindow packet : windowPackets) {
            this.playerRef.getPacketHandler().write(packet);
        }
        return true;
    }
    
    public boolean openCustomPageWithWindows(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final CustomUIPage page, @Nonnull final Window... windows) {
        if (this.windowManager == null) {
            return false;
        }
        final List<OpenWindow> windowPackets = this.windowManager.openWindows(ref, store, windows);
        if (windowPackets == null) {
            return false;
        }
        this.openCustomPage(ref, store, page);
        for (final OpenWindow packet : windowPackets) {
            this.playerRef.getPacketHandler().write(packet);
        }
        return true;
    }
    
    public void updateCustomPage(@Nonnull final CustomPage page) {
        this.customPageRequiredAcknowledgments.incrementAndGet();
        this.playerRef.getPacketHandler().write(page);
    }
    
    public void handleEvent(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final CustomPageEvent event) {
        switch (event.type) {
            case Dismiss: {
                if (this.customPage == null) {
                    return;
                }
                this.customPage.onDismiss(ref, store);
                this.customPage = null;
                break;
            }
            case Data: {
                if (this.customPageRequiredAcknowledgments.get() != 0 || this.customPage == null) {
                    return;
                }
                this.customPage.handleDataEvent(ref, store, event.data);
                break;
            }
            case Acknowledge: {
                if (this.customPageRequiredAcknowledgments.decrementAndGet() < 0) {
                    this.customPageRequiredAcknowledgments.incrementAndGet();
                    throw new IllegalArgumentException("Client sent unexpected acknowledgement");
                }
                break;
            }
        }
    }
}
