import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Dialogs
import QtCore

ApplicationWindow {
    id: appWindow

    property string confile: ""
    property alias settingsFileDialog: settingsFiledialog

    function showWindow(why) {
        if (why === 3) {
            systray.setVisible(false);
            appWindow.show();
        }
    }

    font: Typography.body
    height: Screen.height * .85
    palette.window: Colors.mantle
    palette.placeholderText: Colors.interactive
    palette.text: Colors.foreground
    title: "TERO Personal"
    visible: true
    width: Screen.width * .75

    Component.onCompleted: {
        systray.activated.connect(showWindow);

        if (bad_config) {
            importDialog.open();
        } else {
            if (db_con)
                contentStack.replace("LoginScreen.qml")
            else
                contentStack.replace("NoDbConnection.qml");
        }
    }
    onClosing: close => {
        if (false) {
            console.log("Main window closed!! Was soll ich tun? kann ich mich beenden?!");
        }
    }
    onVisibilityChanged: {
        if (appWindow.visibility === Window.Minimized && config.systray()) {
            systray.setVisible(true);
            appWindow.hide();
        }
    }
    onWindowStateChanged: windowState => {
        if (windowState !== Qt.WindowMinimized) {
            systray.setVisible(false);
            appWindow.show();
        }
    }

    Navigation {
        id: navigation

        visible: !(bad_config || !db_con)
    }
    PrinterDialog {
        id: printerDialog

    }
    ReadMe {
        id: readMeWin

    }
    Item {
        id: mainView

    }

    Rectangle {
        id: contentBackground
        anchors {
            bottom: parent.bottom
            left: navigation.visible ? navigation.right : parent.left
            right: parent.right
            top: parent.top
        }
        color: Colors.background
    }

    StackView {
        id: contentStack

        anchors {
            fill: contentBackground
            margins: Dimensions.l
        }
    }

    Dialog {
        id: importDialog

        anchors.centerIn: parent
        modal: true
        standardButtons: Dialog.Yes | Dialog.No
        title: qsTr("Einstellungen importieren")

        onAccepted: settingsFiledialog.open()
        onRejected: contentStack.replace("Firststart.qml")
    }
    FileDialog {
        id: settingsFiledialog

        currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
        modality: "ApplicationModal"
        nameFilters: [qsTr("PYQCRM Einstellungen (*.pyqrec)")]
        title: qsTr("PYQCRM Einstellungen")

        onAccepted: {
            exportFilePassword.open();
            confile = selectedFile;
        }
    }
    Dialog {
        id: exportFilePassword

        anchors.centerIn: parent
        modal: true
        standardButtons: Dialog.Ok | Dialog.Cancel
        title: qsTr("PYQCRM Einstellungen")

        onAccepted: config.importConfig(confile, exportPasswordInput.text)

        ColumnLayout {
            RowLayout {
                Label {
                    text: qsTr("Passwort eingeben:")
                }
                TextField {
                    id: exportPasswordInput

                    echoMode: TextInput.Password
                    implicitWidth: 300
                }
            }
        }
    }
}
