/*!
 *
 * Copyright (c) 2020-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    NotificationDialog.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      17-Apr-2023
 * \author  (original)  Peter Lucia
 * \date    (original)  02-Jun-2020
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   Contains the Alarm Dialog Implementation
 */
ModalDialog { id : _root
    contentItem.objectName: "_NotificationDialog" // SquishQt

    property alias titleText            : _title.text
    property alias titlePixelSize       : _title.font.pixelSize
    property alias description          : _desc.text
    property alias descriptionPixelSize : _desc.font.pixelSize
    property alias titleBarForeground   : _title.color
    property alias titleBarBackground   : _titleBar.color
    property alias isSilenced           : _muteButton.isSilenced
    property alias timeout              : _muteButton.timeout
    property int   alarmID              : -1

    property alias countDown            : _timeoutText.countDown
    property alias countDownVisible     : _timeoutText.visible


    property bool  resumeVisible        : false
    property bool  rinsebackVisible     : false
    property bool  endVisible           : false
    property bool  okVisible            : false
    property bool  minVisible           : true

    property bool  titleFading : false
    Fader {
        fadingProperyTarget : _root
        fadingProperyRunning:  titleFading
        fadingProperyName   : "titleBarBackground"
        fadingProperyValue  :  titleBarBackground
    }

    // Look for this tag: #First_Time_Message_Sent_With_Silenced
    // the isSilenced is helping not to show this dialog maximized and
    // then if the silence is set minimize it immediately
    // this behaviour does not look nice on the screen and it bounces
    visible : description && ! isSilenced
    radius  : Variables.dialogRadius

    signal muteClicked()
    signal minimizeClicked()

    signal resumeClicked()
    signal rinsebackClicked()
    signal endClicked()
    signal okClicked()

    Rectangle { id: _titleBar
        height  : Variables.mainMenuHeight
        width   : _root.width
        radius  : _root.radius
        clip    : true // the mute button expands so we need to clip the outside unwanted area.

        // Image { id: _icon
        //     source: "qrc:/images/iAlarm"
        //     anchors.horizontalCenter: _title.horizontalCenter
        //     anchors.verticalCenter: _title.verticalCenter
        //     anchors.horizontalCenterOffset: -_title.width + Variables.dialogIconHorizontalOffset;
        //     height  : Variables.alarmListIconDiameter
        //     width   : Variables.alarmListIconDiameter
        // }

        Text { id: _title
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            text: qsTr("Notification")

            width: _titleBar.width
            verticalAlignment   : Text.AlignVCenter
            horizontalAlignment : Text.AlignHCenter
            anchors.centerIn: _titleBar
        }

        MouseArea { id: _minimizeArea
            // this object is not exposed so cannot be missused by the child component, so here it can be enabled and being handled by the minVisible property
            // enabled         : _root.minVisible
            anchors.fill    : parent
            onClicked       : { if (   _root.minVisible ) _root.minimizeClicked()          } // if can     be minimized (minVisible = true ), call the minimizeClinked signal (norma behavior)
            onDoubleClicked : { if ( ! _root.minVisible ) _sdcProgressItem.doubleClicked() } // if can NOT be minimized (minVisible = false), call the sdcard.double click to pop the DiagnosticsDialog
            // TODO: disable doubleClicked later. this is only for diagnostic purpose when alarm dialog covers the entire screen.
        }

        UpDownButton { id: _minimizeButton
            backgroundColor: _titleBar.color
            isUp    : false
            isList  : ! _root.minVisible
            anchors {
                verticalCenter  : _titleBar.verticalCenter
                right           : _titleBar.right
                rightMargin     : Variables.silenceIconMargin
            }
            onClicked: _root.minimizeClicked()
        }

        MuteButton { id: _muteButton
            backgroundColor: _titleBar.color
            anchors {
                verticalCenter  : _titleBar.verticalCenter
                left            : _titleBar.left
                leftMargin      : Variables.silenceIconMargin
            }
            onClicked: _root.muteClicked()
        }

        TimeText { id: _timeoutText
            property int countDown : 0
            seconds         : countDown * 60

            height: _titleBar.height
            anchors {
                verticalCenter  : _titleBar.verticalCenter
                right           : _minimizeButton.left
                rightMargin     : 15
            }
            textPixelSize   : 30
            textWeight      : Font.Normal
            secondsVisible  : false
            hourZero        : false
            minuteZero      : true
        }
    }

    Rectangle { id: _descReect
        color: Colors.transparent
        clip    : true
        anchors {
            fill            : parent
            topMargin       : _titleBar     .height + 2
            bottomMargin    : _buttonGroup  .height
        }

        Text { id: _desc
            objectName: "_NotificationDialog_Description"
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelButton
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter  : parent.verticalCenter
            }
        }
    }

    Row { id: _buttonGroup
        property int buttonsWidth   : 300
        spacing: 50

        anchors {
            bottom          : parent.bottom
            horizontalCenter: parent.horizontalCenter
            rightMargin     : spacing
            leftMargin      : spacing
            bottomMargin    : spacing / 2 // give the content more space.
        }

        // IMPORTANT NOTE : Alarm Dialog buttons
        // It has been granteed by FW that there should never be more than 3 buttons on the ScreenItem
        // Regarding the discussion there should be only 3 situations:
        // 1 - Different combination of the Resume,Rinseback,End
        // 2 - OK button for user to only ack the alarm
        // 3 - No button at all for the situations that the alarm cannot be resolved by user.
        // :: OK button and the other 3 buttons (Resume,Rinseback,End) should never come together
        TouchRect { id  : _resumeTouchRect
            visible     : resumeVisible
            onPressed   : resumeClicked()
            text.text   : qsTr("RESUME")
            width       : _buttonGroup.buttonsWidth
            borderColor : Colors.white
            selectColor : Qt.darker(_root.backgroundColor, 1.15)
        }

        TouchRect { id  : _rinsebackTouchRect
            visible     : rinsebackVisible
            onPressed   : rinsebackClicked()
            text.text   : qsTr("RINSEBACK")
            width       : _buttonGroup.buttonsWidth
            borderColor : Colors.white
            selectColor : Qt.darker(_root.backgroundColor, 1.15)
        }

        TouchRect { id  : _endTouchRect
            visible     : endVisible
            onPressed   : endClicked()
            text.text   : qsTr("END TREATMENT")
            width       : _buttonGroup.buttonsWidth
            borderColor : Colors.white
            selectColor : Qt.darker(_root.backgroundColor, 1.15)
        }
        TouchRect { id  : _okTouchRect
            visible     : okVisible
            onPressed   : okClicked()
            text.text   : qsTr("OK")
            width       : _buttonGroup.buttonsWidth
            borderColor : Colors.white
            selectColor : Qt.darker(_root.backgroundColor, 1.15)
        }
    }

    Text { id: _alarmID
        text            : qsTr("ID") + ":" + _root.alarmID
        anchors {
            left        : parent.left
            bottom      : parent.bottom
            leftMargin  : 5
            bottomMargin: 5
        }
        color           : Colors.textMain
        font.pixelSize  : Fonts.fontPixelDialogText
    }
}
