import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import QtCore
import "../js/qmldict.js" as JsLib

Item
{
    id: utilityDialogs
    property alias backup_config: backupConfig
    property alias backup_encrypt_pw: recoveryPasswordDialog

    property var recpass
    property var configpass
    Dialog
    {

        anchors.centerIn: parent
        id: backupConfig
        title: "Backup Config"
        standardButtons: Dialog.Apply | Dialog.Cancel

        onApplied:
        {

            if (configPwd.text === repeatConfigPwd.text)
            {
                configpass = repeatConfigPwd.text
                saveConfigFile.open()
            }
            else
            {
                configPwd.text = ""
                configPwd.placeholderText = qsTr("Passwort stimmt nicht überein")
                configPwd.placeholderTextColor = "red"
                repeatConfigPwd.placeholderText = qsTr("")
                repeatConfigPwd.text = ""
            }
        }
            onRejected: resetRecoveryConfigDialog()
        GridLayout
        {
            id: gridPw
            columns: 2
            Label
            {
                text: qsTr("Passwort eingeben")
            }
            TextField
            {

                id: configPwd
                placeholderText: qsTr("Sicherungspasswort festlegen")
                echoMode: TextInput.Password
                implicitWidth: 300
            }
            Label
            {
                text: qsTr("Passwort wiederholen")
            }
            TextField
            {
                property string name: "password"
                id: repeatConfigPwd
                placeholderText: qsTr("Sicherungspasswort wiederholen")
                echoMode: TextInput.Password
                implicitWidth: 300
            }

        }

    }

    FileDialog
    {
            id: saveConfigFile
            fileMode: FileDialog.SaveFile
            nameFilters: ["PYQCRM Recovery files (*.pyqrec)"]
            currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
            onAccepted:
            {
                config.backupConfig(saveConfigFile.currentFile, configpass)
            }
            onRejected:
            {
                backupConfig.close()
                resetRecoveryConfigDialog()
            }
    }

    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: recoveryPasswordDialog.open()
        onRejected: contentStack.replace("LoginSreen.qml")
    }

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

    Dialog
    {

        id: recoveryPasswordDialog
        modal: true
        title: qsTr("Wiederherstellung")
        anchors.centerIn: parent
        standardButtons: Dialog.Apply | Dialog.Cancel
        onApplied:
        {
            if (recoveryPasswordInput.text === repeatRecoveryPasswordInput.text)
            {
                recpass = recoveryPasswordInput.text
                saveRecoveryDialog.open()
            }
            else
            {
                recoveryPasswordInput.text = ""
                recoveryPasswordInput.placeholderText = qsTr("Passwort stimmt nicht überein")
                recoveryPasswordInput.placeholderTextColor = "red"
                repeatRecoveryPasswordInput.placeholderText = qsTr("")
                repeatRecoveryPasswordInput.text = ""
            }
        }

        onClosed: resetRecoveryPwDialog()

        ColumnLayout
        {
            GridLayout
            {
                columns: 2
                Label
                {
                    text: qsTr("Wiederherstellungspasswort festlegen: ")
                }

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

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

    FileDialog
    {
        id: saveRecoveryDialog
        title: qsTr("Wiederherstellungsdatei")
        fileMode: FileDialog.SaveFile
        nameFilters: ["PYQCRM Recovery files (*.pyqrec)"]
        currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
        onAccepted:
        {
            config.backupEncryptkey(saveRecoveryDialog.currentFile, recpass)
        }

        onRejected:
        {
            recoveryPasswordDialog.close()
            resetRecoveryPwDialog()
        }
    }

    function resetRecoveryPwDialog()
    {
        recoveryPasswordInput.text = ""
        recoveryPasswordInput.placeholderText = qsTr("Hier Wiederherstellungspasswort eingeben")
        recoveryPasswordInput.placeholderTextColor = repeatRecoveryPasswordInput.placeholderTextColor
        repeatRecoveryPasswordInput.text = ""
        repeatRecoveryPasswordInput.placeholderText = qsTr("Hier Wiederherstellungspasswort wiederholen")
    }

    function resetRecoveryConfigDialog()
    {
        configPwd.text = ""
        configPwd.placeholderText = qsTr("Hier Wiederherstellungspasswort eingeben")
        configPwd.placeholderTextColor = repeatConfigPwd.placeholderTextColor
        repeatConfigPwd.text = ""
        repeatConfigPwd.placeholderText = qsTr("Hier Wiederherstellungspasswort wiederholen")
    }
}
