/*!
 *
 * Copyright (c) 2019-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    ModalDialog.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      28-Sep-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  21-Oct-2019
 *
 */

// Qt
import QtQuick 2.12
import QtQuick.Controls 2.12 // Dialog
import QtGraphicalEffects 1.12

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

/*!
 * \brief   The parent item for modal dialogs
 */
Dialog { id : _root
    contentItem.objectName: "ModalDialog"  //SquishQt testability

    property bool   autoHide            : false
    property int    autoHideDuration    : 1000
    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 bool   showDropShadow      : false
    property alias backgroundItem       : _backgroundItem.children
    property alias backgroundItemZ      : _backgroundItem.z

    width   : Variables.dialogWidth
    height  : Variables.dialogHeight

    visible : false
    x: Math.round((Variables.applicationWidth - width) / 2)
    y: Math.round((Variables.applicationHeight - height) / 2)

    enter: Transition { NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: 200 } }
    exit : Transition { NumberAnimation { property: "opacity"; from: 1.0; to: 0.0; duration: 200 } }

    modal: true

    closePolicy: Dialog.NoAutoClose
    Overlay.modal : Rectangle { id : _borderRect
        anchors.fill        : parent
        anchors.bottomMargin: _alarmBar.visible ? Variables.notificationHeight : 0
        color               : "#99000000"

        Behavior on opacity { NumberAnimation { duration: 300} }
    }

    background: Rectangle { id: _backgroundRect
        color           : Colors.backgroundDialog
        radius          : Variables.dialogRadius

        layer.enabled   : showDropShadow
        layer.effect: DropShadow {
            id: _dropShadow
            horizontalOffset: 4
            verticalOffset  : 4
            radius          : 12
            samples         : 32
            color           : Colors.dropShadowDialogColor
            source          : _backgroundRect
            anchors.fill    : _backgroundRect
        }

        // 👇 Placeholder for an injected component. Ex. numpad
        Item { id: _backgroundItem }
    }

    onVisibleChanged: {
        if (autoHide && visible) {
            _autoHideAnimation.start()
        }
    }

    NumberAnimation { id: _autoHideAnimation
        running: false
        target: _root
        property: "opacity"
        duration: autoHideDuration
        //easing.type: Easing.InOutQuad
        onFinished: {
            _root.visible = false
        }
    }

    NotificationBarSmall { id: _notification ; height: 60}
}
