/*!
 * 
 * Copyright (c) 2019-2020 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)      Behrouz NematiPour
 * \date    (last)      28-Feb-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 * 
 */

// Qt
import QtQuick 2.12

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
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"

    stackView.initialItem   : null

    // ultrafiltration state information bar properties
    readonly property bool   isUFPaused         : vHDTreatmentStates.ufPaused
    readonly property bool   isUFRunning        : vHDTreatmentStates.ufRunning
    readonly property string ufInfoImageSource  : isUFPaused    ? "qrc:/images/iPauseGray" : ""
    readonly property string ufInfoText         : isUFPaused    ? qsTr("Ultrafiltration Paused"    ) : ""
    readonly property string ufInfoTextColor    : isUFPaused    ? "gray"  : ""

    readonly property bool   isSBInProgress     : ( vHDTreatmentStates.sbRunning || vHDTreatmentStates.sbWaitPump )

    signal backClicked

    // Components
    MainMenu            { id: _treatmentMenu
        y               : Variables.mainMenuHeight * -1
        position        : MainMenu.Position.Top
        hidden          : true
        titles          : [ qsTr("Treatment") , qsTr("Trending") , qsTr("Settings") ]
        spacing         :  20
        leftPdding      :  20
        partitionWidth  : 100 // Todo Important : max width should be the width for all bottons
        hasRightText    : true
        hasLogo         : true
    }

    TreatmentBloodPrime         { id: _treatmentBloodPrime          }
    TreatmentUltrafiltrationItem{ id: _treatmentUltrafiltrationItem }
    TreatmentHome               { id: _treatmentHome
        onSectionFlowClicked    : {
            _treatmentAdjustmentFlow.open()
        }
        onSectionVitalsClicked  : {
            // no page yet
        }
        onSectionPressuresClicked: {
            _treatmentAdjustmentPressuresLimits.open()
        }
        onSectionTimeClicked    : {
            _treatmentAdjustmentDuration.durationValue = vTreatmentTime.time_Total / 60 // minutes => hours
            _treatmentAdjustmentDuration.open()
        }
        onSectionUltrafiltrationClicked: {
            _treatmentUltrafiltrationItem.open()
        }
    }

    //// Treatment Adjustment Dialogs
    TreatmentAdjustmentFlow     { id:       _treatmentAdjustmentFlow
        onCloseClicked          : close()
        onConfirmClicked        : vTreatmentAdjustmentFlows.doAdjustment(bloodFlowRateValue, dialysateFlowRateValue)
    }

    TreatmentAdjustmentPressuresLimits { id:       _treatmentAdjustmentPressuresLimits
        onCloseClicked              : close()
        onConfirmClicked            : vTreatmentAdjustmentPressuresLimits.doAdjustment(
                                          arterialPressureLowerBound ,
                                          arterialPressureUpperBound ,
                                          venousPressureLowerBound   ,
                                          venousPressureUpperBound
                                       )

        arterialPressureMinimum     : Variables.arterialValueMin
        arterialPressureLowerBound  : vTreatmentAdjustmentPressuresLimits.arterialLimitLow
        arterialPressureUpperBound  : vTreatmentAdjustmentPressuresLimits.arterialLimitHigh
        arterialPressureMaximum     : Variables.arterialValueMax

        venousPressureMinimum       : Variables.venousValueMin
        venousPressureLowerBound    : vTreatmentAdjustmentPressuresLimits.venousLimitLow
        venousPressureUpperBound    : vTreatmentAdjustmentPressuresLimits.venousLimitHigh
        venousPressureMaximum       : Variables.venousValueMax

        arterialPressureStep        : Variables.arterialLimitStep
        arterialPressureValue       : vTreatmentPressureOcclusion.arterialPressure

        venousPressureStep          : Variables.venousLimitStep
        venousPressureValue         : vTreatmentPressureOcclusion.venousPressure
    }

    TreatmentAdjustmentDuration { id:       _treatmentAdjustmentDuration
        onCloseClicked          : close()
        onConfirmClicked        : vTreatmentAdjustmentDuration.doAdjustment(durationValue)
    }

    // ---------- Manages Responses ----------
    Connections { target: vTreatmentAdjustmentDuration
        onAdjustmentTriggered   : {
            if ( vTreatmentAdjustmentDuration.adjustment_Accepted ) {
                vTreatmentUltrafiltration.maximum               = vTreatmentAdjustmentDuration.ultrafiltration
                _treatmentAdjustmentDuration.accept()
            } else {
                _treatmentAdjustmentDuration.durationValue      = vTreatmentAdjustmentDuration.duration;
                _treatmentAdjustmentDuration.notification.text  = vTreatmentAdjustmentDuration.text();
            }
        }
    }

    Connections { target: vTreatmentAdjustmentPressuresLimits
        onAdjustmentTriggered   : {
            // values have to be update even when rejected HD is reverting back the values.
            // if accepted will be updated for later [may need refresh later]
            _treatmentAdjustmentPressuresLimits.arterialPressureLowerBound = vTreatmentAdjustmentPressuresLimits.arterialLimitLow
            _treatmentAdjustmentPressuresLimits.arterialPressureUpperBound = vTreatmentAdjustmentPressuresLimits.arterialLimitHigh
            _treatmentAdjustmentPressuresLimits.venousPressureLowerBound   = vTreatmentAdjustmentPressuresLimits.venousLimitLow
            _treatmentAdjustmentPressuresLimits.venousPressureUpperBound   = vTreatmentAdjustmentPressuresLimits.venousLimitHigh

            if (vTreatmentAdjustmentPressuresLimits.adjustment_Accepted) {
                _treatmentAdjustmentPressuresLimits.close()
            }
            else {
                _treatmentAdjustmentPressuresLimits.notification.text = vTreatmentAdjustmentPressuresLimits.text()
            }
        }
    }

    Connections { target: vHDOperationMode
        // 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.
        onInTreatmentChanged    : { page( _treatmentBloodPrime  , vinTreatment      )}
    }

    Connections { target: _treatmentBloodPrime
        onVisibleChanged: {
            if (_treatmentBloodPrime.visible) {
                _treatmentMenu.hidden = true
            }
        }
    }

    Connections { target: vHDTreatmentStates
        // in-Treatmet
        onTxBloodPrimeChanged   : { page( _treatmentBloodPrime  , vtxBloodPrime     )}
        onTxDialysisChanged     : { page( _treatmentHome        , vtxDialysis       )}
    }

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