/*!
 *
 * 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)      Vy
 * \date    (last)      21-Mar-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 &&
        _dialysateFlowRate      .active &&
        _duration               .active &&
        _heparinDispensingRate  .active &&
        _heparinBolusVolume     .active &&
        _heparinStopTime        .active &&
        _salineBolus            .active &&
        _heparinType            .active &&
        _acidConcentrate        .active &&
        _bicarbonateConcentrate .active &&
        _dialyzerType           .active &&
        _dialysateTemperature   .active &&
        _arterialPressureLimits .minAdjusted &&
        _arterialPressureLimits .maxAdjusted &&
        _venousPressureLimits   .minAdjusted &&
        _venousPressureLimits   .maxAdjusted &&
        _bloodPressureInterval  .active &&
        _rinsebackFlowRate      .active


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

    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
                value       : vTreatmentRanges.bloodFlowRateDef
                onValueChanged: 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
                value       : vTreatmentRanges.dialysateFlowRateDef
                onValueChanged : 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
                value       : vTreatmentRanges.treatmentDurationDef
                onValueChanged: vTreatmentCreate.treatmentDuration = value;
            }

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

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

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

                    if ( ! mChecked ) {
                        mObject.enabled = false
                        mObject.value   = 0
                        if ( mActive ) {
                            mObject.active = true
                        }
                    }
                }
            }

            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
                value       : vTreatmentRanges.heparinDispensingRateDef
                onValueChanged  : vTreatmentCreate.heparinDispensingRate = value
                adjustable      : _heparinDispensingRateSwitch.isSliderOn
                inActiveZero    : true
                enableAdjustButtons: _heparinDispensingRateSwitch.isSliderOn
                showActiveDisabled: _heparinDispensingRateSwitch.active
                onActiveChanged: { _heparinStopTime.enabled = _heparinDispensingRate.active }

                //            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
                    // Due to the desired behavior of the switch:
                    // activating from inactive-OFF to active-OFF before allowing active-ON
                    // Setting checkable to OFF to manually handle check state.
                    // This will avoid a double call to onClickedChanged and also rid of the
                    // need to set to checked property twice (once by Switch object and again by code)
                    property bool isSliderOn : false
                    checkable: false
                    onClicked: {
                        if(!active) {
                            active = true
                            _heparinDispensingRate.overrideActiveVisual(true)
                            _heparinStopTime.overrideActiveVisual(true)
                        } else {
                            _heparinDispensingRateSwitch.isSliderOn = !_heparinDispensingRateSwitch.isSliderOn
                        }
                        _heparinDispensingRate.active = _heparinDispensingRateSwitch.isSliderOn
                        _heparinStopTime.active = _heparinDispensingRateSwitch.isSliderOn
                        checked = _heparinDispensingRateSwitch.isSliderOn
                    }

                    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
                value       : vTreatmentRanges.heparinBolusVolumeDef
                onValueChanged  : vTreatmentCreate.heparinBolusVolume = value
                adjustable      : _heparinBolusVolumeSwitch.isSliderOn
                inActiveZero    : true
                enableAdjustButtons: _heparinBolusVolumeSwitch.isSliderOn
                showActiveDisabled: _heparinBolusVolumeSwitch.active
                active:false

                //            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
                    // Due to the desired behavior of the switch:
                    // activating from inactive-OFF to active-OFF before allowing active-ON
                    // Setting checkable to OFF to manually handle check state.
                    // This will avoid a double call to onClickedChanged and also rid of the
                    // need to set to checked property twice (once by Switch object and again by code)
                    property bool isSliderOn : false
                    checkable: false
                    onClicked: {
                        if(!active) {
                            active = true
                            _heparinBolusVolume.overrideActiveVisual(true)
                        } else {
                            _heparinBolusVolumeSwitch.isSliderOn = !_heparinBolusVolumeSwitch.isSliderOn
                        }
                        _heparinBolusVolume.active = _heparinBolusVolumeSwitch.isSliderOn
                        checked = _heparinBolusVolumeSwitch.isSliderOn
                    }

                    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
                value       : vTreatmentRanges.heparinStopTimeDef
                onValueChanged  : vTreatmentCreate.heparinStopTime = value
                enabled         : false // this switch depends on the heparin dispencing
                showActiveDisabled  : _heparinDispensingRateSwitch.active
                inActiveZero    : true
                enableAdjustButtons : _heparinDispensingRateSwitch.isSliderOn
                adjustable      : _heparinDispensingRateSwitch.isSliderOn
            }

            SliderCreateTreatment { id: _salineBolus
                objectName  : "_salineBolus"
                label       : "Saline Bolus"
                flickable   : _flickable
                unit        : Variables.unitTextFluid
                minimum     : vTreatmentRanges.salineBolusVolumeMin
                maximum     : vTreatmentRanges.salineBolusVolumeMax
                step        : vTreatmentRanges.salineBolusVolumeRes
                value       : vTreatmentRanges.salineBolusVolumeDef
                onValueChanged : 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
                objectName      : "_heparinTypeRect"
                title           : qsTr("Heparin Type")
                labels          : vTreatmentRanges.heparinTypeOptions
                onClicked       :{vTreatmentCreate.heparinType = curIndex               ; vTreatmentCreate.heparinTypeSet               = true; }
            }

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

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

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

            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
                value       : vTreatmentRanges.dialysateTempDef
                onValueChanged : vTreatmentCreate.dialysateTemp = value
            }

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

                    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 ) vTreatmentCreate.arterialPressureLimitLow  = minValue }
                    onMaxValueChanged   : { if ( maxAdjusted ) vTreatmentCreate.arterialPressureLimitHigh = maxValue }
                    onClicked           : { if ( minAdjusted ) vTreatmentCreate.arterialPressureLimitLow  = minValue
                                            if ( maxAdjusted ) 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
                        }
                        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

                    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 ) vTreatmentCreate.venousPressureLimitLow  = minValue }
                    onMaxValueChanged   : { if ( maxAdjusted ) vTreatmentCreate.venousPressureLimitHigh = maxValue }
                    onClicked           : { if ( minAdjusted ) vTreatmentCreate.venousPressureLimitLow  = minValue
                                            if ( maxAdjusted ) 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
                value       : vTreatmentRanges.bloodPressureMeasureIntervalDef
                onValueChanged: vTreatmentCreate.bloodPressureMeasureInterval = value
                adjustable  : _bloodPressureIntervalSwitch.isSliderOn
                inActiveZero: true
                enableAdjustButtons: _bloodPressureIntervalSwitch.isSliderOn
                showActiveDisabled: _bloodPressureIntervalSwitch.active
                //            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
                    // Due to the desired behavior of the switch:
                    // activating from inactive-OFF to active-OFF before allowing active-ON
                    // Setting checkable to OFF to manually handle check state.
                    // This will avoid a double call to onClickedChanged and also rid of the
                    // need to set to checked property twice (once by Switch object and again by code)
                    property bool isSliderOn : false
                    checkable: false
                    onClicked: {
                        if(!active) {
                            active = true
                            _bloodPressureInterval.overrideActiveVisual(true)
                        } else {
                            _bloodPressureIntervalSwitch.isSliderOn = !_bloodPressureIntervalSwitch.isSliderOn
                        }
                        _bloodPressureInterval.active = _bloodPressureIntervalSwitch.isSliderOn
                        checked = _bloodPressureIntervalSwitch.isSliderOn
                    }

                    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
                    }
                }
            }

            SliderCreateTreatment { id: _rinsebackFlowRate
                objectName  : "_rinsebackFlowRate"
                label       : qsTr("Rinseback Flow Rate")
                flickable   : _flickable
                unit        : Variables.unitTextFlowRate
                minimum     : vTreatmentRanges.rinsebackFlowRateMin
                maximum     : vTreatmentRanges.rinsebackFlowRateMax
                step        : vTreatmentRanges.rinsebackFlowRateRes
                value       : vTreatmentRanges.rinsebackFlowRateDef
                onValueChanged : vTreatmentCreate.rinsebackFlowRate = value
            }
            Item {
                width   : 50
                height  : 50
            }
        }
    }
}
