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

Item {
    property string recpass: ""

    function dbConnectionFailed(msg) {
        notifications.notificationDialog.informativeText = msg;
        notifications.notificationDialog.text = "Verbindung zum Datenbankserver verloren";
        notifications.notificationDialog.open();
    }
    function getEncryptionKey() {
        recoveryPaswordDialog.open();
    }
    function loggedin() {
        contentStack.replace("Dashboard.qml");
    }

    anchors.fill: parent
    anchors.topMargin: Dimensions.l

    Component.onCompleted: {
        loggedin_user.loginOkay.connect(loggedin);
        config.invalidEncryptionKey.connect(getEncryptionKey);
        config.checkEncryptionKey();
        loggedin_user.noDbConnection.connect(dbConnectionFailed);
        benutzerName.forceActiveFocus();
    }

    ColumnLayout {
        anchors.centerIn: parent
        spacing: Dimensions.m

        Label {
            Layout.alignment: Qt.AlignHCenter
            Layout.bottomMargin: Dimensions.l
            font: Typography.h1
            text: qsTr("Login")
        }
        Field {
            label: qsTr("Benutzername")

            TextField {
                id: benutzerName

                focus: true
                implicitWidth: 300
                placeholderText: qsTr("Benutzernamen eingeben")

                onAccepted: {
                    if (benutzerName.text.trim() && passwort.text.trim())
                        loggedin_user.login(benutzerName.text.trim(), passwort.text);
                    else if (benutzerName.text.trim())
                        passwort.forceActiveFocus();
                }
            }
        }
        Field {
            label: qsTr("Passwort")

            TextField {
                id: passwort

                echoMode: TextInput.Password
                implicitWidth: 300
                placeholderText: qsTr("Passwort eingeben")

                onAccepted: {
                    if (benutzerName.text.trim() && passwort.text.trim())
                        loggedin_user.login(benutzerName.text.trim(), passwort.text);
                    else if (passwort.text.trim())
                        benutzerName.forceActiveFocus();
                }
            }
        }
        Button {
            Layout.topMargin: Dimensions.m
            icon.source: "qrc:/images/ArrowRightEndOnRectangle.svg"
            implicitWidth: parent.width
            text: qsTr("Login")

            onClicked: {
                if (benutzerName.text.trim() && passwort.text.trim())
                    loggedin_user.login(benutzerName.text.trim(), passwort.text);
            }
        }
        Item {
            Layout.fillHeight: true
        }
        Dialog {
            id: recoveryPaswordDialog

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

            onAccepted: {
                recpass = recoveryPaswordInput.text;
                getRecoveryDialog.open();
            }

            ColumnLayout {
                RowLayout {
                    Label {
                        text: qsTr("Wiederherstellungspasswort eingeben: ")
                    }
                    TextField {
                        id: recoveryPaswordInput

                        echoMode: TextInput.Password
                        implicitWidth: 300
                        placeholderText: qsTr("Hier Wiederherstellungspasswort eingeben")
                        text: ""
                    }
                }
            }
        }
        FileDialog {
            id: getRecoveryDialog

            currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
            fileMode: FileDialog.OpenFile
            nameFilters: ["PYQCRM Recovery files (*.pyqrec)"]
            title: qsTr("Wiederherstellungsdatei")

            onAccepted: config.getRecoveryKey(getRecoveryDialog.currentFile, recpass)
            onRejected: quit()
        }
        Notifications {
            id: notifications
        }
    }
}
