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

import "../js/qmldict.js" as Qmldict



Item
{
    property string recpass: ""
    property bool adminAvailable: true


    id: firstStartItem
    anchors.fill: parent
    StackView
    {
        id: firstStart
        anchors.fill: parent
        initialItem: "DbConfiguration.qml"
    }

    RowLayout
    {
        anchors.bottom: parent.bottom
        anchors.margins: 9
        width: parent.width

        Item
        {
            Layout.fillWidth: true
        }

        Button
        {
            id: cancelBtn
            text: qsTr("Abbrechen")
            onClicked:
            {
                Qt.quit()
            }
        }

        Button
        {
            id: submitBtn
            text: qsTr("Speichern")
            property var grids: firstStart.currentItem
            property var pyqcrm_conf: ({})
            property var admin: Boolean
            onClicked:
            {
                if (firstStart.currentItem.name === "database")
                {
                    pyqcrm_conf = Qmldict.firstConf(submitBtn.grids)
                    if (pyqcrm_conf)
                    {
                        config.setConfig(pyqcrm_conf)
                    }
                }
                else
                {
                    pyqcrm_conf = Qmldict.firstConf(submitBtn.grids)
                    if (pyqcrm_conf) config.addAdminUser(pyqcrm_conf)
                }
            }
        }
    }

    MessageDialog
    {
        id: recoveryDialog
        text: qsTr("Diesen Wiederherstellungscode musst du sicher aufbewahren!\nMöchtest du das jetzt machen?")
        title: qsTr("Wiederherstellen")
        buttons: MessageDialog.Yes | MessageDialog.No
        onAccepted: recoveryPaswordDialog.open()
        onRejected: contentStack.replace("LoginSreen.qml")
    }

    MessageDialog
    {
        id: conErrDialog
        text: qsTr("Datenbankverbindung fehlgeschlagen")
        title: qsTr("Datenbank Verbindung")
    }

    Dialog
    {

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

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

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

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

    FileDialog
    {
        id: saveRecoveryDialog
        title: qsTr("Wiederherstellungsdatei")
        fileMode: adminAvailable? FileDialog.OpenFile: FileDialog.SaveFile
        nameFilters: ["PYQCRM Recovery files (*.pyqrec)"]
        currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
        onAccepted:
        {
            if (!adminAvailable) config.saveRecoveryKey(saveRecoveryDialog.currentFile, recpass)
            else config.getRecoveryKey(saveRecoveryDialog.currentFile, recpass)

            contentStack.replace("LoginSreen.qml")
        }

        onRejected:
        {
            if (adminAvailable) quit()
        }
    }

    Loader
    {
        id: utilityDialogs
        source: "UtilityDialogs.qml"
    }

    Component.onCompleted:
    {
        config.dbConnectionError.connect(onDbConnectionError)
        config.adminUserError.connect(onAdminUserError)
        config.backupEncryptionKey.connect(onBackupEncryptionKey)
    }

    function onBackupEncryptionKey()
    {

        utilityDialogs.item.recoveryDialog.open()
    }

    function onDbConnectionError(msg, success)
    {
        if (!success)
            conErrDialog.open()
    }

    function onAdminUserError(msg, success)
    {
        if (success)
        {
            recoveryPaswordDialog.open()
        }
        else
        {
            adminAvailable = false
            firstStart.push("AdminUserConfig.qml")
        }
    }
}
