package net.minecraft.client.gui.spectator;

import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.spectator.SpectatorGui;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.spectator.categories.SpectatorPage;
import com.google.common.collect.Lists;
import java.util.List;
import com.google.common.base.MoreObjects;

public class SpectatorMenu {
    private static final SpectatorMenuItem CLOSE_ITEM;
    private static final SpectatorMenuItem SCROLL_LEFT;
    private static final SpectatorMenuItem SCROLL_RIGHT_ENABLED;
    private static final SpectatorMenuItem SCROLL_RIGHT_DISABLED;
    public static final SpectatorMenuItem EMPTY_SLOT;
    private final SpectatorMenuListener listener;
    private SpectatorMenuCategory category;
    private int selectedSlot;
    private int page;
    
    public SpectatorMenu(final SpectatorMenuListener dum) {
        this.selectedSlot = -1;
        this.category = new RootSpectatorMenuCategory();
        this.listener = dum;
    }
    
    public SpectatorMenuItem getItem(final int integer) {
        final int integer2 = integer + this.page * 6;
        if (this.page > 0 && integer == 0) {
            return SpectatorMenu.SCROLL_LEFT;
        }
        if (integer == 7) {
            if (integer2 < this.category.getItems().size()) {
                return SpectatorMenu.SCROLL_RIGHT_ENABLED;
            }
            return SpectatorMenu.SCROLL_RIGHT_DISABLED;
        }
        else {
            if (integer == 8) {
                return SpectatorMenu.CLOSE_ITEM;
            }
            if (integer2 < 0 || integer2 >= this.category.getItems().size()) {
                return SpectatorMenu.EMPTY_SLOT;
            }
            return MoreObjects.<SpectatorMenuItem>firstNonNull(this.category.getItems().get(integer2), SpectatorMenu.EMPTY_SLOT);
        }
    }
    
    public List<SpectatorMenuItem> getItems() {
        final List<SpectatorMenuItem> list2 = Lists.newArrayList();
        for (int integer3 = 0; integer3 <= 8; ++integer3) {
            list2.add(this.getItem(integer3));
        }
        return list2;
    }
    
    public SpectatorMenuItem getSelectedItem() {
        return this.getItem(this.selectedSlot);
    }
    
    public SpectatorMenuCategory getSelectedCategory() {
        return this.category;
    }
    
    public void selectSlot(final int integer) {
        final SpectatorMenuItem dul3 = this.getItem(integer);
        if (dul3 != SpectatorMenu.EMPTY_SLOT) {
            if (this.selectedSlot == integer && dul3.isEnabled()) {
                dul3.selectItem(this);
            }
            else {
                this.selectedSlot = integer;
            }
        }
    }
    
    public void exit() {
        this.listener.onSpectatorMenuClosed(this);
    }
    
    public int getSelectedSlot() {
        return this.selectedSlot;
    }
    
    public void selectCategory(final SpectatorMenuCategory duk) {
        this.category = duk;
        this.selectedSlot = -1;
        this.page = 0;
    }
    
    public SpectatorPage getCurrentPage() {
        return new SpectatorPage(this.category, this.getItems(), this.selectedSlot);
    }
    
    static {
        CLOSE_ITEM = new CloseSpectatorItem();
        SCROLL_LEFT = new ScrollMenuItem(-1, true);
        SCROLL_RIGHT_ENABLED = new ScrollMenuItem(1, true);
        SCROLL_RIGHT_DISABLED = new ScrollMenuItem(1, false);
        EMPTY_SLOT = new SpectatorMenuItem() {
            @Override
            public void selectItem(final SpectatorMenu duj) {
            }
            
            @Override
            public Component getName() {
                return TextComponent.EMPTY;
            }
            
            @Override
            public void renderIcon(final PoseStack dhl, final float float2, final int integer) {
            }
            
            @Override
            public boolean isEnabled() {
                return false;
            }
        };
    }
    
    static class CloseSpectatorItem implements SpectatorMenuItem {
        private CloseSpectatorItem() {
        }
        
        @Override
        public void selectItem(final SpectatorMenu duj) {
            duj.exit();
        }
        
        @Override
        public Component getName() {
            return new TranslatableComponent("spectatorMenu.close");
        }
        
        @Override
        public void renderIcon(final PoseStack dhl, final float float2, final int integer) {
            Minecraft.getInstance().getTextureManager().bind(SpectatorGui.SPECTATOR_LOCATION);
            GuiComponent.blit(dhl, 0, 0, 128.0f, 0.0f, 16, 16, 256, 256);
        }
        
        @Override
        public boolean isEnabled() {
            return true;
        }
    }
    
    static class ScrollMenuItem implements SpectatorMenuItem {
        private final int direction;
        private final boolean enabled;
        
        public ScrollMenuItem(final int integer, final boolean boolean2) {
            this.direction = integer;
            this.enabled = boolean2;
        }
        
        @Override
        public void selectItem(final SpectatorMenu duj) {
            duj.page += this.direction;
        }
        
        @Override
        public Component getName() {
            if (this.direction < 0) {
                return new TranslatableComponent("spectatorMenu.previous_page");
            }
            return new TranslatableComponent("spectatorMenu.next_page");
        }
        
        @Override
        public void renderIcon(final PoseStack dhl, final float float2, final int integer) {
            Minecraft.getInstance().getTextureManager().bind(SpectatorGui.SPECTATOR_LOCATION);
            if (this.direction < 0) {
                GuiComponent.blit(dhl, 0, 0, 144.0f, 0.0f, 16, 16, 256, 256);
            }
            else {
                GuiComponent.blit(dhl, 0, 0, 160.0f, 0.0f, 16, 16, 256, 256);
            }
        }
        
        @Override
        public boolean isEnabled() {
            return this.enabled;
        }
    }
}
