/*!
 *
 * 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    MainStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      11-Jul-2024
 * \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: _postModeScreen

    property var _startupScreen: ( _GuiView.manufactSetup || _GuiView.updateSetup ) ? _settingsStack : _mainHome

    property var currentScreen: stackView.currentItem

    stackView.initialItem   : _root.initialItem

    // 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: _serviceModeScreen
        PlaceHolderText { screenName: qsTr("SERVICE  MODE") }
        onVisibleChanged: if (visible) _mainMenu.hidden = true
    }

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

        ProgressBar {
            minimum : 0
            maximum : vHDPOSTData.itemCount
            value   : _GuiView.useLogUnhandledOnly ? maximum : vHDPOSTData.itemIndex
            width   : _dialityLogo.width    + 50
            height  : 3
            progress.border.width: 0
            border.width: 0
            minText .visible: false
            maxText .visible: false
            marker  .visible: false
            anchors {
                top: _dialityLogo.bottom
                topMargin       : 15
                horizontalCenter: parent.horizontalCenter
            }
            color : _GuiView.useLogUnhandledOnly ? "red": Colors.highlightProgressBar
        }

        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   : ! _postModeScreen.visible
        }
    }

    MainHome                            { id: _mainHome
        onStartTreatment                : { page( _treatmentStack                                               )} // TODO: Remove before merging into staging. Only needed for this branch
        onCreateTreatment               : { vPreTreatmentAdjustmentInitTreatment.doInitiate(                    )}
        onVisibleChanged: {
            if (visible) {
                _mainMenu.hidden = false // initially our landing screen is _initialModeScreen which should not have the Main menu.
            }
            else {
                _mainHome.reasonText = ""
            }
        }
    }

    Connections { target: vTDOpMode
        function onIsTreatmentChanged           ( vValue ) { if( vValue ) _mainMenu.isTreatment()                }
        function onIsManagerChanged             ( vValue ) { if( vValue ) _mainMenu.isManager  ()                }
        function onIsSettingsChanged            ( vValue ) { if( vValue ) _mainMenu.isSettings ()                }

        function onFaultChanged                 ( vValue ) { page( _startupScreen                   , vValue    )}
        function onServiceChanged               ( vValue ) { page( _startupScreen                   , vValue    )}
        function onInitChanged                  ( vValue ) { page( _postModeScreen                  , vValue    )
                                                             if( vValue ) vHDPOSTData.reset() // better to reset on vinit = true because the rest makes the screen animation to run
        }

        function onHomeChanged                  ( vValue ) { page( _startupScreen                   , vValue    )}
        function onStandbyChanged               ( vValue ) { /* It depends on the subModes of the standBy. Handled by onHome.  */   }
        function onStandbyStartChanged          ( vValue ) { /* It depends on the subModes of the standBy. Handled by onHome.  */   }
        function onStandbyWaitTreatmentChanged  ( vValue ) { if ( vValue ) vDisinfectAdjustDisinfect.hasCancel  = false             }
        function onStandbyWaitDisinfectChanged  ( vValue ) { page( _disinfectStack                  , vValue    )}

        function onValidateParametersChanged    ( vValue ) {       _preTreatmentCreateStack.clear   ( vValue    ) // vValue == true if enterig the state ( clear only on entering).
                                                             page( _preTreatmentCreateStack         , vValue    )}
        function onPreTreatmentChanged          ( vValue ) { page( _preTreatmentStack               , vValue    )}
        function onInTreatmentChanged           ( vValue ) { page( _treatmentStack                  , vValue    )}
        function onPostTreatmentChanged         ( vValue ) { page( _postTreatmentStack              , vValue    )}
        function onInvalidModeChanged           ( vValue ) { page( null                             , vValue    )}
    }

    // 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: vTDTreatmentStates
        // Blood Prime
        function onBpRampChanged                ( vValue ) { page( null                             , vValue    )}

        // in-Treatmet
        function onTxDialysisChanged            ( vValue ) { page( _treatmentStack                  , vValue    )}

        function onTxRinsebackChanged           ( vValue ) { page( _endTreatmentRinsebackStack      , vValue    )}
        function onTxRecirculateChanged         ( vValue ) { page( _endTreatmentRecirculateStack    , vValue    )}
        function onTxEndChanged                 ( vValue ) { page( _endTreatmentEndStack            , vValue    )}

        // Treatment Stop
        function onTxStopChanged                ( vValue ) { page( _treatmentStack                  , vValue    )}
        function onTsRecirculateChanged         ( vValue ) { page( null                             , vValue    )}
        function onTsRecirculateDialysateChanged( vValue ) { page( null                             , vValue    )}
        function onTsRecirculateBloodChanged    ( vValue ) { page( null                             , vValue    )}
        function onTsRecirculateNoChanged       ( vValue ) { page( null                             , vValue    )}
    }

    Connections { target: vPreTreatmentAdjustmentInitTreatment
        function onAdjustmentTriggered          ( vValue ) {
            _mainHome.reasonText = vPreTreatmentAdjustmentInitTreatment.adjustment_ReasonText
        }
    }
}
