/*!
 *
 * Copyright (c) 2021-2025 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)      25-Feb-2025
 * \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

    AutoHideInfo { id: _autoHideInfo }

    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       :   vConfirm.isPowerOff || vConfirm.isReject

        onVisibleChanged: {
            _root.isOpen = visible
            if (visible) _alarmItem.alarmHide()
            else         _alarmItem.alarmMaximize()
        }
        onAccepted: {
            if ( vConfirm.isPowerOff ) {
                _GuiView.doActionTransmit(GuiActions.ID_PowerOff, GuiActions.Accepted)
            }
            else {
                if ( ! vConfirm.isReject ) vConfirm.doConfirm( true  )
            }
        }
        onRejected: {
            if ( vConfirm.isPowerOff ) {
                _GuiView.doActionTransmit(GuiActions.ID_PowerOff, GuiActions.Rejected)
            }
            else {
                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()
        if ( vConfirm.needConfirm ) vConfirm.doConfirm( false )
    }

    function reject() { // the power off is the only confirm has separate reject dialog, because the button is not only on UI and is HW as well.
        _autoHideInfo.showDialog(
                    qsTr("Cannot shutdown during 'Treatment'"), // '%1').arg()
                    2000 // notifies user and hides the dialog
                    )
    }

    // ---- Connections
    Connections { target: _GuiView
        function onDidActionReceive( vAction, vData ) {
            switch(vAction) {
            case GuiActions.ID_ShuttingDown:
                _autoHideInfo.showDialog (
                            qsTr("System is shutting down"),
                            5000 // if any error happens and shutdown is not successful then after 5 seconds dialog hides.
                            )
                break;
            }
        }
    }

    Connections { target: vConfirm
        function onIsRejectChanged              ( vValue ) {
            _powerOffDialog.footerUpdate()
        }

        function onVisibleChanged               ( vValue ) {
            if ( _GuiView.dryDemoMode ) return
            if ( vValue ) open  ()
            else          close ()
        }

        function onPoweroffTriggered            ( vValue ) {
            switch ( vValue ) {
            case GuiActions.Command : open  (); break;
            case GuiActions.Timeout : close (); break;
            case GuiActions.Rejected: reject(); break;
            }
        }
        function onAdjustmentTriggered          ( vValue ) {
            // DEBUG:
            // console.debug('\n' +
            //               'id                      : ' + vConfirm.id                     + '\n' +
            //               'command                 : ' + vConfirm.command                + '\n' +
            //               'title                   : ' + vConfirm.title                  + '\n' +
            //               'message                 : ' + vConfirm.message                + '\n' +
            //               'adjustment_ReasonText   : ' + vConfirm.adjustment_ReasonText
            //               )
        }
    }
}
