package xyz.tbvns.rogue_block.client.Gui;

import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.container.Containers;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.GridLayout;
import io.wispforest.owo.ui.container.StackLayout;
import io.wispforest.owo.ui.core.*;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
import xyz.tbvns.rogue_block.Upgrades.Upgrade;
import xyz.tbvns.rogue_block.Upgrades.UpgradesManager;

import java.util.ArrayList;
import java.util.List;

public class UpgradeScreen extends BaseOwoScreen<FlowLayout> {
    static double posX = 0;
    static double posY = 0;

    public static Text toolTip;
    static List<UpgradeComponent> components = new ArrayList<>();

    @Override
    protected @NotNull OwoUIAdapter<FlowLayout> createAdapter() {
        return OwoUIAdapter.create(this, Containers::horizontalFlow);
    }

    @Override
    protected void build(FlowLayout rootComponent) {
        posX = 0; posY = 0;
        components.clear();
        toolTip = null;

        rootComponent.mouseDrag().subscribe((mouseX, mouseY, deltaX, deltaY, button) -> {
            posX += deltaX;
            posY += deltaY;
            for (UpgradeComponent component : components) {
                component.x += deltaX;
                component.y += deltaY;
                component.itemComponent.moveTo((int) component.x, (int) component.y);
                component.textureComponent.moveTo((int) (component.x - 2), (int) (component.y - 2));
            }

            return true;
        });
        rootComponent.surface(Surface.blur(10, 10))
                     .horizontalAlignment(HorizontalAlignment.CENTER)
                     .verticalAlignment(VerticalAlignment.CENTER);

        generateUpgradeComponent(rootComponent);
    }

    @Override
    public void render(DrawContext context, int mouseX, int mouseY, float delta) {
        if (toolTip != null && !toolTip.equals(Text.empty())) {
            setTooltip(toolTip);
        }

        for (UpgradeComponent component : components) {
            component.drawLine(context);
        }
        super.render(context, mouseX, mouseY, delta);
    }

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

    public void generateUpgradeComponent(FlowLayout rootComponent) {
        for (Upgrade upgrade : UpgradesManager.registeredUpgrades) {
            UpgradeComponent component = new UpgradeComponent(upgrade);
            component.add(rootComponent);
            components.add(component);
        }
    }

    public static void update() {
        for (UpgradeComponent upgradeComponent : UpgradeScreen.components) {
            if (!upgradeComponent.isHovered) {
                upgradeComponent.textureComponent.positioning().set(Positioning.absolute((int) upgradeComponent.x - 2, (int) upgradeComponent.y - 2));
                upgradeComponent.itemComponent.positioning().set(Positioning.absolute((int) upgradeComponent.x, (int) upgradeComponent.y));
            } else {
                upgradeComponent.textureComponent.positioning().set(Positioning.absolute((int) upgradeComponent.x - 4, (int) upgradeComponent.y - 4));
                upgradeComponent.itemComponent.positioning().set(Positioning.absolute((int) upgradeComponent.x - 2, (int) upgradeComponent.y - 2));
            }
        }
    }
}
