/*!
 *
 * Copyright (c) 2019-2020 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)      28-Feb-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  28-Feb-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  C++ imports
import Gui.View     0.1
import Gui.Actions  0.1
import VPowerOff    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

    VPowerOff    { id: vPowerOff     }

    AutoHideInfo { id: _autoHideInfo }

    Connections { target: _GuiView
        onDidActionReceive: {
            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;
            }
        }
    }

    PowerOff { id: _powerOffDialog
        onVisibleChanged: {
            if (visible) _alarmItem.alarmHide()
            else         _alarmItem.alarmMaximize()
        }
        onAccepted: {
            _GuiView.doActionTransmit(GuiActions.ID_PowerOff, GuiActions.Accepted)
        }
        onRejected: {
            _GuiView.doActionTransmit(GuiActions.ID_PowerOff, GuiActions.Rejected)
        }
    }

    Connections { target: vPowerOff
        onPoweroffTriggered: {
            switch (vpoweroff) {
            case GuiActions.Command:
                _powerOffDialog.open()
                _alarmItem.alarmHide()
                break;

            case GuiActions.Timeout:
                _powerOffDialog.close()
                _alarmItem.alarmMaximize()
                break;

            case GuiActions.Rejected:
                _autoHideInfo.showDialog(
                            qsTr("Cannot shutdown during 'Treatment'"), // '%1').arg()
                            2000 // notifies user and hides the dialog
                            )
                break;
            }
        }
    }

}
