/*!
 *
 * 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)      18-Jul-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-May-2021
 *
 */

// Qt
import QtQuick 2.12

// 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

                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 alias  message             : _message.text

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

    firstFocusInput : isPassword ? _passwordCurrent : undefined

    Column {
        visible: isPassword

        anchors {
            top                 : parent.top
            horizontalCenter    : parent.horizontalCenter
            topMargin           : passwordChangeMode ? 0 : 120
        }

        PasswordEntry { id: _passwordCurrent
            label.text  : ! passwordChangeMode ? "" : qsTr("Current")
            label.width : ! passwordChangeMode ? 0 : labelWidth
            nextInput   : _passwordUpdated
            anchors {
                top                 : parent.top
                topMargin           : topMarginContent
                horizontalCenter    : parent.horizontalCenter
            }
        }

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

        PasswordEntry { id: _passwordConfirm
            visible     : passwordChangeMode
            label.text  : qsTr("Confirm")
            anchors {
                top                 : _passwordUpdated.bottom
                horizontalCenter    : parent.horizontalCenter
            }
        }
    }
    component PasswordEntry : Item {
        property alias textInput    : _passwordEntry.textInput
        property alias nextInput    : _passwordEntry.nextInput
        property alias label        : _passwordEntry.label
        property int   labelWidth   : 150

        height      : 70
        label.width : labelWidth

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

        Image {
            visible                 : _passwordEntry.visible
            anchors.left            : _passwordEntry.right
            anchors.leftMargin      : Variables.minVGap
            anchors.verticalCenter  : _passwordEntry.verticalCenter
            width                   : Variables.iconsDiameter
            height                  : Variables.iconsDiameter
            source                  : "qrc:/images/iEye"

            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.topMargin       : topMarginContent
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter  : parent.verticalCenter
        horizontalAlignment     : Text.AlignHCenter
        verticalAlignment       : Text.AlignVCenter
    }
}
