<?php

namespace PHPNative\Ui\Widget;

use PHPNative\Framework\TextRenderer;

class Menu extends Container
{
    private Button $menuButton;
    private bool $isOpen = false;
    private Container $dropdown;
    private null|MenuBar $menuBar = null;

    public function __construct(string $title)
    {
        parent::__construct('');

        // Create menu button
        $this->menuButton = new Button($title, 'px-4 py-2 hover:bg-gray-200', fn() => $this->toggle());

        $this->addComponent($this->menuButton);

        // Create dropdown container (initially hidden)
        $this->dropdown = new Container('flex flex-col absolute bg-white border border-gray-300 shadow-lg z-50');
        $this->dropdown->setVisible(false);
        $this->dropdown->setOverlay(true); // Mark as overlay so it doesn't affect layout and renders on top
        $this->dropdown->setZIndex(100); // Dropdown menus at z-index 100 (modals could use 1000+)
        $this->addComponent($this->dropdown);
    }

    /**
     * Add a menu item
     *
     * @param string $text Menu item text
     * @param callable $onClick Click handler
     */
    public function addItem(string $text, callable $onClick): MenuItem
    {
        $menuItem = new MenuItem($text, function () use ($onClick) {
            // Close menu and execute action
            $this->close();
            $onClick();
        });

        $this->dropdown->addComponent($menuItem);

        return $menuItem;
    }

    /**
     * Add a separator
     */
    public function addSeparator(): void
    {
        $separator = new Separator(true, 'my-1');
        $this->dropdown->addComponent($separator);
    }

    public function toggle(): void
    {
        if ($this->isOpen) {
            $this->close();
        } else {
            $this->open();
        }
    }

    public function open(): void
    {
        if ($this->isOpen) {
            return;
        }
        $this->isOpen = true;
        $this->dropdown->setVisible(true);
        if ($this->menuBar !== null) {
            $this->menuBar->notifyMenuOpened($this);
        }
    }

    public function close(bool $notify = true): void
    {
        if (!$this->isOpen) {
            return;
        }
        $this->isOpen = false;
        $this->dropdown->setVisible(false);
        if ($notify && $this->menuBar !== null) {
            $this->menuBar->notifyMenuClosed($this);
        }
    }

    public function layout(null|TextRenderer $textRenderer = null): void
    {
        parent::layout($textRenderer);

        // Get the button's viewport to position dropdown correctly
        $buttonViewport = $this->menuButton->getViewport();
        $this->dropdown->getViewport()->x = $buttonViewport->x;
        $this->dropdown->getViewport()->y = $buttonViewport->y + $buttonViewport->height;
        $this->dropdown->getViewport()->width = 200; // Fixed width for dropdown
        $this->dropdown->getViewport()->windowWidth = $buttonViewport->windowWidth;
        $this->dropdown->getViewport()->windowHeight = $buttonViewport->windowHeight;
        $this->dropdown->layout($textRenderer);
    }

    public function handleMouseMove(float $mouseX, float $mouseY): void
    {
        parent::handleMouseMove($mouseX, $mouseY);
        if ($this->isOpen) {
            $this->dropdown->handleMouseMove($mouseX, $mouseY);
        }
    }

    public function render(&$renderer, null|TextRenderer $textRenderer = null): void
    {
        parent::render($renderer, $textRenderer);
    }

    public function renderContent(&$renderer, null|TextRenderer $textRenderer = null): void
    {
        parent::renderContent($renderer, $textRenderer);
    }

    public function setMenuBar(MenuBar $menuBar): void
    {
        $this->menuBar = $menuBar;
    }

    public function isOpen(): bool
    {
        return $this->isOpen;
    }
}
