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


Item
{
    property string recpass: ""
    anchors.fill: parent

    ColumnLayout
    {

        anchors.fill: parent

        FontLoader
        {
            id: helloStranger
            source: "qrc:/fonts/HelloStranger.otf"
        }

        FontLoader
        {
            id: damarWulan
            source: "qrc:/fonts/Damarwulan.ttf"
        }

        FontLoader
        {
            id: hussarPrint
            source: "qrc:/fonts/HussarPrintA.otf"
        }

        FontLoader
        {
            id: reginaldScript
            source: "qrc:/fonts/ReginaldScript.ttf"
        }

        Item
        {
            height: 65
        }

        Label
        {
            text: qsTr ("Login")
            font.family: helloStranger.font.family
            font.weight: helloStranger.font.weight
            font.styleName: helloStranger.font.styleName
            font.pixelSize: 89
            font.bold: true
            Layout.alignment: Qt.AlignHCenter
        }

        Item
        {
            height: 25
        }

        RowLayout
        {
            Layout.alignment: Qt.AlignHCenter
            spacing: 15

            Label
            {
                text: qsTr ("Benutzername")
                minimumPixelSize: 20
                Layout.preferredWidth: 150
                horizontalAlignment: Text.AlignRight
                font.family: damarWulan.font.family
                font.weight: damarWulan.font.weight
                font.styleName: damarWulan.font.styleName
                font.pixelSize: 21
            }

            TextField
            {
                id: benutzerName
                placeholderText: qsTr ("Benutzernamen eingeben")
                implicitWidth: 300
                font: hussarPrint.font
            }
        }

        RowLayout
        {
            Layout.alignment: Qt.AlignHCenter
            spacing: 15

            Label
            {
                minimumPixelSize: 20
                Layout.preferredWidth: 150
                text: qsTr ("Passwort")
                font.family: damarWulan.font.family
                font.weight: damarWulan.font.weight
                font.styleName: damarWulan.font.styleName
                font.pixelSize: 21
                horizontalAlignment: Text.AlignRight
            }

            TextField
            {
                id: passwort
                placeholderText: qsTr  ("Passwort eingeben")
                implicitWidth: 300
                font: hussarPrint.font
                echoMode: TextInput.Password
            }
        }

        RowLayout
        {
            Layout.preferredWidth: 465
            Layout.alignment: Qt.AlignHCenter

            Button
            {
                text: qsTr ("Feierabend für heute!")
                Layout.alignment: Qt.AlignRight
                font: reginaldScript.font
                onClicked:
                {
                    if (benutzerName.text.trim() && passwort.text.trim())
                        loggedin_user.login(benutzerName.text.trim(), passwort.text)
                }
            }
        }

        Item
        {
            Layout.fillHeight: true

        }

        Dialog
        {
            id: recoveryPaswordDialog
            modal: true
            title: qsTr("Wiederherstellung")
            anchors.centerIn: parent
            standardButtons: Dialog.Ok | Dialog.Cancel
            onAccepted:
            {
                recpass = recoveryPaswordInput.text
                getRecoveryDialog.open()
            }

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

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

        FileDialog
        {
            id: getRecoveryDialog
            title: qsTr("Wiederherstellungsdatei")
            fileMode: FileDialog.OpenFile
            nameFilters: ["PYQCRM Recovery files (*.pyqrec)"]
            currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
            onAccepted: config.getRecoveryKey(getRecoveryDialog.currentFile, recpass)
            onRejected: quit()
        }
    }

    Component.onCompleted:
    {
        loggedin_user.loginOkay.connect(loggedin)
        config.invalidEncryptionKey.connect(getEncryptionKey)
        config.checkEncryptionKey()
    }

    function loggedin()
    {
        appLoader.source = "Dashboard.qml"
    }

    function getEncryptionKey()
    {
        recoveryPaswordDialog.open()
    }
}
