/*!
 *
 * 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    PreTreatmentCreate.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      16-Jun-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  12-Jan-2021
 *
 */

// Qt
import QtQuick 2.12
import QtQuick.Controls 2.12 // ScrollBar

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

/*!
 * \brief   Pre-Treatment create screen
 * \details which contains the components to let user set values of the treatment parameters.
 */
PreTreatmentBase { id: _root
    objectName: "_PreTreatmentCreate" // SquishQt testability

    header.confirmEnabled:
        _bloodFlowRate          .active      && _bloodFlowRate          .          valid &&
        _dialysateFlowRate      .active      && _dialysateFlowRate      .          valid &&
        _duration               .active      && _duration               .          valid &&
        _heparinDispensingRate  .active      && _heparinDispensingRate  .          valid &&
        _heparinBolusVolume     .active      && _heparinBolusVolume     .          valid &&
        _heparinStopTime        .active      && _heparinStopTime        .          valid &&
        _salineBolus            .active      && _salineBolus            .          valid &&
        _heparinType._heparinTypeActive      &&
        _acidConcentrate        .active      &&
        _bicarbonateConcentrate .active      &&
        _dialyzerType           .active      &&
        _dialysateTemperature   .active      && _dialysateTemperature   .          valid &&
        _bloodPressureInterval  .active      && _bloodPressureInterval  .          valid

    Connections{ target:vTreatmentCreate
        // Update flicker location to show the invalid parameter
        function onDidValidationFail() {
            if ( vTreatmentCreate.               bloodFlowRateRejectReason ) { scrollTo( _bloodFlowRate           ); return }
            if ( vTreatmentCreate.           dialysateFlowRateRejectReason ) { scrollTo( _dialysateFlowRate       ); return }
            if ( vTreatmentCreate.           treatmentDurationRejectReason ) { scrollTo( _duration                ); return }
            if ( vTreatmentCreate.       heparinDispensingRateRejectReason ) { scrollTo( _heparinDispensingRate   ); return }
            if ( vTreatmentCreate.          heparinBolusVolumeRejectReason ) { scrollTo( _heparinBolusVolume      ); return }
            if ( vTreatmentCreate.             heparinStopTimeRejectReason ) { scrollTo( _heparinStopTime         ); return }
            if ( vTreatmentCreate.           salineBolusVolumeRejectReason ) { scrollTo( _salineBolus             ); return }

            if ( vTreatmentCreate.                 heparinTypeRejectReason ) { scrollTo( _heparinType             ); return }
            if ( vTreatmentCreate.             acidConcentrateRejectReason ) { scrollTo( _acidConcentrate         ); return }
            if ( vTreatmentCreate.      bicarbonateConcentrateRejectReason ) { scrollTo( _bicarbonateConcentrate  ); return }
            if ( vTreatmentCreate.                dialyzerTypeRejectReason ) { scrollTo( _dialyzerType            ); return }

            if ( vTreatmentCreate.               dialysateTempRejectReason ) { scrollTo( _dialysateTemperature    ); return }
            if ( vTreatmentCreate.bloodPressureMeasureIntervalRejectReason ) { scrollTo( _bloodPressureInterval   ); return }
        }
    }

    function setInteractive(vInteractive) {
        _flickable.interactive = vInteractive
    }

    function scrollTo(vItem) {
        _flickable.contentY = vItem.mapToItem(_flickable.contentItem, 0, 0).y
    }


    ScrollBar {
        flickable   : _flickable
        anchors.fill: _flickable
        anchors.rightMargin : Variables.minVGap
    }

    Flickable { id: _flickable
        objectName: "TreatmentCreateFlickable"
        clip: true
        anchors {
            top             : _root.title.bottom
            topMargin       : Variables.minVGap
            bottom          : _root.bottom
            bottomMargin    : Variables.notificationHeight + Variables.minVGap
            horizontalCenter: parent.horizontalCenter
        }
        width: parent.width
        contentWidth: parent.width
        contentHeight: _column.implicitHeight
        flickDeceleration: Variables.createTreatmentFlickableDeceleration

        Column { id: _column
            spacing: Variables.treatmentSpacing
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.fill: parent

            Text { id: _titleTextPrescription
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("PRESCRIPTION")
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelButton
                font.bold: true
            }

            SliderCreateTreatment { id: _bloodFlowRate
                objectName  : "_bloodFlowRate"
                label       : qsTr("Blood Flow Rate")
                flickable   : _flickable
                unit        : Variables.unitTextFlowRate
                minimum     : vTreatmentRanges.bloodFlowRateMin
                maximum     : vTreatmentRanges.bloodFlowRateMax
                step        : vTreatmentRanges.bloodFlowRateRes
                defaultValue: vTreatmentRanges.bloodFlowRateDef
                valid       : !vTreatmentCreate.bloodFlowRateRejectReason
                onValueChanged : {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.bloodFlowRateRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.bloodFlowRate                  = value
                }
            }

            SliderCreateTreatment { id: _dialysateFlowRate
                objectName  : "_dialysateFlowRate"
                label       : qsTr("Dialysate Flow Rate")
                flickable   : _flickable
                unit        : Variables.unitTextFlowRate
                minimum     : vTreatmentRanges.dialysateFlowRateMin
                maximum     : vTreatmentRanges.dialysateFlowRateMax
                step        : vTreatmentRanges.dialysateFlowRateRes
                defaultValue: vTreatmentRanges.dialysateFlowRateDef
                valid       : !vTreatmentCreate.dialysateFlowRateRejectReason
                onValueChanged : {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.dialysateFlowRateRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.dialysateFlowRate                  = value
                }
            }

            SliderCreateTreatment { id: _duration
                objectName  : "_duration"
                label       : qsTr("Duration")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                minimum     : vTreatmentRanges.treatmentDurationMin
                maximum     : vTreatmentRanges.treatmentDurationMax
                step        : vTreatmentRanges.treatmentDurationRes
                defaultValue: vTreatmentRanges.treatmentDurationDef
                valid       : !vTreatmentCreate.treatmentDurationRejectReason
                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.treatmentDurationRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.treatmentDuration                  = value
                }
            }

            Connections { target : _heparinDispensingRateSwitch
                function onActiveChanged    ( ) {
                    let mActive = _heparinDispensingRateSwitch.active
                    let mObject = _heparinStopTime

                    if ( ! mActive ) {
                        mObject.enabled     = false
                        mObject.active      = false
                        mObject.adjustable  = false
                    }
                }

                function onCheckedChanged   ( ) {
                    let mActive  = _heparinDispensingRateSwitch.active
                    let mChecked = _heparinDispensingRateSwitch.checked
                    let mObject  = _heparinStopTime

                    mObject.enabled     = mActive && mChecked
                    mObject.active      = mActive && ! mChecked
                    mObject.adjustable  = mChecked

                    if ( ! mChecked ) mObject.reset ( 0 )
                }
            }

            SliderCreateTreatment { id: _heparinDispensingRate
                objectName  : "_heparinDispensingRate"
                label       : qsTr("Heparin Dispensing Rate")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextDispensingRate
                decimal     : Variables.heparinPrecision
                minimum     : vTreatmentRanges.heparinDispensingRateMin
                maximum     : vTreatmentRanges.heparinDispensingRateMax
                step        : vTreatmentRanges.heparinDispensingRateRes
                defaultValue: vTreatmentRanges.heparinDispensingRateDef
                valid       : !vTreatmentCreate.heparinDispensingRateRejectReason
                adjustable      : _heparinDispensingRateSwitch.checked
                inActiveZero    : true
                enableAdjustButtons: _heparinDispensingRateSwitch.checked
                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.heparinDispensingRateRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.heparinDispensingRate                  = _heparinDispensingRateSwitch.checked ? value : 0
                }
                //            ToDo: create a component for Switch,
                //            ToDo: Consider putting the new CheckBox component into the SliderCreateTreatment component and set via boolean property
                //            This is a full implementation of a Switch
                Switch { id: _heparinDispensingRateSwitch
                    property bool active: false
                    onActiveChanged : {
                        _heparinDispensingRate.active           = active
                    }
                    onCheckedChanged: {
                        if ( ! active ) {
                            active = true
                            checked = ! checked
                        }
                        vTreatmentCreate.heparinDispensingRate  = 0
                        _heparinDispensingRate.reset            ( 0 )
                        _heparinDispensingRate.active           = ! checked
                    }

                    x       : width * -1.5
                    y       : Variables.createTreatmentSwitchYDisplacement    // these values are set to align the switch with slider
                    width   : 75    // these values are set to align the switch with slider
                    height  : 85    // these values are set to align the switch with slider
                    // DEBUG: background      : Rectangle { color : "white" }

                    indicator: Rectangle {
                        implicitWidth   : Variables.sliderCircleDiameter * 1.7
                        implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                        radius          : implicitHeight
                        anchors.centerIn: parent
                        color           : _heparinDispensingRateSwitch.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
                        border.color    : _heparinDispensingRateSwitch.checked ? Colors.borderButton            : Colors.createTreatmentInactive
                        Rectangle {
                            property real diameter  : Variables.sliderCircleDiameter

                            x: _heparinDispensingRateSwitch.checked ? parent.width - width : 0
                            anchors.verticalCenter: parent.verticalCenter
                            width   : diameter
                            height  : diameter
                            radius  : diameter
                            color   : _heparinDispensingRateSwitch.active ? Colors.highlightProgressBar : Colors.createTreatmentInactive
                            border  {
                                width: Variables.progressbarHandlerBorderWidth
                                color: Colors.textMain
                            }
                        }
                    }

                    contentItem: Text {
                        text    : _heparinDispensingRateSwitch.checked ? qsTr("ON") : qsTr("OFF")
                        font    : _heparinDispensingRateSwitch.font
                        color   : _heparinDispensingRateSwitch.active  ? Colors.textMain : Colors.textDisableButton
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignHCenter
                        anchors.centerIn: parent
                    }
                }
            }

            SliderCreateTreatment { id: _heparinBolusVolume
                objectName  : "_heparinBolusVolume"
                label       : qsTr("Heparin Bolus Volume")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextFluid
                decimal     : Variables.heparinPrecision
                minimum     : vTreatmentRanges.heparinBolusVolumeMin
                maximum     : vTreatmentRanges.heparinBolusVolumeMax
                step        : vTreatmentRanges.heparinBolusVolumeRes
                defaultValue: vTreatmentRanges.heparinBolusVolumeDef
                valid       : !vTreatmentCreate.heparinBolusVolumeRejectReason
                adjustable      : _heparinBolusVolumeSwitch.checked
                inActiveZero    : true
                enableAdjustButtons: _heparinBolusVolumeSwitch.checked
                onValueChanged  : {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.heparinBolusVolumeRejectReason = Variables.noRejectReason
                    }
                    vTreatmentCreate.heparinBolusVolume                 = _heparinBolusVolumeSwitch.checked ? value : 0
                }

                //            ToDo: create a component for this,
                //            ToDo: Consider putting the new CheckBox component into the SliderCreateTreatment component and set via boolean property
                //            This is a full implementation of a CheckBox
                Switch { id: _heparinBolusVolumeSwitch
                    property bool active: false
                    onCheckedChanged: {
                        if ( ! active ) {
                            active = true
                            checked = ! checked
                        }
                        vTreatmentCreate.heparinBolusVolume     = 0
                        _heparinBolusVolume.reset               ( 0 )
                        _heparinBolusVolume.active              = ! checked
                    }

                    x       : width * -1.5
                    y       : Variables.createTreatmentSwitchYDisplacement    // these values are set to align the switch with slider
                    width   : 75    // these values are set to align the switch with slider
                    height  : 85    // these values are set to align the switch with slider
                    // DEBUG: background      : Rectangle { color : "white" }

                    indicator: Rectangle {
                        implicitWidth   : Variables.sliderCircleDiameter * 1.7
                        implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                        radius          : implicitHeight
                        anchors.centerIn: parent
                        color           : _heparinBolusVolumeSwitch.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
                        border.color    : _heparinBolusVolumeSwitch.checked ? Colors.borderButton            : Colors.createTreatmentInactive
                        Rectangle {
                            property real diameter  : Variables.sliderCircleDiameter

                            x: _heparinBolusVolumeSwitch.checked ? parent.width - width : 0
                            anchors.verticalCenter: parent.verticalCenter
                            width   : diameter
                            height  : diameter
                            radius  : diameter
                            color   : _heparinBolusVolumeSwitch.active ? Colors.highlightProgressBar : Colors.createTreatmentInactive
                            border  {
                                width: Variables.progressbarHandlerBorderWidth
                                color: Colors.textMain
                            }
                        }
                    }

                    contentItem: Text {
                        text    : _heparinBolusVolumeSwitch.checked ? qsTr("ON") : qsTr("OFF")
                        font    : _heparinBolusVolumeSwitch.font
                        color   : _heparinBolusVolumeSwitch.active  ? Colors.textMain : Colors.textDisableButton
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignHCenter
                        anchors.centerIn: parent
                    }
                }
            }

            SliderCreateTreatment { id: _heparinStopTime
                objectName  : "_heparinStopTime"
                label       : qsTr("Heparin Stop Time")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                minimum     : vTreatmentRanges.heparinStopTimeMin
                maximum     : vTreatmentRanges.heparinStopTimeMax
                step        : vTreatmentRanges.heparinStopTimeRes
                defaultValue: vTreatmentRanges.heparinStopTimeDef
                valid       : !vTreatmentCreate.heparinStopTimeRejectReason
                enabled         : false // this switch depends on the heparin dispencing
                adjustable      : false // this switch depends on the heparin dispencing
                enableAdjustButtons: _heparinDispensingRateSwitch.checked
                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.heparinStopTimeRejectReason = Variables.noRejectReason
                    }
                    vTreatmentCreate.heparinStopTime                 = value
                }
            }

            SliderCreateTreatment { id: _salineBolus
                objectName  : "_salineBolus"
                label       : "Saline Bolus"
                flickable   : _flickable
                unit        : Variables.unitTextFluid
                minimum     : vTreatmentRanges.salineBolusVolumeMin
                maximum     : vTreatmentRanges.salineBolusVolumeMax
                step        : vTreatmentRanges.salineBolusVolumeRes
                defaultValue: vTreatmentRanges.salineBolusVolumeDef
                valid       : !vTreatmentCreate.salineBolusVolumeRejectReason
                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.salineBolusVolumeRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.salineBolusVolume                  = value
                }
            }

            Text { id: _titleTextOperation
                anchors.horizontalCenter: parent.horizontalCenter
                text        : qsTr("OPERATING  PARAMETERS")
                color       : Colors.textMain
                font.pixelSize: Fonts.fontPixelButton
                font.bold   : true
            }

            GridSelection { id  : _heparinType
                readonly property bool _heparinTypeActive : (enabled && active) || (!enabled)

                objectName      : "_heparinTypeRect"
                title           : qsTr("Heparin Type")
                labels          : vTreatmentRanges.heparinTypeOptions
                onClicked       :{vTreatmentCreate.heparinType = curIndex               ; vTreatmentCreate.heparinTypeSet               = true; }
                enabled         : _heparinDispensingRateSwitch.checked || _heparinBolusVolumeSwitch.checked
                onEnabledChanged: {
                    if (!enabled) {
                        // set disabled value
                        vTreatmentCreate.heparinType    = -1
                        vTreatmentCreate.heparinTypeSet = false
                    }
                }

                valid               : ! vTreatmentCreate.heparinTypeRejectReason
                onCurIndexChanged   :   vTreatmentCreate.heparinTypeRejectReason            = Variables.noRejectReason
            }

            GridSelection { id  : _acidConcentrate
                objectName      : "_acidConcentrateRect"
                title           : qsTr("Acid Concentrate")
                labels          : vTreatmentRanges.acidConcentrateOptions
                onClicked       :{vTreatmentCreate.acidConcentrate = curIndex           ; vTreatmentCreate.acidConcentrateSet           = true; }

                valid               : ! vTreatmentCreate.acidConcentrateRejectReason
                onCurIndexChanged   :   vTreatmentCreate.acidConcentrateRejectReason        = Variables.noRejectReason
            }

            GridSelection { id  : _bicarbonateConcentrate
                objectName      : "_bicarbonateConcentrateRect"
                title           : qsTr("Bicarbonate Concentrate")
                labels          : vTreatmentRanges.bicarbonateConcentrateOptions
                onClicked       :{vTreatmentCreate.bicarbonateConcentrate = curIndex    ; vTreatmentCreate.bicarbonateConcentrateSet    = true; }

                valid               : ! vTreatmentCreate.bicarbonateConcentrateRejectReason
                onCurIndexChanged   :   vTreatmentCreate.bicarbonateConcentrateRejectReason = Variables.noRejectReason
            }

            GridSelection { id  : _dialyzerType
                objectName      : "_dialyzerTypeRect"
                title           : qsTr("Dialyzer Type")
                labels          : vTreatmentRanges.dialyzerTypeOptions
                onClicked       :{vTreatmentCreate.dialyzerType = curIndex              ; vTreatmentCreate.dialyzerTypeSet              = true; }

                valid               : ! vTreatmentCreate.dialyzerTypeRejectReason
                onCurIndexChanged   :   vTreatmentCreate.dialyzerTypeRejectReason           = Variables.noRejectReason
            }

            Item { height: 1; width: 1  /* TODO : there is a design flaw in here, this is a workaround */ }

            SliderCreateTreatment { id: _dialysateTemperature
                objectName  : "_dialysateTemperature"
                label       : qsTr("Dialysate Temperature")
                flickable   : _flickable
                unit        : Variables.unitTextTemperature
                decimal     : Variables.dialysateTempPrecision
                minimum     : vTreatmentRanges.dialysateTempMin
                maximum     : vTreatmentRanges.dialysateTempMax
                step        : vTreatmentRanges.dialysateTempRes
                defaultValue: vTreatmentRanges.dialysateTempDef
                valid       : !vTreatmentCreate.dialysateTempRejectReason

                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.dialysateTempRejectReason  = Variables.noRejectReason
                    }
                    vTreatmentCreate.dialysateTemp                  = value
                }
            }

/*          [DEN-15359] Arterial and Venous Pressure has been removed, it has been kept here since it supposed to become a component and don't watnt to loose the coce.

            // TODO : This has to be a Component
            Column { id: _arterialColumn
                spacing: 45
                anchors.horizontalCenter: parent.horizontalCenter
                Row { id: _arterialTitleRow
                    function setColor() {
                        let slider = _arterialPressureLimits
                        let color = Colors.textDisableButton
                        if ( slider.minAdjusted && slider.maxAdjusted ) {
                            color = Colors.textMain
                        }

                        // if it's invalid upper or lower bound, set invalid color
                        if (!_arterialPressureLimits.lowerBoundValid || !_arterialPressureLimits.upperBoundValid) {
                            color = Colors.createTreatmentInvalidParam
                        }
                        return color
                    }
                    width   : parent.width
                    height  : _arterialLabel.height
                    anchors.left: parent.left
                    Text { id: _arterialLabel
                        visible         : true
                        width           : parent.width/2
                        color           : _arterialTitleRow.setColor()
                        text            : qsTr("Arterial Pressure Limits")
                        font.pixelSize  : Fonts.fontPixelFluidText
                    }
                    Text { id: _arterialUnit
                        visible         : true
                        width           : parent.width/2
                        color           : _arterialLabel.color
                        text            : Variables.unitTextBloodPressure
                        font.pixelSize  : Fonts.fontPixelFluidText
                        horizontalAlignment: Text.AlignRight
                    }
                }
                RangeSlider { id: _arterialPressureLimits
                    objectName  : "_arterialPressureLimitsSlider"
                    // dimension
                    height          : Variables.sliderDefaultBodyHeight
                    width           : Variables.createTreatmentSliderWidth
                    anchors.horizontalCenter: parent.horizontalCenter
                    ticks           : true
                    stepSnap        : true
                    hasAdjust       : true

                    // Texts
                    lowerText.visible           : true
                    lowerText.font.pixelSize    : Fonts.fontPixelFluidText
                    lowerText.font.bold         : false
                    lowerText.anchors.topMargin : -60
                    lowerTextHorizontalCenter   : true

                    upperText.visible           : true
                    upperText.font.pixelSize    : Fonts.fontPixelFluidText
                    upperText.font.bold         : false
                    upperText.anchors.topMargin : -60
                    upperTextHorizontalCenter   : true

                    lowerBoundValid: !vTreatmentCreate.arterialPressureLimitLowRejectReason
                    upperBoundValid: !vTreatmentCreate.arterialPressureLimitHighRejectReason

                    minText.visible             : true
                    minText.font.bold           : false
                    minVerticalEdgeVisible      : false

                    maxText.visible             : true
                    maxText.font.bold           : false
                    maxVerticalEdgeVisible      : false

                    // Ranges
                    step                : vTreatmentRanges.arterialPressureLimitLowRes // currently min & max are on the same slider/scale so used min as the main res
                    gapValue            : vTreatmentRanges.arterialPressureLimitLowGap // currently min & max are on the same slider/scale so used min as the main res
                    minimum             : vTreatmentRanges.arterialPressureLimitLowMin
                    maximum             : vTreatmentRanges.arterialPressureLimitHighMax
                    minValue            : vTreatmentRanges.arterialPressureLimitLowDef
                    maxValue            : vTreatmentRanges.arterialPressureLimitHighDef
                    minValueLowerBound  : vTreatmentRanges.arterialPressureLimitLowMin
                    minValueUpperBound  : vTreatmentRanges.arterialPressureLimitLowMax
                    maxValueLowerBound  : vTreatmentRanges.arterialPressureLimitHighMin
                    maxValueUpperBound  : vTreatmentRanges.arterialPressureLimitHighMax
                    // scroll handling and value updates
                    onPressed           : { setInteractive(false) }
                    onDragged           : { setInteractive(false) }
                    onReleased          : { setInteractive(true ) }
                    onMinValueChanged   : {
                        if ( minAdjusted ) {
                            // Reset the valid state to allow repositioning to the next invalid parameter
                            if(!lowerBoundValid) {
                                vTreatmentCreate.arterialPressureLimitLowRejectReason = Variables.noRejectReason
                            }
                            vTreatmentCreate.arterialPressureLimitLow                 = minValue
                        }
                    }
                    onMaxValueChanged   : {
                        if ( maxAdjusted ) {
                            // Reset the valid state to allow repositioning to the next invalid parameter
                            if(!upperBoundValid) {
                                vTreatmentCreate.arterialPressureLimitHighRejectReason = Variables.noRejectReason
                            }
                            vTreatmentCreate.arterialPressureLimitHigh                 = maxValue
                        }
                    }

                    SliderArrows{ id:_arterialPressureLimitsMaxArrows
                        anchors.verticalCenter  : _arterialPressureLimits.verticalCenter
                        anchors.left            : _arterialPressureLimits.right
                        anchors.leftMargin      : Variables.sliderAdjustButtonLeftMargin
                        onIncrementValue        : _arterialPressureLimits.incrementMax(true)
                        onDecrementValue        : _arterialPressureLimits.decrementMax(true)
                    }

                    SliderArrows{ id:_arterialPressureLimitsMinArrows
                        anchors.verticalCenter  : _arterialPressureLimits.verticalCenter
                        anchors.right           : _arterialPressureLimits.left
                        anchors.rightMargin     : Variables.sliderAdjustButtonRightMargin
                        onIncrementValue        : _arterialPressureLimits.incrementMin(true)
                        onDecrementValue        : _arterialPressureLimits.decrementMin(true)
                    }
                }
            }

            // TODO : This has to be a Component
            Column { id: _venousColumn
                spacing: 45
                anchors.horizontalCenter: parent.horizontalCenter
                Row { id: _venousTitleRow
                    function setColor() {
                        let slider = _venousPressureLimits
                        let color = Colors.textDisableButton
                        if ( slider.minAdjusted && slider.maxAdjusted ) {
                            color = Colors.textMain
                        }

                        // if it's invalid upper or lower bound, set invalid color
                        if (!_venousPressureLimits.lowerBoundValid || !_venousPressureLimits.upperBoundValid) {
                            color = Colors.createTreatmentInvalidParam
                        }
                        return color
                    }
                    width   : parent.width
                    height  : _venousLabel.height
                    anchors.left: parent.left
                    Text { id: _venousLabel
                        visible         : true
                        width           : parent.width/2
                        color           : _venousTitleRow.setColor()
                        text            : qsTr("Venous Pressure Limits")
                        font.pixelSize  : Fonts.fontPixelFluidText
                    }
                    Text { id: _venousUnit
                        visible         : true
                        width           : parent.width/2
                        color           : _venousTitleRow.setColor()
                        text            : Variables.unitTextBloodPressure
                        font.pixelSize  : Fonts.fontPixelFluidText
                        horizontalAlignment: Text.AlignRight
                    }
                }
                RangeSlider { id: _venousPressureLimits
                    objectName: "_venousPressureLimitsSlider"
                    // dimension
                    height          : Variables.sliderDefaultBodyHeight
                    width           : Variables.createTreatmentSliderWidth
                    anchors.horizontalCenter: parent.horizontalCenter
                    ticks           : true
                    stepSnap        : true
                    hasAdjust       : true

                    // slider visuals
                    showTickmarks   : true
                    isRoundedEnds   : true
                    isTickMarksRound: true

                    // Texts
                    lowerText.visible           : true
                    lowerText.font.pixelSize    : Fonts.fontPixelFluidText
                    lowerText.font.bold         : false
                    lowerText.anchors.topMargin : -60
                    lowerTextHorizontalCenter   : true

                    upperText.visible           : true
                    upperText.font.pixelSize    : Fonts.fontPixelFluidText
                    upperText.font.bold         : false
                    upperText.anchors.topMargin : -60
                    upperTextHorizontalCenter   : true

                    lowerBoundValid             : !vTreatmentCreate.venousPressureLimitLowRejectReason
                    upperBoundValid             : !vTreatmentCreate.venousPressureLimitHighRejectReason

                    minText.visible             : true
                    minText.font.bold           : false
                    minVerticalEdgeVisible      : false

                    maxText.visible             : true
                    maxText.font.bold           : false
                    maxVerticalEdgeVisible      : false

                    // Ranges
                    step                : vTreatmentRanges.venousPressureLimitLowRes // currently min & max are on the same slider/scale so used min as the main res
                    gapValue            : vTreatmentRanges.venousPressureLimitLowGap // currently min & max are on the same slider/scale so used min as the main res
                    minimum             : vTreatmentRanges.venousPressureLimitLowMin
                    maximum             : vTreatmentRanges.venousPressureLimitHighMax
                    minValue            : vTreatmentRanges.venousPressureLimitLowDef
                    maxValue            : vTreatmentRanges.venousPressureLimitHighDef
                    minValueLowerBound  : vTreatmentRanges.venousPressureLimitLowMin
                    minValueUpperBound  : vTreatmentRanges.venousPressureLimitLowMax
                    maxValueLowerBound  : vTreatmentRanges.venousPressureLimitHighMin
                    maxValueUpperBound  : vTreatmentRanges.venousPressureLimitHighMax
                    // scroll handling and value updates
                    onPressed           : { setInteractive(false) }
                    onDragged           : { setInteractive(false) }
                    onReleased          : { setInteractive(true ) }
                    onMinValueChanged   : {
                        if ( minAdjusted ) {
                            // Reset the valid state to allow repositioning to the next invalid parameter
                            if(!lowerBoundValid) {
                                vTreatmentCreate.venousPressureLimitLowRejectReason = Variables.noRejectReason
                            }
                            vTreatmentCreate.venousPressureLimitLow                 = minValue
                        }
                    }
                    onMaxValueChanged   : {
                        if ( maxAdjusted ) {
                            // Reset the valid state to allow repositioning to the next invalid parameter
                            if(!upperBoundValid) {
                                vTreatmentCreate.venousPressureLimitHighRejectReason = Variables.noRejectReason
                            }
                            vTreatmentCreate.venousPressureLimitHigh                 = maxValue
                        }
                    }

                    SliderArrows{ id:_venousPressureLimitsMaxArrows
                        anchors.verticalCenter  : _venousPressureLimits.verticalCenter
                        anchors.left            : _venousPressureLimits.right
                        anchors.leftMargin      : Variables.sliderAdjustButtonLeftMargin
                        onIncrementValue        : _venousPressureLimits.incrementMax(true)
                        onDecrementValue        : _venousPressureLimits.decrementMax(true)
                    }

                    SliderArrows{ id:_venousPressureLimitsMinArrows
                        anchors.verticalCenter  : _venousPressureLimits.verticalCenter
                        anchors.right           : _venousPressureLimits.left
                        anchors.rightMargin     : Variables.sliderAdjustButtonRightMargin
                        onIncrementValue        : _venousPressureLimits.incrementMin(true)
                        onDecrementValue        : _venousPressureLimits.decrementMin(true)
                    }
                }
            }

*/

            SliderCreateTreatment { id: _bloodPressureInterval
                objectName  : "_bloodPressureMeasurementInterval"
                label       : qsTr("Blood Pressure Measurement Interval")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                minimum     : vTreatmentRanges.bloodPressureMeasureIntervalMin
                maximum     : vTreatmentRanges.bloodPressureMeasureIntervalMax
                step        : vTreatmentRanges.bloodPressureMeasureIntervalRes
                defaultValue: vTreatmentRanges.bloodPressureMeasureIntervalDef
                valid       : !vTreatmentCreate.bloodPressureMeasureIntervalRejectReason
                adjustable  : _bloodPressureIntervalSwitch.checked
                inActiveZero: true
                enableAdjustButtons: _bloodPressureIntervalSwitch.checked
                onValueChanged: {
                    // Reset the valid state to allow repositioning to the next invalid parameter
                    if(!valid) {
                        vTreatmentCreate.bloodPressureMeasureIntervalRejectReason   = Variables.noRejectReason
                    }
                    vTreatmentCreate.bloodPressureMeasureInterval                   = _bloodPressureIntervalSwitch.checked ? value : 0
                }

                //            ToDo: create a component for this,
                //            ToDo: Consider putting the new CheckBox component into the SliderCreateTreatment component and set via boolean property
                //            This is a full implementation of a CheckBox
                Switch { id: _bloodPressureIntervalSwitch
                    property bool active: false
                    onCheckedChanged: {
                        if ( ! active ) {
                            active = true
                            checked = ! checked
                        }
                        vTreatmentCreate.bloodPressureMeasureInterval   = 0
                        _bloodPressureInterval.reset                    ( 0 )
                        _bloodPressureInterval.active                   = ! checked

                    }

                    x       : width * -1.5
                    y       : Variables.createTreatmentSwitchYDisplacement    // these values are set to align the switch with slider
                    width   : 75    // these values are set to align the switch with slider
                    height  : 85    // these values are set to align the switch with slider
                    // DEBUG: background      : Rectangle { color : "white" }

                    indicator: Rectangle {
                        implicitWidth   : Variables.sliderCircleDiameter * 1.7
                        implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                        radius          : implicitHeight
                        anchors.centerIn: parent
                        color           : _bloodPressureIntervalSwitch.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
                        border.color    : _bloodPressureIntervalSwitch.checked ? Colors.borderButton            : Colors.createTreatmentInactive
                        Rectangle {
                            property real diameter  : Variables.sliderCircleDiameter

                            x: _bloodPressureIntervalSwitch.checked ? parent.width - width : 0
                            anchors.verticalCenter: parent.verticalCenter
                            width   : diameter
                            height  : diameter
                            radius  : diameter
                            color   : _bloodPressureIntervalSwitch.active ? Colors.highlightProgressBar : Colors.createTreatmentInactive
                            border  {
                                width: Variables.progressbarHandlerBorderWidth
                                color: Colors.textMain
                            }
                        }
                    }

                    contentItem: Text {
                        text    : _bloodPressureIntervalSwitch.checked ? qsTr("ON") : qsTr("OFF")
                        font    : _bloodPressureIntervalSwitch.font
                        color   : _bloodPressureIntervalSwitch.active  ? Colors.textMain : Colors.textDisableButton
                        verticalAlignment: Text.AlignTop
                        horizontalAlignment: Text.AlignHCenter
                        anchors.centerIn: parent
                    }
                }
            }

            Item {
                width   : 50
                height  : 50
            }
        }
    }
}
