<?php

namespace ServerManager\UI;

use PHPNative\Framework\Settings;
use PHPNative\Tailwind\Data\Icon as IconName;
use PHPNative\Ui\Widget\Button;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Icon;
use PHPNative\Ui\Widget\Label;
use PHPNative\Ui\Widget\MenuBar;
use PHPNative\Ui\Widget\Modal;
use PHPNative\Ui\Widget\TextInput;

class SettingsModal
{
    private Modal $modal;
    private TextInput $apiKeyInput;
    private TextInput $privateKeyPathInput;
    private TextInput $remoteStartDirInput;
    private TextInput $doneBoardInput;

    public function __construct(Settings $settings, MenuBar $menuBar, string &$apiKey, string &$privateKeyPath, string &$remoteStartDir)
    {
        // Create input fields
        $this->apiKeyInput = new TextInput('API Key', 'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black');
        $this->privateKeyPathInput = new TextInput(
            'Private Key Path',
            'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black'
        );
        $this->remoteStartDirInput = new TextInput(
            'Remote Start Directory',
            'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black'
        );
        $this->doneBoardInput = new TextInput(
            'Fertig-Board (z.B. fertig)',
            'w-full border border-gray-300 rounded px-3 py-2 bg-white text-black'
        );

        // Set initial values
        $this->apiKeyInput->setValue($apiKey);
        $this->privateKeyPathInput->setValue($privateKeyPath);
        $this->remoteStartDirInput->setValue($remoteStartDir);
        $doneBoard = (string) $settings->get('kanban.done_board', 'fertig');
        $this->doneBoardInput->setValue($doneBoard);

        // Create modal dialog
        $modalDialog = new Container('bg-white rounded-lg p-6 flex flex-col w-96 gap-3');
        $modalDialog->addComponent(new Label('API Einstellungen', 'text-xl font-bold text-black'));
        $modalDialog->addComponent(new Label(
            'Bitte gib deinen API Key und den Pfad zum Private Key für SSH-Verbindungen ein.',
            'text-sm text-gray-700'
        ));

        // API Key field
        $fieldContainer = new Container('flex flex-col gap-1');
        $fieldContainer->addComponent(new Label('API Key', 'text-sm text-gray-600'));
        $fieldContainer->addComponent($this->apiKeyInput);
        $modalDialog->addComponent($fieldContainer);

        // Private Key field
        $privateKeyFieldContainer = new Container('flex flex-col gap-1');
        $privateKeyFieldContainer->addComponent(new Label('Private Key Pfad', 'text-sm text-gray-600'));
        $privateKeyFieldContainer->addComponent($this->privateKeyPathInput);
        $modalDialog->addComponent($privateKeyFieldContainer);

        // Remote Start Directory field
        $remoteStartDirFieldContainer = new Container('flex flex-col gap-1');
        $remoteStartDirFieldContainer->addComponent(new Label('Remote Start Verzeichnis (z.B. /var/www)', 'text-sm text-gray-600'));
        $remoteStartDirFieldContainer->addComponent($this->remoteStartDirInput);
        $modalDialog->addComponent($remoteStartDirFieldContainer);

        // Done-board field
        $doneBoardFieldContainer = new Container('flex flex-col gap-1');
        $doneBoardFieldContainer->addComponent(new Label(
            'Board-Name für erledigte Tasks (z.B. "fertig")',
            'text-sm text-gray-600'
        ));
        $doneBoardFieldContainer->addComponent($this->doneBoardInput);
        $modalDialog->addComponent($doneBoardFieldContainer);

        // Buttons
        $buttonRow = new Container('flex flex-row gap-2 justify-end');
        $cancelButton = new Button('Abbrechen', 'px-4 py-2 bg-gray-200 text-black rounded hover:bg-gray-300');
        $saveButton = new Button('Speichern', 'px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 flex items-center');
        $saveIcon = new Icon(IconName::save, 18, 'text-white mr-2');
        $saveButton->setIcon($saveIcon);
        $buttonRow->addComponent($cancelButton);
        $buttonRow->addComponent($saveButton);
        $modalDialog->addComponent($buttonRow);

        $this->modal = new Modal($modalDialog, 'flex items-center justify-center bg-black', 200);

        // Setup button handlers - use references directly in closures
        $settingsModal = $this;
        $apiKeyInputRef = $this->apiKeyInput;
        $privateKeyPathInputRef = $this->privateKeyPathInput;
        $remoteStartDirInputRef = $this->remoteStartDirInput;
        $doneBoardInputRef = $this->doneBoardInput;

        $cancelButton->setOnClick(function () use ($menuBar, $settingsModal) {
            $menuBar->closeAllMenus();
            $settingsModal->hide();
        });

        $saveButton->setOnClick(function () use (
            $menuBar,
            $settingsModal,
            &$apiKey,
            &$privateKeyPath,
            &$remoteStartDir,
            $settings,
            $apiKeyInputRef,
            $privateKeyPathInputRef,
            $remoteStartDirInputRef,
            $doneBoardInputRef
        ) {
            $apiKey = trim($apiKeyInputRef->getValue());
            $privateKeyPath = trim($privateKeyPathInputRef->getValue());
            $remoteStartDir = trim($remoteStartDirInputRef->getValue());
            $doneBoard = trim($doneBoardInputRef->getValue());
            if ($doneBoard === '') {
                $doneBoard = 'fertig';
            }

            $settings->set('api_key', $apiKey);
            $settings->set('private_key_path', $privateKeyPath);
            $settings->set('remote_start_dir', $remoteStartDir);
            $settings->set('kanban.done_board', $doneBoard);
            $settings->save();

            $menuBar->closeAllMenus();
            $settingsModal->hide();
        });
    }

    public function getModal(): Modal
    {
        return $this->modal;
    }

    public function show(): void
    {
        $this->modal->setVisible(true);
    }

    public function hide(): void
    {
        $this->modal->setVisible(false);
    }
}
