package de.winniepat.citrus.cosmetics;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.UUID;

public class CosmeticScreen extends Screen {
    private static final int BUTTON_WIDTH = 200;
    private static final int BUTTON_HEIGHT = 20;
    private static final int PADDING = 5;

    private UUID playerUUID;

    public CosmeticScreen() {
        super(Text.of("Cosmetics"));
        MinecraftClient client = MinecraftClient.getInstance();
        this.playerUUID = client.player != null ? client.player.getUuid() : null;
    }

    @Override
    protected void init() {
        if (playerUUID == null) {
            this.close();
            return;
        }

        int centerX = this.width / 2;
        int y = this.height / 4;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§6§lCOSMETICS").copy().formatted(Formatting.BOLD),
                button -> {}
        ).dimensions(centerX - 100, y - 30, 200, 20).build());

        y += 40;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§e§lWINGS").copy().formatted(Formatting.BOLD),
                button -> {}
        ).dimensions(centerX - 100, y, 200, 20).build());
        y += 25;

        String wingsStatus = ClientCosmeticManager.areWingsVisible(playerUUID) ? "§aVisible" : "§cHidden";
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Toggle Wings: " + wingsStatus),
                button -> {
                    ClientCosmeticManager.toggleWings(playerUUID);
                    this.clearAndInit();
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());
        y += 25;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Select Wing Type"),
                button -> {
                    MinecraftClient.getInstance().setScreen(new WingSelectionScreen());
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());

        y += 40;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§e§lHAT").copy().formatted(Formatting.BOLD),
                button -> {}
        ).dimensions(centerX - 100, y, 200, 20).build());
        y += 25;

        String hatStatus = ClientCosmeticManager.isHatVisible(playerUUID) ? "§aVisible" : "§cHidden";
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Toggle Hat: " + hatStatus),
                button -> {
                    ClientCosmeticManager.toggleHat(playerUUID);
                    this.clearAndInit();
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());
        y += 25;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Select Hat Type"),
                button -> {
                    MinecraftClient.getInstance().setScreen(new HatSelectionScreen());
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());

        y += 40;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§e§lHALO").copy().formatted(Formatting.BOLD),
                button -> {}
        ).dimensions(centerX - 100, y, 200, 20).build());
        y += 25;

        String haloStatus = ClientCosmeticManager.isHaloVisible(playerUUID) ? "§aVisible" : "§cHidden";
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Toggle Halo: " + haloStatus),
                button -> {
                    ClientCosmeticManager.toggleHalo(playerUUID);
                    this.clearAndInit();
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());
        y += 25;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("Select Halo Type"),
                button -> {
                    MinecraftClient.getInstance().setScreen(new HaloSelectionScreen());
                }
        ).dimensions(centerX - 100, y, 200, BUTTON_HEIGHT).build());

        y += 50;

        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§cClose"),
                button -> this.close()
        ).dimensions(centerX - 50, y, 100, BUTTON_HEIGHT).build());
    }

    @Override
    public void render(DrawContext context, int mouseX, int mouseY, float delta) {
        this.renderBackground(context, mouseX, mouseY, delta);
        super.render(context, mouseX, mouseY, delta);

        context.drawCenteredTextWithShadow(
                this.textRenderer,
                Text.of("§6§lCosmetics Menu").copy().formatted(Formatting.BOLD),
                this.width / 2,
                30,
                0xFFFFFF
        );
    }

    @Override
    public boolean shouldPause() {
        return false;
    }
}

class WingSelectionScreen extends Screen {
    private final de.winniepat.citrus.cosmetics.wings.WingType[] wingTypes;

    public WingSelectionScreen() {
        super(Text.of("Select Wings"));
        wingTypes = de.winniepat.citrus.cosmetics.wings.WingType.getAvailableTypes();
    }

    @Override
    protected void init() {
        int centerX = this.width / 2;
        int y = 60;

        for (de.winniepat.citrus.cosmetics.wings.WingType type : wingTypes) {
            if (type == de.winniepat.citrus.cosmetics.wings.WingType.NONE) continue;

            this.addDrawableChild(ButtonWidget.builder(
                    Text.of("Select: " + type.getId()),
                    button -> {
                        UUID uuid = MinecraftClient.getInstance().player.getUuid();
                        ClientCosmeticManager.setActiveWings(uuid, type.getId());
                        this.close();
                    }
            ).dimensions(centerX - 100, y, 200, 20).build());
            y += 25;
        }

        y += 20;
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§cBack"),
                button -> this.close()
        ).dimensions(centerX - 50, y, 100, 20).build());
    }
}

class HatSelectionScreen extends Screen {
    private final de.winniepat.citrus.cosmetics.hat.HatType[] hatTypes;

    public HatSelectionScreen() {
        super(Text.of("Select Hat"));
        hatTypes = de.winniepat.citrus.cosmetics.hat.HatType.getAvailableTypes();
    }

    @Override
    protected void init() {
        int centerX = this.width / 2;
        int y = 60;

        for (de.winniepat.citrus.cosmetics.hat.HatType type : hatTypes) {
            if (type == de.winniepat.citrus.cosmetics.hat.HatType.NONE) continue;

            this.addDrawableChild(ButtonWidget.builder(
                    Text.of("Select: " + type.getId()),
                    button -> {
                        UUID uuid = MinecraftClient.getInstance().player.getUuid();
                        ClientCosmeticManager.setActiveHat(uuid, type.getId());
                        this.close();
                    }
            ).dimensions(centerX - 100, y, 200, 20).build());
            y += 25;
        }

        y += 20;
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§cBack"),
                button -> this.close()
        ).dimensions(centerX - 50, y, 100, 20).build());
    }
}

class HaloSelectionScreen extends Screen {
    private final de.winniepat.citrus.cosmetics.halo.HaloType[] haloTypes;

    public HaloSelectionScreen() {
        super(Text.of("Select Halo"));
        haloTypes = de.winniepat.citrus.cosmetics.halo.HaloType.getAvailableTypes();
    }

    @Override
    protected void init() {
        int centerX = this.width / 2;
        int y = 60;

        for (de.winniepat.citrus.cosmetics.halo.HaloType type : haloTypes) {
            if (type == de.winniepat.citrus.cosmetics.halo.HaloType.NONE) continue;

            this.addDrawableChild(ButtonWidget.builder(
                    Text.of("Select: " + type.getId()),
                    button -> {
                        UUID uuid = MinecraftClient.getInstance().player.getUuid();
                        ClientCosmeticManager.setActiveHalo(uuid, type.getId());
                        this.close();
                    }
            ).dimensions(centerX - 100, y, 200, 20).build());
            y += 25;
        }

        y += 20;
        this.addDrawableChild(ButtonWidget.builder(
                Text.of("§cBack"),
                button -> this.close()
        ).dimensions(centerX - 50, y, 100, 20).build());
    }
}