package net.halbear.skyboxmod.utility;

import net.halbear.skyboxmod.SkyboxMod;
import net.halbear.skyboxmod.rendering.screen.ConfigScreen;
import net.halbear.skyboxmod.rendering.skybox.DistantSkyObject;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.options.OptionsScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.client.event.ScreenEvent;

import java.util.function.Supplier;

@EventBusSubscriber(modid = SkyboxMod.MODID, value = Dist.CLIENT)
public class ClientEventHandler {

    @SubscribeEvent
    public static void onClientTick(ClientTickEvent.Pre event) {
        Minecraft mc = Minecraft.getInstance();
        if (mc.level != null && mc.player != null) {
            DistantSkyObject.TickUpdate(mc.level.dimension());
        }
    }

    private static boolean isSkinCustomizationButton(Component component) {
        if (component.getContents() instanceof TranslatableContents translatable) {
            return "options.skinCustomisation".equals(translatable.getKey());
        }
        return component.getString().contains("Skin Customization");
    }

   @SubscribeEvent
    public static void onScreenInit(ScreenEvent.Init.Post event) {
        if (event.getScreen() instanceof OptionsScreen pauseScreen) {
            for (var listener : event.getListenersList()) {
                if (listener instanceof AbstractWidget widget) {
                    Component message = widget.getMessage();
                    SkyboxMod.LOGGER.debug(message.getString());
                    if (isSkinCustomizationButton(message)) {

                        int targetX = widget.getX();
                        int targetY = widget.getY();
                        int targetWidth = widget.getWidth();
                        int targetHeight = widget.getHeight();
                        SkyboxMod.LOGGER.debug("New Button Tiiime");

                        int myButtonX = targetX;
                        int myButtonY = targetY - targetHeight - 4;

                        Button customMenuButton= new Button(
                                myButtonX, myButtonY, widget.getWidth(), widget.getHeight(),
                                Component.translatable("hals_skybox_mod.gui.optionsbutton"),
                                button -> {
                                    SkyboxMod.LOGGER.debug("Cliiick!");
                                    net.minecraft.client.Minecraft.getInstance().setScreen(
                                            new ConfigScreen(pauseScreen,pauseScreen.getMinecraft().options)
                                    );
                                }, new Button.CreateNarration() {
                            @Override
                            public MutableComponent createNarrationMessage(Supplier<MutableComponent> supplier) {
                                return Component.translatable("hals_skybox_mod.gui.optionsbutton");
                            }
                        }
                        ) {
                            @Override
                            protected void extractContents(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, float partialTick) {
                                this.setX(widget.getX());
                                this.setY(widget.getY() - widget.getHeight() - 4);
                                this.setWidth(targetWidth);
                                this.setHeight(targetHeight);

                                this.extractDefaultSprite(guiGraphics);
                                this.extractDefaultLabel(guiGraphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE));
                               // SkyboxMod.LOGGER.debug("Resize: xy:" + widget.getX() + "_" + widget.getY() + " wh:" + targetWidth + "x" + targetHeight);
                            }
                        };

                        event.getScreen().renderables.add(customMenuButton);
                        event.addListener(customMenuButton);
                        break;
                    }
                }
            }
        }
    }
}
