/*!
 *
 * 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    EntryDialog.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      28-Feb-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Jun-2021
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   EntryDialog is a dialog like scree
 *          This screen has been designed to be able to accept user inputs like TextEntry
 *          which requires Keyboard since the dialogs will cover the keyboard
 */
Rectangle { id: _root
    objectName: "EntryDialog"  //SquishQt testability

    default property alias content  : _backgroundRect.data

    signal autoHidden    ()
    signal   closeClicked()
    signal confirmClicked()
    signal   opened      ()
    signal   closed      ( bool vQuit )


    property bool   closeVisible        : true
    property bool   confirmVisible      : true
    property bool   confirmEnabled      : true

    property bool   autoHide            : false
    property int    autoHideDuration    : 1000
    property bool   autoHideCancel      : false
    property alias  contentRect         : _backgroundRect
    property alias  backgroundColor     : _backgroundRect.color
    property alias  textColor           : _notification.textColor
    property alias  border              : _backgroundRect.border
    property alias  radius              : _backgroundRect.radius
    property alias  notificationText    : _notification.text
    property alias  notification        : _notification
    property alias  titleText           : _titleText.text

    function open   (       ) { opacity = 1
             opened (       )              }
    function close  ( vQuit ) { opacity = 0
             closed ( vQuit )              }

    width   : Variables.applicationWidth
    height  : Variables.applicationHeight
    opacity : 0
    visible : opacity
    anchors.centerIn: parent

    color   : Colors.borderDialog
    Behavior on opacity { PropertyAnimation{}}
    onVisibleChanged: {
        if (autoHide && visible) {
            _autoHideAnimation.start()
        }
        if (! visible) if (_keyboard) _keyboard.setVisible(false)
        notificationText = ""
    }

    onAutoHideCancelChanged: {
        if ( _root.autoHideCancel ) {
            _autoHideAnimation.stop()
            _root.autoHideCancel = false
        }
    }

    NumberAnimation { id: _autoHideAnimation
        running : false
        target  : _root
        property: "opacity"
        duration: autoHideDuration
        onFinished: {
            _root.close()
            autoHidden()
        }
    }

    MouseArea { id: _modal
        anchors.fill: parent
    }

    Rectangle { id: _backgroundRect
        anchors.centerIn: parent
        width   : Variables.dialogWidth
        height  : Variables.dialogHeight
        color   : Colors.backgroundDialog
        radius  : Variables.dialogRadius

        MouseArea { id: autoHideCancel
            anchors.fill: parent
            onClicked: {
                _autoHideAnimation.stop()
                mouse.accepted = false
            }
        }

        Rectangle { id : _headerRect
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right

            ConfirmButton { id : _confirmButton
                visible: _root.confirmVisible
                enabled: _root.confirmEnabled
                onClicked : {
                    confirmClicked()
                    _keyboard.setVisible(false)
                }
            }

            TitleText { id : _titleText
                font.pixelSize: Fonts.fontPixelButton
                color: Colors.textMain
                height: Variables.mainMenuHeight
                anchors {
                    top: parent.top
                    horizontalCenter: parent.horizontalCenter
                    margins: 35
                }
            }

            CloseButton { id : _closeButton
                visible: _root.closeVisible
                anchors.left: parent.left
                onClicked : {
                    closeClicked()
                    _keyboard.setVisible(false)
                    close()
                }
            }
        }

        NotificationBarSmall { id: _notification }
    }
}
