<?php

declare(strict_types=1);

namespace App\Framework\Console\Components;

use App\Framework\Console\ConsoleColor;
use App\Framework\Console\ConsoleOutputInterface;

/**
 * Menu bar with clickable entries
 */
final readonly class MenuBar
{
    /**
     * @param array<string> $items Menu items (e.g. ['Datei', 'Bearbeiten', 'Ansicht', 'Hilfe'])
     */
    public function __construct(
        private array $items
    ) {
    }

    /**
     * Render the menu bar at the top of the screen
     */
    public function render(ConsoleOutputInterface $output, ?string $activeMenu = null, int $screenWidth = 80): void
    {
        $menuLine = '';
        $separator = ' | ';

        foreach ($this->items as $index => $item) {
            if ($index > 0) {
                $menuLine .= $separator;
            }

            $isActive = $item === $activeMenu;
            $color = $isActive ? ConsoleColor::BRIGHT_WHITE : ConsoleColor::WHITE;
            $prefix = $isActive ? '▶ ' : '  ';

            $menuLine .= "{$prefix}{$item}";
        }

        // Pad to screen width
        $padding = max(0, $screenWidth - mb_strlen($menuLine));
        $menuLine .= str_repeat(' ', $padding);

        $output->writeLine($menuLine, ConsoleColor::BRIGHT_WHITE);
        $output->writeLine(str_repeat('═', $screenWidth), ConsoleColor::GRAY);
    }

    /**
     * Check if a mouse click is within the menu bar area
     */
    public function isClickInMenuArea(int $x, int $y): bool
    {
        // Menu bar is always at y=1 (first line)
        return $y === 1 && $x >= 1;
    }

    /**
     * Get menu item at click position
     */
    public function getItemAtPosition(int $x): ?string
    {
        // Simple calculation: approximate position based on item lengths
        // This is a simplified version - in a real implementation, you'd track exact positions
        $currentPos = 3; // Start after initial padding

        foreach ($this->items as $item) {
            $itemLength = mb_strlen($item) + 4; // Include prefix and spacing
            if ($x >= $currentPos && $x < $currentPos + $itemLength) {
                return $item;
            }
            $currentPos += $itemLength + 3; // Add separator length
        }

        return null;
    }

    /**
     * Get hotkey for a menu item (first letter)
     */
    public function getHotkeyForItem(string $item): ?string
    {
        $firstChar = mb_strtoupper(mb_substr($item, 0, 1));
        if ($firstChar === '') {
            return null;
        }

        return $firstChar;
    }

    /**
     * Find menu item by hotkey
     */
    public function getItemByHotkey(string $hotkey): ?string
    {
        $hotkeyUpper = mb_strtoupper($hotkey);

        foreach ($this->items as $item) {
            if (mb_strtoupper(mb_substr($item, 0, 1)) === $hotkeyUpper) {
                return $item;
            }
        }

        return null;
    }

    /**
     * Get all menu items
     */
    public function getItems(): array
    {
        return $this->items;
    }
}

