/*!
 *
 * 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    PowerItem.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      30-Aug-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  01-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  C++ imports
import Gui.View     0.1
import Gui.Actions  0.1

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

/*!
 * \brief   PowerItem is the power off logic container to be separated from the rest of the code.
 * \todo    This item needs to be modified later to be independent of the _GuiView
 */
Item { id: _root
    anchors.fill: parent

    property bool isOpen: false


    ConfirmDialog { id: _powerOffDialog
        idText          :   vConfirm.id
        titleText       :   vConfirm.title
        messageText     :   vConfirm.message
        confirmText     :   vConfirm.confirm
        cancelText      :   vConfirm.cancel
//        confirmVisible  : ! vConfirm.isReject
        notificationText:   vConfirm.adjustment_ReasonText
        autoClose       :   false

        onVisibleChanged: {
            _root.isOpen = visible
            if (visible) _alarmItem.alarmHide()
            else         _alarmItem.alarmMaximize()
        }

        onAccepted: {
            if ( ! vConfirm.isReject ) {
                vConfirm.doConfirm( true  )
            }
        }

        onRejected: if ( ! vConfirm.isReject ) vConfirm.doConfirm( false )
    }

    function open() {
        _root.isOpen = true
        _powerOffDialog.open()
        _confirmDialog.close()      // close the user confirmation dialog if the power off is requested
        _alarmItem.alarmMinimize()
    }

    function close() {
        _root.isOpen = false
        _powerOffDialog.close()
        _alarmItem.alarmMaximize()
    }

    function cancel() {
        _root.isOpen = false
        _powerOffDialog.close()
        vConfirm.doConfirm( false )
    }

    Connections { target: vConfirm
        function onIsRejectTriggered    ( vValue ) {
            if ( vValue && ! _powerOffDialog.visible ) {
                _autoHideInfo.showDialog(
                    vConfirm.adjustment_ReasonText,
                    2000 // notifies user and hides the dialog
                    )
            }
        }

        function onVisibleTriggered     ( vValue ) {
            print(vValue)
            print("vConfirm.isReject " + vConfirm.isReject)
            if ( vConfirm.isReject )            return

            if ( vValue )                       open  ()
            else                                close ()
        }

        function onPowerOffTriggered    ( vValue ) {
            _autoHideInfo.showDialog (
                qsTr("System is shutting down"),
                5000 // if any error happens and shutdown is not successful then after 5 seconds dialog hides.
                )
        }
    }
}
