/*!
 *
 * Copyright (c) 2021-2022 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    MainStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      28-Jan-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  01-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/disinfect"
import "qrc:/pages/pretreatment"
import "qrc:/pages/pretreatment/create"
import "qrc:/pages/treatment"
import "qrc:/pages/endtreatment"
import "qrc:/pages/posttreatment"

/*!
 * \brief   ManagerStack is the screen
 * which contains the main stack for all the Trearment Manager screens
 * This is the screen which will show up when "Manager" option
 * is selected from the main menu.
 */
StackItem { id : _root

    // DEBUG: this property can mostly be used for debugging to get pass the initial screen
    property var initialItem: _initialModeScreen // _mainHome

    stackView.initialItem   : _root.initialItem

    function doSettingsDisinfect() {
        _mainMenu.currentIndex = 0
        page( _disinfectStack )
        _disinfectStack.doSettingsDisinfect()
    }

    // Standby / Disinfection
    DisinfectStack                  { id: _disinfectStack                   }
    // Pre Treatment
    PreTreatmentCreateStack         { id: _preTreatmentCreateStack          }
    PreTreatmentStack               { id: _preTreatmentStack                }
    // In TreatmentStack
    TreatmentStack                  { id: _treatmentStack                   }

    // End Treatment
    EndTreatmentRinsebackStack      { id: _endTreatmentRinsebackStack       }
    EndTreatmentRecirculateStack    { id: _endTreatmentRecirculateStack     }
    EndTreatmentEndStack            { id: _endTreatmentEndStack             }

    // Post Treatment
    PostTreatmentStack              { id: _postTreatmentStack               }

    ScreenItem { id: _faultModeScreen
        // we recently decided to not change the screen on fault and stay on current(whatever are we on currently) screen.
        PlaceHolderText { screenName: qsTr("FAULT  MODE") }
        onVisibleChanged: if (visible) _mainMenu.hidden = true
    }

    ScreenItem { id: _serviceModeScreen
        PlaceHolderText { screenName: qsTr("SERVICE  MODE") }
        onVisibleChanged: if (visible) _mainMenu.hidden = true
    }

    ScreenItem { id: _initialModeScreen
        onVisibleChanged:
            if (visible) {
                _mainMenu.hidden = true
            }
        Image { id: _dialityLogo
            Behavior on opacity { OpacityAnimator { duration: 1000 } }
            opacity         : _initialModeScreen.visible ? 1 : 0
            anchors.centerIn: parent
            source          : "qrc:/images/iLogoDiality"
        }

        ProgressBar {
            minimum : 0
            maximum : vHDPOSTData.itemCount
            value   : vHDPOSTData.itemIndex
            width   : _dialityLogo.width    + 50
            height  : 3
            minText .visible: false
            maxText .visible: false
            marker  .visible: false
            anchors {
                top: _dialityLogo.bottom
                topMargin       : 15
                horizontalCenter: parent.horizontalCenter
            }
        }

        WaitDone { id: _hdWaitdone
            diameter: 100
            anchors {
                top: _dialityLogo.bottom
                topMargin: 100
                horizontalCenter: parent.horizontalCenter
            }
            visible : true
            // HD will pause for a few seconds before changing to stand-by mode
            // So we can show that POST has completed
            fail    : ! vHDPOSTData.doneResult
            done    :   vHDPOSTData.done
            pause   : ! _initialModeScreen.visible
        }
    }

    MainHome                            { id: _mainHome
        onStartTreatment                : { page( _treatmentStack                                               )}
        onCreateTreatment               : { vPreTreatmentAdjustmentInitTreatment.doInitiate()                    }
        onVisibleChanged: {
            if (visible) {
                _mainMenu.hidden = false // initially our landing screen is _initialModeScreen which should not have the Main menu.
            }
        }
    }

    Connections { target: vHDOperationMode
        // onFaultChanged               : { page( _faultModeScreen                  , vfault                    )} // may needed later.
        onServiceChanged                : { page( _serviceModeScreen                , vservice                  )}
        onInitChanged                   : { page( _root.initialItem                 , vinit                     )
                                            if  ( vinit ) vHDPOSTData.reset() // better to reset on vinit = true because the rest makes the screen animation to run
        }

        onHomeChanged                   : { page( _mainHome                         , vhome                     )}
        onStandbyChanged                : { /* It depends on the subModes of the standBy. Handled by onHome.  */ }
        onStandbyStartChanged           : { /* It depends on the subModes of the standBy. Handled by onHome.  */ }
        onStandbyWaitTreatmentChanged   : { /* It depends on the subModes of the standBy. Handled by onHome.  */ }
        onStandbyWaitDisinfectChanged   : { page( _disinfectStack                   , vstandbyWaitDisinfect     )}

        onValidateParametersChanged     : { page( _preTreatmentCreateStack          , vvalidateParameters       )}
        onPreTreatmentChanged           : { page( _preTreatmentStack                , vpreTreatment             )}
        onInTreatmentChanged            : { page( _treatmentStack                   , vinTreatment              )}
        onPostTreatmentChanged          : { page( _postTreatmentStack               , vpostTreatment            )}
        onInvalidModeChanged            : { page( null                              , vinvalidMode              )}
    }

    // the page function is more flixible regarding our current design
    // and it's easier(or may not need) to modify later if required.
    // and is more optimized and will never leave screen empty
    // also with what ever order they have been push with the reverse/correct animation will be poped or pushed.
    Connections { target: vHDTreatmentStates
        // Blood Prime
        onBpRampChanged             : { page( null                              ,  vbpRamp                  )}

        // in-Treatmet
        onTxDialysisChanged         : { page( _treatmentStack                   ,  vtxDialysis              )}

        onTxRinsebackChanged        : { page( _endTreatmentRinsebackStack       ,  vtxRinseback             )}
        onTxRecirculateChanged      : { page( _endTreatmentRecirculateStack     ,  vtxRecirculate           )}
        onTxEndChanged              : { page( _endTreatmentEndStack             ,  vtxEnd                   )}

        // Treatment Stop
        onTxStopChanged             : { page( _treatmentStack                   ,  vtxStop                  )}
        onTsRecirculateChanged      : { page( null                              ,  vtsRecirculate           )}
        onTsRecirculateNoChanged    : { page( null                              ,  vtsRecirculateNo         )}
    }

    Connections { target: vPreTreatmentAdjustmentInitTreatment
        onAdjustmentTriggered: {
            // this may be handled by alarm and in that case if it is shown, it would be duplicate or simply will be covered by alarm.
            // if not it is not handled by alarm, removing the comment will make it work properly, since the MainHome modified to support this.
            // _mainHome.reasonText = vPreTreatmentAdjustmentInitTreatment.text()
        }
    }
}
