<?php

namespace ServerManager;

use PHPNative\Framework\Application;
use PHPNative\Framework\Settings;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Label;
use PHPNative\Ui\Widget\StatusBar;
use PHPNative\Ui\Widget\TabContainer;
use PHPNative\Ui\Window;
use ServerManager\UI\KanbanTab;
use ServerManager\UI\MenuBarBuilder;
use ServerManager\UI\ServerListTab;
use ServerManager\UI\SettingsModal;
use ServerManager\UI\SftpManagerTab;

class App
{
    private Application $app;
    private Window $window;
    private Settings $settings;

    public function __construct()
    {
        // Initialize application and window
        $this->app = new Application();
        $this->window = new Window('Server Manager', 800, 600);
        $this->settings = new Settings('ServerManager');
    }

    public function run(): void
    {
        // Main container
        $mainContainer = new Container('flex flex-col bg-gray-100');

        // Status label (referenced by tabs)
        $statusLabel = new Label(
            text: 'Fenster: ' .
            $this->window->getViewport()->windowWidth .
                'x' .
                $this->window->getViewport()->windowHeight,
            style: 'basis-4/8 text-black',
        );

        // Settings variables (simple variables work better with async than object properties)
        $currentApiKey = $this->settings->get('api_key', '');
        $currentPrivateKeyPath = $this->settings->get('private_key_path', '');
        $currentRemoteStartDir = $this->settings->get('remote_start_dir', '/');

        // Create menu bar first
        $menuBar = new \PHPNative\Ui\Widget\MenuBar();
        $mainContainer->addComponent($menuBar);

        // Create settings modal with the real menu bar
        $settingsModal = new SettingsModal(
            $this->settings,
            $menuBar,
            $currentApiKey,
            $currentPrivateKeyPath,
            $currentRemoteStartDir,
        );
        $mainContainer->addComponent($settingsModal->getModal());

        // Build menu bar menus after modal is created
        MenuBarBuilder::buildMenus($this->app, $menuBar, $settingsModal);

        // Tab container
        $tabContainer = new TabContainer('flex-1');

        // Create tabs
        $kanbanTab = new KanbanTab($this->settings);
        $serverListTab = new ServerListTab(
            $currentApiKey,
            $currentPrivateKeyPath,
            $tabContainer,
            $statusLabel,
            $this->settings,
            $kanbanTab,
        );
        $kanbanTab->setServerListTab($serverListTab);
        $sftpManagerTab = new SftpManagerTab(
            $currentApiKey,
            $currentPrivateKeyPath,
            $currentRemoteStartDir,
            $serverListTab,
            $tabContainer,
            $statusLabel,
        );

        // Add tabs
        $tabContainer->addTab('Server', $serverListTab->getContainer());
        $tabContainer->addTab('Kanban', $kanbanTab->getContainer());
        $tabContainer->addTab('SFTP Manager', $sftpManagerTab->getContainer());

        $mainContainer->addComponent($tabContainer);

        // Add modals to main container (must be at top level for overlay to work)
        $mainContainer->addComponent($sftpManagerTab->getEditModal());
        $mainContainer->addComponent($sftpManagerTab->getFilenameModal());
        $mainContainer->addComponent($sftpManagerTab->getRenameModal());
        $mainContainer->addComponent($sftpManagerTab->getDeleteConfirmModal());

        // Status bar
        $statusBar = new StatusBar();
        $statusBar->addSegment($statusLabel);
        $statusBar->addSegment(new Label('v1.0', 'basis-1/8 text-center text-black border-l border-gray-300'));
        $statusBar->addSegment(new Label(
            'PHPNative Framework',
            'basis-3/8 text-right text-black pr-2 border-l border-gray-300',
        ));
        $mainContainer->addComponent($statusBar);

        // Tray disabled for now due to GTK/XKB issues with static builds
        // TODO: Re-enable after rebuilding static PHP with new SDL3 tray implementation
        if (function_exists('tray_setup')) {
            try {
                tray_setup('', ['Beenden']);
            } catch (\Throwable $e) {
                // Tray initialization failed, continue without tray
                error_log('Tray setup failed: ' . $e->getMessage());
            }
        }

        // Set window content and run
        $this->window->setRoot($mainContainer);
        $this->app->addWindow($this->window);
        $this->app->run();
    }
}
