package com.mojang.realmsclient.gui;

import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.gui.GuiComponent;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.util.Mth;
import com.mojang.realmsclient.util.RealmsTextureManager;
import net.minecraft.client.Minecraft;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.datafixers.util.Pair;
import com.mojang.realmsclient.dto.RealmsWorldOptions;
import net.minecraft.network.chat.TextComponent;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import java.util.function.Consumer;
import com.mojang.realmsclient.dto.RealmsServer;
import java.util.function.Supplier;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.components.TickableWidget;
import net.minecraft.client.gui.components.Button;

public class RealmsWorldSlotButton extends Button implements TickableWidget {
    public static final ResourceLocation SLOT_FRAME_LOCATION;
    public static final ResourceLocation EMPTY_SLOT_LOCATION;
    public static final ResourceLocation DEFAULT_WORLD_SLOT_1;
    public static final ResourceLocation DEFAULT_WORLD_SLOT_2;
    public static final ResourceLocation DEFAULT_WORLD_SLOT_3;
    private final Supplier<RealmsServer> serverDataProvider;
    private final Consumer<Component> toolTipSetter;
    private final int slotIndex;
    private int animTick;
    @Nullable
    private State state;
    
    public RealmsWorldSlotButton(final int integer1, final int integer2, final int integer3, final int integer4, final Supplier<RealmsServer> supplier, final Consumer<Component> consumer, final int integer7, final OnPress a) {
        super(integer1, integer2, integer3, integer4, TextComponent.EMPTY, a);
        this.serverDataProvider = supplier;
        this.slotIndex = integer7;
        this.toolTipSetter = consumer;
    }
    
    @Nullable
    public State getState() {
        return this.state;
    }
    
    @Override
    public void tick() {
        ++this.animTick;
        final RealmsServer dip2 = this.serverDataProvider.get();
        if (dip2 == null) {
            return;
        }
        final RealmsWorldOptions div5 = dip2.slots.get(this.slotIndex);
        final boolean boolean10 = this.slotIndex == 4;
        boolean boolean11;
        String string4;
        long long6;
        String string5;
        boolean boolean12;
        if (boolean10) {
            boolean11 = (dip2.worldType == RealmsServer.WorldType.MINIGAME);
            string4 = "Minigame";
            long6 = dip2.minigameId;
            string5 = dip2.minigameImage;
            boolean12 = (dip2.minigameId == -1);
        }
        else {
            boolean11 = (dip2.activeSlot == this.slotIndex && dip2.worldType != RealmsServer.WorldType.MINIGAME);
            string4 = div5.getSlotName(this.slotIndex);
            long6 = div5.templateId;
            string5 = div5.templateImage;
            boolean12 = div5.empty;
        }
        final Action a11 = getAction(dip2, boolean11, boolean10);
        final Pair<Component, Component> pair12 = this.getTooltipAndNarration(dip2, string4, boolean12, boolean10, a11);
        this.state = new State(boolean11, string4, long6, string5, boolean12, boolean10, a11, (Component)pair12.getFirst());
        this.setMessage((Component)pair12.getSecond());
    }
    
    private static Action getAction(final RealmsServer dip, final boolean boolean2, final boolean boolean3) {
        if (boolean2) {
            if (!dip.expired && dip.state != RealmsServer.State.UNINITIALIZED) {
                return Action.JOIN;
            }
        }
        else {
            if (!boolean3) {
                return Action.SWITCH_SLOT;
            }
            if (!dip.expired) {
                return Action.SWITCH_SLOT;
            }
        }
        return Action.NOTHING;
    }
    
    private Pair<Component, Component> getTooltipAndNarration(final RealmsServer dip, final String string, final boolean boolean3, final boolean boolean4, final Action a) {
        if (a == Action.NOTHING) {
            return (Pair<Component, Component>)Pair.of(null, new TextComponent(string));
        }
        Component mr7;
        if (boolean4) {
            if (boolean3) {
                mr7 = TextComponent.EMPTY;
            }
            else {
                mr7 = new TextComponent(" ").append(string).append(" ").append(dip.minigameName);
            }
        }
        else {
            mr7 = new TextComponent(" ").append(string);
        }
        Component mr8;
        if (a == Action.JOIN) {
            mr8 = new TranslatableComponent("mco.configure.world.slot.tooltip.active");
        }
        else {
            mr8 = (boolean4 ? new TranslatableComponent("mco.configure.world.slot.tooltip.minigame") : new TranslatableComponent("mco.configure.world.slot.tooltip"));
        }
        final Component mr9 = mr8.copy().append(mr7);
        return (Pair<Component, Component>)Pair.of(mr8, mr9);
    }
    
    @Override
    public void renderButton(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        if (this.state == null) {
            return;
        }
        this.drawSlotFrame(dhl, this.x, this.y, integer2, integer3, this.state.isCurrentlyActiveSlot, this.state.slotName, this.slotIndex, this.state.imageId, this.state.image, this.state.empty, this.state.minigame, this.state.action, this.state.actionPrompt);
    }
    
    private void drawSlotFrame(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final boolean boolean6, final String string7, final int integer8, final long long9, @Nullable final String string10, final boolean boolean11, final boolean boolean12, final Action a, @Nullable final Component mr) {
        final boolean boolean13 = this.isHovered();
        if (this.isMouseOver(integer4, integer5) && mr != null) {
            this.toolTipSetter.accept(mr);
        }
        final Minecraft dlx18 = Minecraft.getInstance();
        final TextureManager elw19 = dlx18.getTextureManager();
        if (boolean12) {
            RealmsTextureManager.bindWorldTemplate(String.valueOf(long9), string10);
        }
        else if (boolean11) {
            elw19.bind(RealmsWorldSlotButton.EMPTY_SLOT_LOCATION);
        }
        else if (string10 != null && long9 != -1L) {
            RealmsTextureManager.bindWorldTemplate(String.valueOf(long9), string10);
        }
        else if (integer8 == 1) {
            elw19.bind(RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_1);
        }
        else if (integer8 == 2) {
            elw19.bind(RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_2);
        }
        else if (integer8 == 3) {
            elw19.bind(RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_3);
        }
        if (boolean6) {
            final float float20 = 0.85f + 0.15f * Mth.cos(this.animTick * 0.2f);
            RenderSystem.color4f(float20, float20, float20, 1.0f);
        }
        else {
            RenderSystem.color4f(0.56f, 0.56f, 0.56f, 1.0f);
        }
        GuiComponent.blit(dhl, integer2 + 3, integer3 + 3, 0.0f, 0.0f, 74, 74, 74, 74);
        elw19.bind(RealmsWorldSlotButton.SLOT_FRAME_LOCATION);
        final boolean boolean14 = boolean13 && a != Action.NOTHING;
        if (boolean14) {
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        }
        else if (boolean6) {
            RenderSystem.color4f(0.8f, 0.8f, 0.8f, 1.0f);
        }
        else {
            RenderSystem.color4f(0.56f, 0.56f, 0.56f, 1.0f);
        }
        GuiComponent.blit(dhl, integer2, integer3, 0.0f, 0.0f, 80, 80, 80, 80);
        this.drawCenteredString(dhl, dlx18.font, string7, integer2 + 40, integer3 + 66, 16777215);
    }
    
    static {
        SLOT_FRAME_LOCATION = new ResourceLocation("realms", "textures/gui/realms/slot_frame.png");
        EMPTY_SLOT_LOCATION = new ResourceLocation("realms", "textures/gui/realms/empty_frame.png");
        DEFAULT_WORLD_SLOT_1 = new ResourceLocation("minecraft", "textures/gui/title/background/panorama_0.png");
        DEFAULT_WORLD_SLOT_2 = new ResourceLocation("minecraft", "textures/gui/title/background/panorama_2.png");
        DEFAULT_WORLD_SLOT_3 = new ResourceLocation("minecraft", "textures/gui/title/background/panorama_3.png");
    }
    
    public enum Action {
        NOTHING, 
        SWITCH_SLOT, 
        JOIN;
    }
    
    public static class State {
        private final boolean isCurrentlyActiveSlot;
        private final String slotName;
        private final long imageId;
        private final String image;
        public final boolean empty;
        public final boolean minigame;
        public final Action action;
        @Nullable
        private final Component actionPrompt;
        
        State(final boolean boolean1, final String string2, final long long3, @Nullable final String string4, final boolean boolean5, final boolean boolean6, final Action a, @Nullable final Component mr) {
            this.isCurrentlyActiveSlot = boolean1;
            this.slotName = string2;
            this.imageId = long3;
            this.image = string4;
            this.empty = boolean5;
            this.minigame = boolean6;
            this.action = a;
            this.actionPrompt = mr;
        }
    }
}
