/*
 * Copyright 2026  Kevin Donnelly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http: //www.gnu.org/licenses/>.
 */

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC
import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami
import org.kde.plasma.components as PlasmaComponents

Window {
    id: manualAdd
    signal stationSelected(var station)
    signal open

    LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
    LayoutMirroring.childrenInherit: true

    flags: Qt.Dialog
    modality: Qt.WindowModal

    width: Kirigami.Units.gridUnit * 17
    height: Kirigami.Units.gridUnit * 10

    SystemPalette {
        id: syspal
    }

    function printDebug(msg) {
        if (plasmoid.configuration.logConsole) {
            console.log("[debug] [ManualStationAdd.qml] " + msg);
        }
    }

    onOpen: {
        manualAdd.visible = true;
        stationIDInput.text = "";
        stationNameInput.text = "";
    }

    title: i18n("Add Station...")
    color: syspal.window

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: Kirigami.Units.largeSpacing
        spacing: Kirigami.Units.largeSpacing

        QQC.TextField {
            id: stationIDInput
            Layout.fillWidth: true
            placeholderText: i18n("Enter station ID")
        }

        QQC.TextField {
            id: stationNameInput
            Layout.fillWidth: true
            placeholderText: i18n("Enter station name (optional)")
        }

        RowLayout {
            Layout.alignment: Qt.AlignRight
            spacing: Kirigami.Units.smallSpacing

            QQC.Button {
                text: i18n("Confirm")
                enabled: stationIDInput.text.trim().length > 0
                onClicked: {
                    var stationID = stationIDInput.text.trim();
                    var address = stationNameInput.text.trim().length > 0 ? stationNameInput.text.trim() : stationID;
                    manualAdd.stationSelected({stationID, address});
                    manualAdd.visible = false;
                }
            }

            QQC.Button {
                text: i18n("Cancel")
                onClicked: {
                    manualAdd.visible = false;
                }
            }
        }
    }
}
