/*!
 *
 * Copyright (c) 2021-2023 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)      03-Apr-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 showPasswordReEntryField   : false
    readonly property string password        : _password.textInput.text
    readonly property string passwordReEntry : _passwordReEntry.textInput.text
    property alias  message                  : _message.text

    function clearPasswordReEntry() {
        _passwordReEntry.textInput.text = ""
    }

    function clearPassword() {
        _password.textInput.text = ""
    }

    firstFocusInput : isPassword ? _password : undefined
    TextEntry { id  : _password
        clip                : true
        visible             : _root.isPassword
        textInput   .width  : 450
        nextInput           : _passwordReEntry
        anchors {
            top             : parent.top
            topMargin       : {
                if(_root.showPasswordReEntryField) {
                    return topMarginContent + 120 - _passwordReEntry.height
                } else {
                    return topMarginContent + 120 // moved a little down to be more on center and close to keyboard top edge.
                }
            }
            horizontalCenter: parent.horizontalCenter
        }
        textInput.inputMethodHints  : Qt.ImhNone
        textInput.echoMode          : TextInput.Password
    }

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

    MouseArea {
        anchors.fill: _showPassword
        anchors.margins: -20
        onPressed   : _password.textInput.echoMode = TextInput.Normal
        onReleased  : _password.textInput.echoMode = TextInput.Password
    }

    TextEntry { id  : _passwordReEntry
        clip                : true
        visible             : _root.showPasswordReEntryField
        textInput   .width  : 450
        anchors {
            top             : _password.bottom
            topMargin       : height/2 // moved a little down to be more on center and close to keyboard top edge.
            horizontalCenter: _password.horizontalCenter
        }
        textInput.inputMethodHints  : Qt.ImhNone
        textInput.echoMode          : TextInput.Password
    }

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

    MouseArea {
        anchors.fill: _showPasswordReEntryImage
        anchors.margins: -20
        onPressed   : _passwordReEntry.textInput.echoMode = TextInput.Normal
        onReleased  : _passwordReEntry.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
    }
}
