/*!
 *
 * Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    UserConfirmation.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      16-Jul-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-May-2021
 *
 */

// Qt
import QtQuick 2.15

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/settings"

/*!
 * \brief   Contains the message for user information or password entry for user to confirm.
 */
SettingsBase { id: _root
    objectName                  : "UserConfirmation" // SquishQt testability

    confirmEnabled              : ! passwordChangeMode ||
                                  (vSettings.isPasswordHighStrength(_root.passwordUpdated) &&
                                   _root.passwordUpdated === _root.passwordConfirm)
                property int   labelWidth           : 150

                property bool   isPassword          : false
                property bool   passwordChangeMode  : false
    readonly    property string passwordCurrent     : _passwordCurrent.textInput.text
    readonly    property string passwordUpdated     : _passwordUpdated.textInput.text
    readonly    property string passwordConfirm     : _passwordConfirm.textInput.text
                property int    passwordLengthMax   : 30
                property alias  message             : _message.text

    readonly    property string hidePassword        : "qrc:/images/iEyeClosed"
    readonly    property string showPassword        : "qrc:/images/iEye"

    function clearPasswordCurrent() { _passwordCurrent.textInput.text = "" }
    function clearPasswordUpdated() { _passwordUpdated.textInput.text = "" }
    function clearPasswordReEntry() { _passwordConfirm.textInput.text = "" }
    function clearPasswords      () {
             clearPasswordCurrent()
             clearPasswordUpdated()
             clearPasswordReEntry()
    }

    firstFocusInput : isPassword ? _passwordCurrent : undefined

    contentItem: Item {
        visible: isPassword

        Column { id: _passwordEntries
            anchors.horizontalCenter        : parent.horizontalCenter
            anchors.horizontalCenterOffset  : passwordChangeMode ? Variables.defaultMargin * -4 : 0
            width                           : parent.width / 2.5

            PasswordEntry { id: _passwordCurrent
                label.text  : passwordChangeMode ? qsTr("Current") : ""
                label.width : passwordChangeMode ? _root.labelWidth : 0
                lengthMax   : passwordLengthMax
                nextInput   : _passwordUpdated
                anchors {
                    top                 : parent.top
                    topMargin           : passwordChangeMode ? Variables.defaultMargin * 2 : Variables.defaultMargin * 5
                    horizontalCenter    : parent.horizontalCenter
                }
            }

            PasswordEntry { id: _passwordUpdated
                visible     : passwordChangeMode
                label.text  : qsTr("New")
                nextInput   : _passwordConfirm
                lengthMax   : passwordLengthMax
                anchors {
                    top                 : _passwordCurrent.bottom
                    horizontalCenter    : parent.horizontalCenter
                }
            }

            PasswordEntry { id: _passwordConfirm
                visible     : passwordChangeMode
                label.text  : qsTr("Confirm")
                lengthMax   : passwordLengthMax
                anchors {
                    top                 : _passwordUpdated.bottom
                    horizontalCenter    : parent.horizontalCenter
                }
            }
        }

        PasswordRequirements {id: _passwordRequirements
            anchors {
                top         : parent.top
                left        : _passwordEntries.right
                leftMargin  : Variables.defaultMargin * 3
            }
            visible         : passwordChangeMode
            height          : parent.height / 2
            width           : parent.width / 4
            password        : passwordUpdated
        }
    }

    component PasswordEntry : Item {
        property alias textInput    : _passwordEntry.textInput
        property alias nextInput    : _passwordEntry.nextInput
        property alias label        : _passwordEntry.label
        property alias lengthMax    : _passwordEntry.lengthMax

        height      : 70
        label.width : _root.labelWidth

        TextEntry { id: _passwordEntry
            clip                        : true
            textInput   .width          : 450
            textInput.inputMethodHints  : Qt.ImhNone
            textInput.echoMode          : TextInput.Password
            anchors.horizontalCenter    : parent.horizontalCenter

            onTextChanged: {
                // Replace non-ASCII characters as they are typed in
                var filtered = text.replace(/[^\x00-\x7F]/g, "")
                if (text !== filtered) { text = filtered }  // revert invalid characters
            }
        }

        Image {
            visible                 : _passwordEntry.visible
            anchors.left            : _passwordEntry.right
            anchors.leftMargin      : Variables.minVGap
            anchors.verticalCenter  : _passwordEntry.verticalCenter
            width                   : 35
            height                  : 35

            source                  : _mouseArea.pressed ? _root.showPassword : _root.hidePassword
            fillMode                : Image.PreserveAspectFit

            MouseArea { id: _mouseArea
                anchors.fill: parent
                anchors.margins: -20
                onPressed   : _passwordEntry.textInput.echoMode = TextInput.Normal
                onReleased  : _passwordEntry.textInput.echoMode = TextInput.Password
            }
        }
    }

    Text { id                   : _message
        visible                 : ! _root.isPassword
        text                    : "Do you ... ?"
        color                   : Colors.textMain
        font.pixelSize          : Fonts.fontPixelTitle
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top             : parent.top
        anchors.topMargin       : Variables.defaultMargin * 10
        horizontalAlignment     : Text.AlignHCenter
        verticalAlignment       : Text.AlignVCenter
    }
}
