/*!
 *
 * Copyright (c) 2020-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    TreatmentStack.qml
 * \author  (last)      Vy
 * \date    (last)      14-Jun-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

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

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

    onVisibleChanged        : { // this should never happen by design, but in tests it can easily happen and will block the screen touch.
        _treatmentAdjustmentSetPoints       .close()
        _treatmentAdjustmentVitals          .close()
        _treatmentAdjustmentPressuresLimits .close()
        _treatmentAdjustmentBolusVolume     .close()
        _treatmentAdjustmentDuration        .close()
        _treatmentAdjustmentUltrafiltration .close()

        if (! visible) {
            _headerBar.menuHidden = true
        }
    }

    stackView.initialItem   : null

    readonly property bool   isTreatmentPaused  : vTDTreatmentStates.txStop
    readonly property bool   isSBInProgress     : ( vTDTreatmentStates.sbRunning || vTDTreatmentStates.sbWaitPump )
    readonly property bool   isTreatmentEnd     : vTDTreatmentStates.txEnd
             property int   headerMenuIndex     : _headerBar.headerMenuIndex

    onHeaderMenuIndexChanged: {
        switch( _headerBar.currentScreen ) {
        case MainMenu.Treatment:
            pop(null)       // unwind stack back to main treatment
            break
        case MainMenu.Trends:
            page( _treatmentTrends )
            break;
        case MainMenu.Heparin:
            page( _treatmentHeparin )
            break
        case MainMenu.HDF:  // TODO
            page( _treatmentHDF )
            break
        default:
            pop(null)       // unwind stack back to main treatment
            break
        }
    }

    // Components
    TreatmentBloodPrime         { id: _treatmentBloodPrime          }
    TreatmentHome               { id: _treatmentHome                }

    Connections                 { target: _treatmentHome
        function onSectionFlowClicked               ( vValue ) {
            _treatmentAdjustmentSetPoints.open()
        }
        function onSectionVitalsClicked             ( vValue ) {
            _treatmentAdjustmentVitalsInterval.open()
        }
        function onSectionPressuresClicked          ( vValue ) {
            _treatmentAdjustmentPressuresLimits.open()
        }
        function onSectionSalineClicked             ( vValue ) {
            _treatmentAdjustmentBolusVolume.open()
        }
        function onSectionTimeClicked               ( vValue ) {
            _treatmentAdjustmentDuration.open()
        }
        function onSectionUltrafiltrationClicked    ( vValue ) {
            _treatmentAdjustmentUltrafiltration.open()
        }
    }

    ScreenItem                  { id: _treatmentTrends      } // TODO: make me!
    TreatmentHeparin            { id: _treatmentHeparin     }
    Connections { target: _treatmentHeparin
        function onIdleTimeout  ( ) {
            _headerBar.headerMenuIndex = MainMenu.Treatment // on heparin idle timeout go to main treatment
        }
    }

    ScreenItem                  { id: _treatmentHDF         } // TODO: make me!

    //// Treatment Adjustment Dialogs
    TreatmentAdjustmentSetPoints        { id:       _treatmentAdjustmentSetPoints       }
    TreatmentAdjustmentPressuresLimits  { id:       _treatmentAdjustmentPressuresLimits }
    TreatmentAdjustmentBolusVolume      { id:       _treatmentAdjustmentBolusVolume     }
    TreatmentAdjustmentUltrafiltration  { id:       _treatmentAdjustmentUltrafiltration }

    Connections                 { target:   _treatmentAdjustmentSetPoints
        function onConfirmClicked                   ( vValue ) {
            vTreatmentAdjustmentSetPoints.doAdjustment(
                _treatmentAdjustmentSetPoints.bloodFlowRate         ,
                _treatmentAdjustmentSetPoints.dialysateFlowRate     ,
                _treatmentAdjustmentSetPoints.dialysateTemperature  ,
                _treatmentAdjustmentSetPoints.acidConcentrate       ,
                _treatmentAdjustmentSetPoints.bicarbConcentrate
            )
        }
    }
    Connections                 { target:   _treatmentAdjustmentPressuresLimits
        function onConfirmClicked                   ( vValue ) {
            vTreatmentAdjustmentPressuresLimits.doAdjustment(
                _treatmentAdjustmentPressuresLimits.arterialPressureLimitWindow ,
                _treatmentAdjustmentPressuresLimits.venousPressureLimitWindow   ,
                _treatmentAdjustmentPressuresLimits.venousPressureLimitAsymtrc  ,
                _treatmentAdjustmentPressuresLimits.tmpLimitWindow
            )
        }
    }
    Connections                 { target:   _treatmentAdjustmentBolusVolume
        function onConfirmClicked                   ( vValue ) {
            vTreatmentAdjustmentBolusVolume.doAdjustment(_treatmentAdjustmentBolusVolume.fluidBolusVolume)
        }
    }

    TreatmentAdjustmentDuration { id: _treatmentAdjustmentDuration }

    TreatmentAdjustmentVitalsInterval { id: _treatmentAdjustmentVitalsInterval  }

    // ---------- Manages Responses ----------
    Connections { target: vTreatmentAdjustmentBolusVolume
        function onAdjustmentTriggered              ( vValue ) {
            if ( vTreatmentAdjustmentBolusVolume.adjustment_Accepted ) {
                _treatmentAdjustmentBolusVolume.close()
            }
            else {
                _treatmentAdjustmentBolusVolume.notification.text = vTreatmentAdjustmentBolusVolume.text()
            }
        }
    }

    Connections { target: vTreatmentAdjustmentPressuresLimits
        function onAdjustmentTriggered              ( vValue ) {
            if (vTreatmentAdjustmentPressuresLimits.adjustment_Accepted) {
                _treatmentAdjustmentPressuresLimits.close()
            }
            else {
                _treatmentAdjustmentPressuresLimits.notification.text = vTreatmentAdjustmentPressuresLimits.text()
            }
        }
    }

    Connections { target: vTreatmentAdjustmentSetPoints
        function onAdjustmentTriggered              ( vValue ) {
            if (vTreatmentAdjustmentSetPoints.adjustment_Accepted) {
                _treatmentAdjustmentSetPoints.close()
            }
            else {
                _treatmentAdjustmentSetPoints.notification.text = vTreatmentAdjustmentSetPoints.text()
            }
        }
    }

    Connections { target: vTDOpMode
        // The initail screen should be the Blood Prime, since that one is the earlier state in the list.
        // also since it is being used in two stacks In-Treatment and also in End-Treatmet is has been defined in the MainStack.
        function onInTreatmentChanged               ( vValue ) { page( _treatmentBloodPrime  , vValue   )}
    }

    Connections { target: _treatmentBloodPrime
        function onVisibleChanged                   ( vValue ) {
            if (_treatmentBloodPrime.visible) {
                _headerBar.menuHidden = true
            }
        }
    }

    Connections { target: vTDTreatmentStates
        // in-Treatmet
        function onTxBloodPrimeChanged              ( vValue ) { page( _treatmentBloodPrime  , vValue           )}
        function onTxDialysisChanged                ( vValue ) { page( _treatmentHome        , vValue           )}
        function onTxEndChanged                     ( vValue ) { if ( vValue ) {  _endTreatmentDialog.open()    }}
    }

    Connections { target: _mainHome
        function onStartTreatment                   ( vValue ) { page( _treatmentHome                           )}
    }
}
