package top.r3944realms.lib39.example.client.screen;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector2f;
import top.r3944realms.lib39.client.gui.component.WheelWidget;
import top.r3944realms.lib39.util.lang.FourConsumer;
import top.r3944realms.lib39.util.lang.Pair;

import java.util.List;
import java.util.Objects;

import static top.r3944realms.lib39.client.gui.component.WheelWidget.IGNORE_CURSOR_MOVE_LENGTH;

/**
 * The type Forge screen.
 */
public class ForgeScreen extends Screen {
    private final LocalPlayer player = Objects.requireNonNull(Minecraft.getInstance().player);
    private final InteractionHand hand;
    private final int mode;

    /**
     * The Wheel.
     */
    public WheelWidget wheel;

    /**
     * Instantiates a new Forge screen.
     *
     * @param hand the hand
     * @param mode the mode
     */
    public ForgeScreen(InteractionHand hand, int mode) {
        super(Component.literal("Test"));
        this.hand = hand;
        this.mode = mode;
    }

    @Override
    protected void init() {
        int leftPos = (this.width - 75) / 2;
        int topPos = (this.height - 75) / 2;
        ItemStack holding = player.getItemInHand(this.hand);
        WheelWidget wheel = new WheelWidget(
                leftPos, topPos, 75, 75,
                12.5f, 32.5f, 0.75f,
                List.of(
                        Pair.of(
                                Component.literal("auto"),
                                renderItem(holding)),
                        Pair.of(
                                Component.literal("axe"),
                                renderItem(holding)),
                        Pair.of(
                                Component.literal("shovel"),
                                renderItem(holding)),
                        Pair.of(
                                Component.literal("hoe"),
                                renderItem(holding)),
                        Pair.of(
                                Component.literal("pickaxe"),
                                renderItem(holding))
                )
        ).setCurrentIndex(this.wheel != null ? this.wheel.getCurrentSectionIndex() : this.mode);
        this.clearWidgets();
        this.wheel = this.addRenderableWidget(wheel);
    }

    @Contract(pure = true)
    private static @NotNull FourConsumer<GuiGraphics, PoseStack, Integer, Integer> renderItem(ItemStack holding) {
        return (graphics, pose, width, height) -> {
            ItemStack stack = holding.copy();
            graphics.renderItem(stack, 2, 2, 9910597);
        };
    }
    @Override
    public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) {
        if (wheel != null && wheel.isClosingAnimationStarted()) return true;
        float screenCenterX = this.width / 2f;
        float screenCenterY = this.height / 2f;
        Vector2f cursorVec2 = new Vector2f(
                (float) mouseX - screenCenterX,
                (float) mouseY - screenCenterY
        );
        if (cursorVec2.length() < IGNORE_CURSOR_MOVE_LENGTH) {
            return true;
        }
        return super.mouseDragged(mouseX, mouseY, button, dragX, dragY);
    }

    @Override
    public boolean mouseReleased(double mouseX, double mouseY, int button) {
        if (wheel != null ) {
            wheel.onClosing();
        }
        return super.mouseReleased(mouseX, mouseY, button);
    }

    @Override
    public void removed() {
        super.removed();
    }


    @Override
    public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        for (Renderable renderable : this.renderables) {
            renderable.render(guiGraphics, mouseX, mouseY, partialTick);
        }
    }

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

}
