/*!
 *
 * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved.
 * \copyright                                                       \n
 *          THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM,  \n
 *          IN PART OR IN WHOLE,                                    \n
 *          WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n
 *
 * \file    main.qml
 * \date    2019/09/30
 * \author  Behrouz NematiPour
 *
 */

// Qt
import QtQuick 2.12

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

// TODO : When Testing data on Setting screen is removed
//        This needs to be moved into the TreatmentFlows.qml
import VTreatmentBloodFlow                          0.1;
import VTreatmentDialysateFlow                      0.1;
import VTreatmentPressureOcclusion                  0.1;
import VTreatmentUltrafiltration                    0.1;
import VTreatmentTime                               0.1;
import VAlarmStatus                                 0.1;
import VPowerOff                                    0.1;

import VTreatmentAdjustmentDuration                 0.1;
import VTreatmentAdjustmentFlows                    0.1;
import VTreatmentAdjustmentUltrafiltrationState     0.1;
import VTreatmentAdjustmentUltrafiltrationEdit      0.1;
import VTreatmentAdjustmentUltrafiltrationConfirm   0.1;

import VLoadCellReadings                            0.1;
import VTemperatureSensors                          0.1;

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

/*!
 * \brief   Initialization and start point of the QML
 */
Item { id : _mainItem
    width : Variables.applicationWidth
    height: Variables.applicationHeight

    property alias powerOffDialog: _powerOffDialog

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

    // TODO : When Testing data on Setting screen is removed
    //        This needs to be moved into the Treatment______.qml

    VAlarmStatus                                { id: vAlarmStatus                                  }
    VPowerOff                                   { id: vPowerOff                                     }

    VTreatmentBloodFlow                         { id: vTreatmentBloodFlow                           }
    VTreatmentDialysateFlow                     { id: vTreatmentDialysateFlow                       }
    VTreatmentPressureOcclusion                 { id: vTreatmentPressureOcclusion                   }
    VTreatmentUltrafiltration                   { id: vTreatmentUltrafiltration                     }
    VTreatmentTime                              { id: vTreatmentTime                                }

    VLoadCellReadings                           { id: vLoadCellReadings                             }
    VTemperatureSensors                         { id: vTemperatureSensors                           }

    VTreatmentAdjustmentDuration                { id: vTreatmentAdjustmentDuration                  }
    VTreatmentAdjustmentFlows                   { id: vTreatmentAdjustmentFlows                     }
    VTreatmentAdjustmentUltrafiltrationState    { id: vTreatmentAdjustmentUltrafiltrationState      }
    VTreatmentAdjustmentUltrafiltrationEdit     { id: vTreatmentAdjustmentUltrafiltrationEdit       }
    VTreatmentAdjustmentUltrafiltrationConfirm  { id: vTreatmentAdjustmentUltrafiltrationConfirm    }

    // ----- Follow the below Z order -----
    // 1 - Screens
    // 1 - 1
    SettingsStack  { id: _settingsStack  }
    // 1 - 2
    ManagerStack   { id: _managerStack   }
    // 1 - 3
    TreatmentStack { id: _treatmentStack }

    // 3 - Footer
    MainMenu { id: _mainMenu
        titles: [ qsTr("Treatment") , qsTr("Manager") , qsTr("Settings") ]
        Component.onCompleted: {
            _treatmentStack.visible = true
        }
        onItemPressed: {
            _treatmentStack.visible = vIndex == 0;
            _managerStack  .visible = vIndex == 1;
            _settingsStack .visible = vIndex == 2;
        }
    }

    // 4 - Modals
    AutoHideInfo { id: _autoHideInfo }

    PowerOff { id: _powerOffDialog
        onAccepted: {
            _GuiView.doActionTransmit(GuiActions.PowerOff, GuiActions.Accepted)
        }
        onRejected: {
            _GuiView.doActionTransmit(GuiActions.PowerOff, GuiActions.Rejected)
        }
    }

    NotificationBar { id: _alarm
        anchors.bottom: _mainMenu.top
        level   : vAlarmStatus.alarm_Priority
        text    : vAlarmStatus.text
    }

    // 9 - Others
    Text { // TEST : Application version should be moved into the information screen later.
        color: Colors.textMain
        anchors {
            right: parent.right
            margins: 10
            bottom: parent.bottom
        }
        horizontalAlignment: Text.AlignRight
        verticalAlignment: Text.AlignBottom

        height: Variables.mainMenuHeight
        text: Qt.application.version
        font.pixelSize: 14
    }

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

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

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

            }
        }
    }
}
