package xyz.tbvns.rogue_block.client.Gui;

import io.wispforest.owo.ui.component.Components;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.GridLayout;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.event.MouseEnter;
import lombok.AllArgsConstructor;
import net.kyori.adventure.platform.fabric.FabricAudiences;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.tbvns.rogue_block.Upgrades.Upgrade;
import xyz.tbvns.rogue_block.Upgrades.UpgradeRarity;
import xyz.tbvns.rogue_block.Utils;
import xyz.tbvns.rogue_block.client.Rogue_blockClient;

import java.util.List;

public class UpgradeComponent {
    Upgrade upgrade;
    boolean isHovered = false;
    double x, y;

    Component textureComponent;
    Component itemComponent;

    public UpgradeComponent(Upgrade upgrade) {
        this.upgrade = upgrade;
        x = upgrade.getGuiPos()[0];
        y = upgrade.getGuiPos()[1];
    }

    public void add(FlowLayout layout) {
        Component item = Components.item(upgrade.itemDisplay());
        item.positioning().set(Positioning.absolute((int) x, (int) y));
        Component component = getTextureComponent(upgrade.getRarity().unobtained, item);

        layout.child(component);
        layout.child(item);

        textureComponent = component;
        itemComponent = item;
    }

    public void drawLine(DrawContext context) {
        if (upgrade.getParent() != null) {
            try {

                int pX = ((Upgrade) upgrade.getParent().getConstructor().newInstance()).getGuiPos()[0] + 8 + (int) UpgradeScreen.posX;
                int pY = ((Upgrade) upgrade.getParent().getConstructor().newInstance()).getGuiPos()[1] + 8 + (int) UpgradeScreen.posY;

                int tempX = upgrade.getGuiPos()[0] + 8 + (int) UpgradeScreen.posX;
                int tempY = upgrade.getGuiPos()[1] + 8 + (int) UpgradeScreen.posY;

                context.drawHorizontalLine( tempX - (tempX - pX),
                        tempX,
                        tempY,
                        new Color(1, 1, 1, 1).argb()
                );
                context.drawVerticalLine(tempX - (tempX - pX),
                        tempY - (tempY - pY),
                        tempY,
                        new Color(1, 1, 1, 1).argb()
                );
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public Component getTextureComponent(Identifier identifier, Component item) {
        Component component =
            Components.texture(
                    identifier,
                    1, 1, 256, 256
            ).sizing(
                    Sizing.fixed(21),
                    Sizing.fixed(21)
            );
        component.positioning().set(Positioning.absolute((int) (x - 2), (int) (y - 2)));
        UpgradeScreen.update();

        component.mouseEnter().subscribe(() -> {
            component.sizing(Sizing.fixed(25), Sizing.fixed(25));
            item.sizing(Sizing.fixed(20), Sizing.fixed(20));
            isHovered = true;
            UpgradeScreen.toolTip = Rogue_blockClient.audiences.toNative(upgrade.generateTooltip());
            UpgradeScreen.update();
        });

        component.mouseLeave().subscribe(() -> {
            component.sizing(Sizing.fixed(21), Sizing.fixed(21));
            item.sizing(Sizing.fixed(16), Sizing.fixed(16));
            isHovered = false;
            UpgradeScreen.toolTip = Text.empty();
            UpgradeScreen.update();
        });

        component.mouseDown().subscribe((mouseX, mouseY, button) -> {
            ParentComponent parentComponent = textureComponent.root();
            textureComponent.remove();
            FlowLayout layout = (FlowLayout) parentComponent;
            textureComponent = getTextureComponent(upgrade.getRarity().obtained, item);
            layout.child(textureComponent);
            return true;
        });
        return component;
    }
}
