package net.minecraft.client.gui.screens.debug;

import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.ItemStack;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.renderer.entity.ItemRenderer;
import com.mojang.blaze3d.platform.InputConstants;
import java.util.Iterator;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.client.gui.GuiComponent;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.GameType;
import com.google.common.collect.Lists;
import net.minecraft.client.gui.chat.NarratorChatListener;
import java.util.List;
import java.util.Optional;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.screens.Screen;

public class GameModeSwitcherScreen extends Screen {
    private static final ResourceLocation GAMEMODE_SWITCHER_LOCATION;
    private static final int ALL_SLOTS_WIDTH;
    private static final Component SELECT_KEY;
    private final Optional<GameModeIcon> previousHovered;
    private Optional<GameModeIcon> currentlyHovered;
    private int firstMouseX;
    private int firstMouseY;
    private boolean setFirstMousePos;
    private final List<GameModeSlot> slots;
    
    public GameModeSwitcherScreen() {
        super(NarratorChatListener.NO_TITLE);
        this.currentlyHovered = Optional.<GameModeIcon>empty();
        this.slots = Lists.newArrayList();
        this.previousHovered = getFromGameType(this.getDefaultSelected());
    }
    
    private GameType getDefaultSelected() {
        final GameType bpy2 = Minecraft.getInstance().gameMode.getPlayerMode();
        GameType bpy3 = Minecraft.getInstance().gameMode.getPreviousPlayerMode();
        if (bpy3 == GameType.NOT_SET) {
            if (bpy2 == GameType.CREATIVE) {
                bpy3 = GameType.SURVIVAL;
            }
            else {
                bpy3 = GameType.CREATIVE;
            }
        }
        return bpy3;
    }
    
    @Override
    protected void init() {
        super.init();
        this.currentlyHovered = (this.previousHovered.isPresent() ? this.previousHovered : getFromGameType(this.minecraft.gameMode.getPlayerMode()));
        for (int integer2 = 0; integer2 < GameModeIcon.VALUES.length; ++integer2) {
            final GameModeIcon a3 = GameModeIcon.VALUES[integer2];
            this.slots.add(new GameModeSlot(a3, this.width / 2 - GameModeSwitcherScreen.ALL_SLOTS_WIDTH / 2 + integer2 * 30, this.height / 2 - 30));
        }
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        if (this.checkToClose()) {
            return;
        }
        dhl.pushPose();
        RenderSystem.enableBlend();
        this.minecraft.getTextureManager().bind(GameModeSwitcherScreen.GAMEMODE_SWITCHER_LOCATION);
        final int integer4 = this.width / 2 - 62;
        final int integer5 = this.height / 2 - 30 - 27;
        GuiComponent.blit(dhl, integer4, integer5, 0.0f, 0.0f, 125, 75, 128, 128);
        dhl.popPose();
        super.render(dhl, integer2, integer3, float4);
        this.currentlyHovered.ifPresent(a -> this.drawCenteredString(dhl, this.font, a.getName(), this.width / 2, this.height / 2 - 30 - 20, -1));
        this.drawCenteredString(dhl, this.font, GameModeSwitcherScreen.SELECT_KEY, this.width / 2, this.height / 2 + 5, 16777215);
        if (!this.setFirstMousePos) {
            this.firstMouseX = integer2;
            this.firstMouseY = integer3;
            this.setFirstMousePos = true;
        }
        final boolean boolean8 = this.firstMouseX == integer2 && this.firstMouseY == integer3;
        for (final GameModeSlot b10 : this.slots) {
            b10.render(dhl, integer2, integer3, float4);
            final GameModeSlot b11;
            this.currentlyHovered.ifPresent(a -> b11.setSelected(a == b11.icon));
            if (!boolean8 && b10.isHovered()) {
                this.currentlyHovered = Optional.<GameModeIcon>of(b10.icon);
            }
        }
    }
    
    private void switchToHoveredGameMode() {
        switchToHoveredGameMode(this.minecraft, this.currentlyHovered);
    }
    
    private static void switchToHoveredGameMode(final Minecraft dlx, final Optional<GameModeIcon> optional) {
        if (dlx.gameMode == null || dlx.player == null || !optional.isPresent()) {
            return;
        }
        final Optional<GameModeIcon> optional2 = getFromGameType(dlx.gameMode.getPlayerMode());
        final GameModeIcon a4 = optional.get();
        if (optional2.isPresent() && dlx.player.hasPermissions(2) && a4 != optional2.get()) {
            dlx.player.chat(a4.getCommand());
        }
    }
    
    private boolean checkToClose() {
        if (!InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 292)) {
            this.switchToHoveredGameMode();
            this.minecraft.setScreen(null);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (integer1 == 293 && this.currentlyHovered.isPresent()) {
            this.setFirstMousePos = false;
            this.currentlyHovered = this.currentlyHovered.get().getNext();
            return true;
        }
        return super.keyPressed(integer1, integer2, integer3);
    }
    
    @Override
    public boolean isPauseScreen() {
        return false;
    }
    
    static {
        GAMEMODE_SWITCHER_LOCATION = new ResourceLocation("textures/gui/container/gamemode_switcher.png");
        ALL_SLOTS_WIDTH = GameModeIcon.values().length * 30 - 5;
        SELECT_KEY = new TranslatableComponent("debug.gamemodes.select_next", new Object[] { new TranslatableComponent("debug.gamemodes.press_f4").withStyle(ChatFormatting.AQUA) });
    }
    
    enum GameModeIcon {
        CREATIVE((Component)new TranslatableComponent("gameMode.creative"), "/gamemode creative", new ItemStack(Blocks.GRASS_BLOCK)), 
        SURVIVAL((Component)new TranslatableComponent("gameMode.survival"), "/gamemode survival", new ItemStack(Items.IRON_SWORD)), 
        ADVENTURE((Component)new TranslatableComponent("gameMode.adventure"), "/gamemode adventure", new ItemStack(Items.MAP)), 
        SPECTATOR((Component)new TranslatableComponent("gameMode.spectator"), "/gamemode spectator", new ItemStack(Items.ENDER_EYE));
        
        protected static final GameModeIcon[] VALUES;
        final Component name;
        final String command;
        final ItemStack renderStack;
        
        private GameModeIcon(final Component mr, final String string4, final ItemStack bki) {
            this.name = mr;
            this.command = string4;
            this.renderStack = bki;
        }
        
        private void drawIcon(final ItemRenderer ehh, final int integer2, final int integer3) {
            ehh.renderAndDecorateItem(this.renderStack, integer2, integer3);
        }
        
        private Component getName() {
            return this.name;
        }
        
        private String getCommand() {
            return this.command;
        }
        
        private Optional<GameModeIcon> getNext() {
            switch (this) {
                case CREATIVE: {
                    return Optional.<GameModeIcon>of(GameModeIcon.SURVIVAL);
                }
                case SURVIVAL: {
                    return Optional.<GameModeIcon>of(GameModeIcon.ADVENTURE);
                }
                case ADVENTURE: {
                    return Optional.<GameModeIcon>of(GameModeIcon.SPECTATOR);
                }
                default: {
                    return Optional.<GameModeIcon>of(GameModeIcon.CREATIVE);
                }
            }
        }
        
        private static Optional<GameModeIcon> getFromGameType(final GameType bpy) {
            switch (bpy) {
                case SPECTATOR: {
                    return Optional.<GameModeIcon>of(GameModeIcon.SPECTATOR);
                }
                case SURVIVAL: {
                    return Optional.<GameModeIcon>of(GameModeIcon.SURVIVAL);
                }
                case CREATIVE: {
                    return Optional.<GameModeIcon>of(GameModeIcon.CREATIVE);
                }
                case ADVENTURE: {
                    return Optional.<GameModeIcon>of(GameModeIcon.ADVENTURE);
                }
                default: {
                    return Optional.<GameModeIcon>empty();
                }
            }
        }
        
        static {
            VALUES = values();
        }
    }
    
    public class GameModeSlot extends AbstractWidget {
        private final GameModeIcon icon;
        private boolean isSelected;
        
        public GameModeSlot(final GameModeIcon a, final int integer3, final int integer4) {
            super(integer3, integer4, 25, 25, a.getName());
            this.icon = a;
        }
        
        @Override
        public void renderButton(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
            final Minecraft dlx6 = Minecraft.getInstance();
            this.drawSlot(dhl, dlx6.getTextureManager());
            this.icon.drawIcon(GameModeSwitcherScreen.this.itemRenderer, this.x + 5, this.y + 5);
            if (this.isSelected) {
                this.drawSelection(dhl, dlx6.getTextureManager());
            }
        }
        
        @Override
        public boolean isHovered() {
            return super.isHovered() || this.isSelected;
        }
        
        public void setSelected(final boolean boolean1) {
            this.isSelected = boolean1;
            this.narrate();
        }
        
        private void drawSlot(final PoseStack dhl, final TextureManager elw) {
            elw.bind(GameModeSwitcherScreen.GAMEMODE_SWITCHER_LOCATION);
            dhl.pushPose();
            dhl.translate(this.x, this.y, 0.0);
            GuiComponent.blit(dhl, 0, 0, 0.0f, 75.0f, 25, 25, 128, 128);
            dhl.popPose();
        }
        
        private void drawSelection(final PoseStack dhl, final TextureManager elw) {
            elw.bind(GameModeSwitcherScreen.GAMEMODE_SWITCHER_LOCATION);
            dhl.pushPose();
            dhl.translate(this.x, this.y, 0.0);
            GuiComponent.blit(dhl, 0, 0, 25.0f, 75.0f, 25, 25, 128, 128);
            dhl.popPose();
        }
    }
}
