/*!
 *
 * 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
import QtQuick.Controls 2.12 // StackView

//  Qml imports
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/dialogs"
import "qrc:/globals"
import "qrc:/pages/endtreatment"
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.activeStack = stackView
        }
        else {
            _headerBar.menuHidden = true
            stackView.initialItem = null
        }
    }

    stackView.initialItem   : _treatmentHome

    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 HeaderBar.Treatment:
            pop(null)       // unwind stack back to main treatment
            break
        case HeaderBar.Trends:
            page( _treatmentTrends )
            break;
        case HeaderBar.Heparin:
            page( _treatmentHeparin )
            break
        case HeaderBar.HDF:  // TODO
            page( _treatmentHDF )
            break
        default:
            pop(null)       // unwind stack back to main treatment
            break
        }
    }

    // Components
    TreatmentBloodPrime         { id: _treatmentBloodPrime
        onIncrementRate         : function(newValue) { vTreatmentAdjustmentBloodFlowRate.doAdjustment(newValue) }
        onDecrementRate         : function(newValue) { vTreatmentAdjustmentBloodFlowRate.doAdjustment(newValue) }
        onPause                 : { vTreatmentAdjustmentBloodPrime.doPause()                                    }
        onResume                : { vTreatmentAdjustmentBloodPrime.doResume()                                   }
    }

    Connections                 { target: vTreatmentAdjustmentBloodPrime
        function onAdjustmentTriggered              ( vValue ) {
            if (vTreatmentAdjustmentBloodPrime.adjustment_Accepted) {
                _treatmentBloodPrime.notification.text = ""
            }
            else {
                _treatmentBloodPrime.notification.text = vTreatmentAdjustmentBloodPrime.text()
            }
        }
    }
    Connections                 { target: vTreatmentAdjustmentBloodFlowRate
        function onAdjustmentTriggered              ( vValue ) {
            if (vTreatmentAdjustmentBloodPrime.adjustment_Accepted) {
                _treatmentBloodPrime.notification.text = ""
            }
            else {
                _treatmentBloodPrime.notification.text = vTreatmentAdjustmentBloodFlowRate.text()
            }
        }
    }

    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 = HeaderBar.Treatment // on heparin idle timeout go to main treatment
        }
    }

    TreatmentHDF                { id: _treatmentHDF         }
    Connections                 { target: _treatmentHDF
        function onSectionPressuresClicked ( ) {
            _treatmentAdjustmentPressuresLimits.open()
        }

        function onTreatmentHDFEditClicked ( ) {
            _treatmentAdjustmentHDF.open()

        }
    }

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

    //// Treatment Adjustment Dialogs
    TreatmentAdjustmentSetPoints            { id:       _treatmentAdjustmentSetPoints       }
    TreatmentAdjustmentPressuresLimits      { id:       _treatmentAdjustmentPressuresLimits }
    TreatmentAdjustmentBolusVolume          { id:       _treatmentAdjustmentBolusVolume     }
    TreatmentAdjustmentUltrafiltrationStack { id:       _treatmentAdjustmentUltrafiltration }
    TreatmentAdjustmentHDF                  { id:       _treatmentAdjustmentHDF             }
    TreatmentAdjustmentDurationStack        { id:       _treatmentAdjustmentDuration        }
    TreatmentAdjustmentVitalsInterval       { id:       _treatmentAdjustmentVitalsInterval  }

    Connections                 { target:   _treatmentAdjustmentSetPoints
        function onConfirmClicked                   ( vValue ) {
            vTreatmentAdjustmentSetPoints.doAdjustment(
//                _treatmentAdjustmentSetPoints.treatmentModality     , // TODO
                _treatmentAdjustmentSetPoints.bloodFlowRate         ,
                _treatmentAdjustmentSetPoints.dialysateFlowRate     ,
                _treatmentAdjustmentSetPoints.dialysateTemperature  ,
//                _treatmentAdjustmentSetPoints.hepatitus               // TODO
                _treatmentAdjustmentSetPoints.acidConcentrate       ,
                _treatmentAdjustmentSetPoints.bicarbConcentrate
//                _treatmentAdjustmentSetPoints.sodium                  // TODO
//                _treatmentAdjustmentSetPoints.bicarboante             // TODO
            )
        }
    }
    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)
        }
    }

    // ---------- 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: _treatmentHome
        function onVisibleChanged                   ( vValue ) {
            if (_treatmentHome.visible) {
                _mainMenu.hidden = true
                _headerBar.menuHidden = false
            }
        }
    }

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

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

    Connections { target: _endTreatmentRecirculateStack
        function onVisibleChanged                   ( vValue ) {
            if (_endTreatmentRecirculateStack.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()        }}
        function onTxRinsebackChanged               ( vValue ) { page( _endTreatmentRinsebackStack      , vValue    )}
        function onTxRecirculateChanged             ( vValue ) { page( _endTreatmentRecirculateStack    , vValue    )}
    }
}
