/*!
 *
 * 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    AlarmItem.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      24-Jan-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  01-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  C++ imports
import VAlarmStatus     0.1
import VAlarmActiveList 0.1

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

/*!
 * \brief   AlarmItem is the alarm logic container to be separated from the rest of the code.
 *          This item needs to work with the NotificationBar component.
 */
Item { id : _root

    readonly property bool _DEBUG_DISABLE_MAXIMIZE_: false    // due to touch screen issue on some devices, the maximized alarm can't be minimized to see the screen underneath.

    anchors.fill: parent

    VAlarmStatus     { id: vAlarmStatus     }
    VAlarmActiveList { id: vAlarmActiveList }

    readonly property alias  backgroundColor: _notificationDialog.titleBarBackground
    readonly property alias  textColor      : _notificationDialog.textColor
    readonly property string title          : vAlarmStatus.title
    readonly property alias  isSilenced     : vAlarmStatus.alarm_Flag_alarmsSilenced
    readonly property alias  timeout        : vAlarmStatus.alarm_MuteTimeout
    readonly property alias  alarm_AlarmID  : vAlarmStatus.alarm_AlarmID
    readonly property alias  hasAlarm       : vAlarmStatus.hasAlarm

    signal goToPost()

    function alarmMinimize() {
        // The has alarm check has been added here to let other components call this function,
        // without worrying about the alarmDialog popoing up without an alarm present.
        // Look at the PowerItem as an example.
        if ( ! vAlarmStatus.hasAlarm ) return

        _notificationDialog.close()
        _alarmBar       .visible = true
    }
    function alarmMaximize() {
        // checks if a confirmation is open won't close it by default
        // keep it up until user closes it.
        if ( _powerItem.isOpen       ) return

        // The has alarm check has been added here to let other components call this function,
        // without worrying about the alarmDialog popoing up without an alarm present.
        // Look at the PowerItem as an example.
        if ( ! vAlarmStatus.hasAlarm ) return

        // close the user confirmation dialog if the alarm dialog is going to be dispalyed
        _confirmDialog.close()

        if ( _root._DEBUG_DISABLE_MAXIMIZE_ ) {
            alarmMinimize()
        } else {
            _notificationDialog.alarmMaximize()
            _alarmBar       .visible = false
        }
        _powerItem.cancel()
    }
    function alarmList() {
        vAlarmActiveList.doRequest()
        _notificationDialog.alarmList()
        _alarmBar       .visible = false
    }
    function alarmHide() {
        _notificationDialog.close()
        _alarmBar       .visible = false
    }

    Connections { target: _alarmBar
        function onListClicked                  (        ) { alarmList     () }
        function onMaximizeClicked              (        ) { alarmMaximize () }
        function onClicked                      (        ) { alarmMaximize () }
        function onMuteClicked                  (        ) { vAlarmStatus.doSilence() }
    }
    Connections { target: vAlarmStatus
        function onDidAlarmRaise                (        ) { alarmMaximize  () }
        function onDidAlarmEmpty                (        ) { alarmHide      () }
    }

    NotificationDialog { id : _notificationDialog
                            onGoToPost: _root.goToPost()
    }

    Connections { target: vAlarmStatus
        function onAlarm_PriorityChanged        ( vValue ) {
            [
                _notificationDialog.titleBarBackground,
                _notificationDialog.titleBarForeground,
                _notificationDialog.backgroundColor   ,
                _notificationDialog.textColor         ,
            ] = Colors.alarmPriorityColors(vValue)
        }
    }
}
