/*!
 *
 * 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/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
        }
    }

    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
        StackView.onActivating  : { _headerBar.titleText = qsTr("Blood Priming")  }
        StackView.onDeactivated : { _headerBar.titleText = ""                     }
        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()

        }
    }

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

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

    TreatmentAdjustmentDurationStack { 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) {
                // TODO update setpoints here?
                vTreatmentCreate.treatmentModality                  =  _treatmentAdjustmentSetPoints.treatmentModality
                vTreatmentCreate.acidConcentrate                    = _treatmentAdjustmentSetPoints.acidConcentrate
                vTreatmentCreate.acidConcentrateConversionFactor    = vTreatmentRanges.acidConcentrateModel.get(vTreatmentCreate.acidConcentrate).value
                vTreatmentCreate.dryBicarbCartSize                  = _treatmentAdjustmentSetPoints.bicarbConcentrate
                vTreatmentCreate.hepatitusBStatus                   = _treatmentAdjustmentSetPoints.hepatitus
                vTreatmentCreate.sodium                             = _treatmentAdjustmentSetPoints.sodium
                vTreatmentCreate.bicarbonate                        = _treatmentAdjustmentSetPoints.bicarbonate
                _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()    }}
    }
}
