/*!
 *
 * Copyright (c) 2021-2023 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    PreTreatmentConnectionStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      23-Nov-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/pretreatment"
import "qrc:/pages/pretreatment/connection"

/*!
 * \brief   the pre-treatment consumbles stack screen
 */
StackItem { id: _root
    objectName: "_PreTreatmentConsumblesStack"

    stackView.initialItem   : null

    property int stackStepIndex : 7

    PreTreatmentUltrafiltration { id: _preTreatmentUltrafiltration
        header.stepIndex        : 5
        header.confirmVisible   : true
        title.text              : qsTr("Ultrafiltration Setup")
    }

    PreTreatmentBase { id: _preTreatmentVitals // BP/HR Entry
        function update(vSystolic, vDiastolic ,vHeartRate) {
            _bphrEntry.systolic   = vSystolic   ? vSystolic  : ""
            _bphrEntry.diastolic  = vDiastolic  ? vDiastolic : ""
            _bphrEntry.heartRate  = vHeartRate  ? vHeartRate : ""
        }

        header.stepIndex        : 6
        header.confirmText.text : _bphrEntry.isValid ? qsTr("CONFIRM") : qsTr("SKIP")
        header.backVisible      : true
        header.confirmVisible   : true
        title.text              : qsTr("BP/HR")
        BPHREntry { id: _bphrEntry
            topMarginContent    : 250
            contentRectHeight   : _root.height
        }
        Label {
            text    : qsTr("Press START on Blood Pressure Measurement Cuff to display reading or enter vitals manually")
            width   : parent.width
            anchors {
                bottom: parent.bottom
                bottomMargin: Variables.notificationHeight + Variables.minVGap
                horizontalCenter: parent.horizontalCenter
            }
            horizontalAlignment: Text.AlignHCenter
        }
        onConfirmClicked: {
            if ( _bphrEntry.isValid ) {
                vTreatmentVitals.doConfirm(
                            _bphrEntry.systolic ,
                            _bphrEntry.diastolic,
                            _bphrEntry.heartRate
                            )
            }
            else {
                vTreatmentVitals.doSkip() // only for logging
            }

        }
        Connections { target: vTreatmentVitals
            function onDidTrigger               ( vSystolic, vDiastolic, vHeartRate ) {
                _preTreatmentVitals.update( vSystolic, vDiastolic, vHeartRate )
            }
        }
    }

    PreTreatmentBase { id: _preTreatmentPatientConnection
        header.stepIndex        : 7
        header.backVisible      : true
        header.confirmVisible   : true
        title.text              : qsTr("Patient Connection")
        header.confirmText.text : qsTr("CONTINUE")
        instructionBased        : true
    }

    PreTreatmentBase { id: _preTreatmentStartTreatment
        header.stepIndex        : 8
        header.confirmVisible   : true
        header.backVisible      : true
        title.text              : qsTr("Start Treatment")
        header.confirmText.text : qsTr("START")
        instructionBased        : true
    }

    Connections { target: vPreTreatmentStates
        function onPatientConnectionChanged     ( vValue ) { page( _preTreatmentUltrafiltration      , vValue   )}
    }

    Connections { target: vPreTreatmentAdjustmentUltrafiltrationInit
        function onAdjustmentTriggered          ( vValue ) {
            // the maximum ultrafiltration volume has to be set/revert regardless
            vTreatmentUltrafiltration.maximum      = vPreTreatmentAdjustmentUltrafiltrationInit.volume
            _preTreatmentUltrafiltration.ufVolume  = vPreTreatmentAdjustmentUltrafiltrationInit.volume
            // but only if it has been accepted it will navigate
            if ( vPreTreatmentAdjustmentUltrafiltrationInit.adjustment_Accepted ) {
                page( _preTreatmentVitals )
            }
            else {
                _preTreatmentUltrafiltration.reasonText = vPreTreatmentAdjustmentUltrafiltrationInit.text()
            }
        }
    }

    Connections { target: _preTreatmentUltrafiltration
        function onConfirmClicked               ( vValue ) { vPreTreatmentAdjustmentUltrafiltrationInit.doAdjustment(_preTreatmentUltrafiltration.ufVolume)}
    }

    Connections { target: _preTreatmentVitals
        function onBackClicked                  ( vValue ) { page( _preTreatmentUltrafiltration         )}
        function onConfirmClicked               ( vValue ) { page( _preTreatmentPatientConnection       )
        }
    }

    Connections { target: _preTreatmentPatientConnection
        function onBackClicked                  ( vValue ) { page( _preTreatmentVitals                  )}
        function onConfirmClicked               ( vValue ) { vPreTreatmentAdjustmentPatientConnectionConfirm.doConfirm()}
    }

    Connections { target: _preTreatmentStartTreatment
        function onBackClicked                  ( vValue ) { page( _preTreatmentPatientConnection       )}
        function onConfirmClicked               ( vValue ) { vPreTreatmentAdjustmentStartTreatment.doStart()}
    }

    Connections { target: vPreTreatmentAdjustmentPatientConnectionConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPreTreatmentAdjustmentPatientConnectionConfirm.adjustment_Accepted ) {
                page( _preTreatmentStartTreatment )
                _preTreatmentPatientConnection.reasonText = ""
            } else {
                _preTreatmentPatientConnection.reasonText = vPreTreatmentAdjustmentPatientConnectionConfirm.text()
            }
        }
    }

    Connections { target: vPreTreatmentAdjustmentStartTreatment
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPreTreatmentAdjustmentStartTreatment.adjustment_Accepted ) {
                _preTreatmentStartTreatment.reasonText = ""
            } else {
                _preTreatmentStartTreatment.reasonText = vPreTreatmentAdjustmentStartTreatment.text()
            }
        }
    }

    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = true
        }
        else {
            stackView.initialItem = null
        }
    }
}
