/*!
 *
 * Copyright (c) 2021-2022 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)      05-Apr-2022
 * \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
                value       : vTreatmentCreate.bloodFlowRate
                minimum     : vTreatmentCreate.bloodFlowRateMin
                maximum     : vTreatmentCreate.bloodFlowRateMax
                step        : vTreatmentCreate.bloodFlowRateRes
                onPressed   : vTreatmentCreate.bloodFlowRate = value
                onReleased  : vTreatmentCreate.bloodFlowRate = value
            }

            SliderCreateTreatment { id: _dialysateFlowRate
                objectName  : "_dialysateFlowRate"
                label       : qsTr("Dialysate Flow Rate")
                flickable   : _flickable
                unit        : Variables.unitTextFlowRate
                value       : vTreatmentCreate.dialysateFlowRate
                minimum     : vTreatmentCreate.dialysateFlowRateMin
                maximum     : vTreatmentCreate.dialysateFlowRateMax
                step        : vTreatmentCreate.dialysateFlowRateRes
                onPressed   : vTreatmentCreate.dialysateFlowRate = value
                onReleased  : vTreatmentCreate.dialysateFlowRate = value
            }

            SliderCreateTreatment { id: _duration
                objectName  : "_duration"
                label       : qsTr("Duration")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                value       : vTreatmentCreate.duration
                minimum     : vTreatmentCreate.durationMin
                maximum     : vTreatmentCreate.durationMax
                step        : vTreatmentCreate.durationRes
                onPressed   : vTreatmentCreate.duration = value
                onReleased  : vTreatmentCreate.duration = value
            }

//            ToDo: create a component for this,
//            This is a full implementation of a CheckBox
//            Switch { id: _heparinDelivery
//                text: qsTr("Heparin Delivery")
//                width   : Variables.createTreatmentSliderWidth  + 25

//                anchors.horizontalCenter: parent.horizontalCenter

//                indicator: Rectangle {
//                    implicitWidth   : Variables.sliderCircleDiameter * 1.7
//                    implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
//                    radius          : implicitHeight
//                    x               : _heparinDelivery.leftPadding
//                    y               : parent.height / 2 - height / 2
//                    color           : _heparinDelivery.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
//                    border.color    : _heparinDelivery.checked ? Colors.borderButton            : Colors.createTreatmentInactive
//                    Rectangle { id: _handler
//                        property real diameter  : Variables.sliderCircleDiameter

//                        x: _heparinDelivery.checked ? parent.width - width : 0
//                        anchors.verticalCenter: parent.verticalCenter
//                        width   : diameter
//                        height  : diameter
//                        radius  : diameter
//                        color   : _heparinDelivery.checked ? Colors.highlightProgressBar : Colors.createTreatmentInactive
//                        border  {
//                            width: 4
//                            color: Colors.textMain
//                        }
//                    }
//                }

//                contentItem: Text {
//                    enabled:  _heparinDelivery.checked
//                    text: _heparinDelivery.text
//                    font: _heparinDelivery.font
//                    opacity: enabled ? 1.0 : 0.3
//                    color   : Colors.textButton
//                    verticalAlignment: Text.AlignVCenter
//                    leftPadding: _heparinDelivery.indicator.width + _heparinDelivery.spacing
//                }
//            }

            SliderCreateTreatment { id: _heparinDispensingRate
                objectName  : "_heparinDispensingRate"
                label       : qsTr("Heparin Dispensing Rate")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextDispensingRate
                value       : vTreatmentCreate.heparinDispensingRate
                decimal     : Variables.heparinPrecision
                minimum     : vTreatmentCreate.heparinDispensingRateMin
                maximum     : vTreatmentCreate.heparinDispensingRateMax
                step        : vTreatmentCreate.heparinDispensingRateRes
                onPressed   : vTreatmentCreate.heparinDispensingRate = value
                onReleased  : vTreatmentCreate.heparinDispensingRate = value
                adjustable  : _heparinDispensingRateSwitch.checked
                inActiveZero: true

                Switch { id: _heparinDispensingRateSwitch
                    property bool active: false
                    onCheckedChanged: {
                        if ( ! active ) {
                            active = true
                            checked = ! checked
                        }
                        vTreatmentCreate.heparinDispensingRate  = 0
                        _heparinDispensingRate.value            = 0
                        _heparinDispensingRate.active           = ! checked
                    }

                    x       : width * -2
                    y       : parent.height - 13 // (height / 2)
                    width   : indicator.width
                    indicator: Rectangle {
                        implicitWidth   : Variables.sliderCircleDiameter * 1.7
                        implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                        radius          : implicitHeight
                        x               : _heparinDispensingRateSwitch.leftPadding
                        y               : parent.height / 2 - height / 2
                        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 {
                        width   : _heparinDispensingRateSwitch.width
                        text    : _heparinDispensingRateSwitch.checked ? qsTr("ON") : qsTr("OFF")
                        font    : _heparinDispensingRateSwitch.font
                        color   : _heparinDispensingRateSwitch.active  ? Colors.textMain : Colors.textDisableButton
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter
                        anchors.bottom: _heparinDispensingRateSwitch.top
                        anchors.bottomMargin: 12
                    }
                }
            }

            SliderCreateTreatment { id: _heparinBolusVolume
                objectName  : "_heparinBolusVolume"
                label       : qsTr("Heparin Bolus Volume")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextFluid
                value       : vTreatmentCreate.heparinBolusVolume
                decimal     : Variables.heparinPrecision
                minimum     : vTreatmentCreate.heparinBolusVolumeMin
                maximum     : vTreatmentCreate.heparinBolusVolumeMax
                step        : vTreatmentCreate.heparinBolusVolumeRes
                onPressed   : vTreatmentCreate.heparinBolusVolume = value
                onReleased  : vTreatmentCreate.heparinBolusVolume = value
                adjustable  : _heparinBolusVolumeSwitch.checked
                inActiveZero: true

                Switch { id: _heparinBolusVolumeSwitch
                    property bool active: false
                    onCheckedChanged: {
                        if ( ! active ) {
                            active = true
                            checked = ! checked
                        }
                        vTreatmentCreate.heparinBolusVolume     = 0
                        _heparinBolusVolume.value               = 0
                        _heparinBolusVolume.active              = ! checked

                    }

                    x       : width * -2
                    y       : parent.height - 13 // (height / 2)
                    width   : indicator.width
                    indicator: Rectangle {
                        implicitWidth   : Variables.sliderCircleDiameter * 1.7
                        implicitHeight  : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                        radius          : implicitHeight
                        x               : _heparinBolusVolumeSwitch.leftPadding
                        y               : parent.height / 2 - height / 2
                        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 {
                        width   : _heparinBolusVolumeSwitch.width
                        text    : _heparinBolusVolumeSwitch.checked ? qsTr("ON") : qsTr("OFF")
                        font    : _heparinBolusVolumeSwitch.font
                        color   : _heparinBolusVolumeSwitch.active  ? Colors.textMain : Colors.textDisableButton
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter
                        anchors.bottom: _heparinBolusVolumeSwitch.top
                        anchors.bottomMargin: 12
                    }
                }
            }

            SliderCreateTreatment { id: _heparinStopTime
                objectName  : "_heparinStopTime"
                label       : qsTr("Heparin Stop Time")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                value       : vTreatmentCreate.heparinStopTime
                minimum     : vTreatmentCreate.heparinStopTimeMin
                maximum     : vTreatmentCreate.heparinStopTimeMax
                step        : vTreatmentCreate.heparinStopTimeRes
                onPressed   : vTreatmentCreate.heparinStopTime = value
                onReleased  : vTreatmentCreate.heparinStopTime = value
            }

            SliderCreateTreatment { id: _salineBolus
                objectName  : "_salineBolus"
                label       : "Saline Bolus"
                flickable   : _flickable
                unit        : Variables.unitTextFluid
                value       : vTreatmentCreate.salineBolusVolume
                minimum     : vTreatmentCreate.salineBolusVolumeMin
                maximum     : vTreatmentCreate.salineBolusVolumeMax
                step        : vTreatmentCreate.salineBolusVolumeRes
                onPressed   : vTreatmentCreate.salineBolusVolume = value
                onReleased  : 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          : vTreatmentCreate.heparinTypeOptions
                onClicked       : vTreatmentCreate.heparinType = curIndex
            }

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

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

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

            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
                value       : vTreatmentCreate.dialysateTemp
                minimum     : vTreatmentCreate.dialysateTempMin
                maximum     : vTreatmentCreate.dialysateTempMax
                step        : vTreatmentCreate.dialysateTempRes
                onPressed   : vTreatmentCreate.dialysateTemp = value
                onReleased  : 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.progressbarFluidHeight
                    width           : Variables.createTreatmentSliderWidth
                    diameter        : Variables.sliderCircleDiameter
                    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 : -50
                    lowerTextHorizontalCenter   : true

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

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

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

                    // Ranges
                    step                : Variables.arterialLimitStep
                    gapValue            : Variables.arterialLimitGap
                    minimum             : vTreatmentCreate.arterialPressureLimitLowMin
                    maximum             : vTreatmentCreate.arterialPressureLimitHighMax
                    minValue            : vTreatmentCreate.arterialPressureLimitLowMin
                    maxValue            : vTreatmentCreate.arterialPressureLimitHighMax
                    minValueLowerBound  : vTreatmentCreate.arterialPressureLimitLowMin
                    minValueUpperBound  : vTreatmentCreate.arterialPressureLimitLowMax
                    maxValueLowerBound  : vTreatmentCreate.arterialPressureLimitHighMin
                    maxValueUpperBound  : vTreatmentCreate.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 }
                }
            }

            // 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.progressbarFluidHeight
                    width           : Variables.createTreatmentSliderWidth
                    diameter        : Variables.sliderCircleDiameter
                    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 : -50
                    lowerTextHorizontalCenter   : true

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

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

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

                    // Ranges
                    step                : Variables.arterialLimitStep
                    gapValue            : Variables.arterialLimitGap
                    minimum             : vTreatmentCreate.venousPressureLimitLowMin
                    maximum             : vTreatmentCreate.venousPressureLimitHighMax
                    minValue            : vTreatmentCreate.venousPressureLimitLowMin
                    maxValue            : vTreatmentCreate.venousPressureLimitHighMax
                    minValueLowerBound  : vTreatmentCreate.venousPressureLimitLowMin
                    minValueUpperBound  : vTreatmentCreate.venousPressureLimitLowMax
                    maxValueLowerBound  : vTreatmentCreate.venousPressureLimitHighMin
                    maxValueUpperBound  : vTreatmentCreate.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 }
                }
            }

            SliderCreateTreatment { id: _bloodPressureInterval
                objectName  : "_bloodPressureMeasurementInterval"
                label       : qsTr("Blood Pressure Measurement Interval")
                zeroLabel   : qsTr("OFF")
                flickable   : _flickable
                unit        : Variables.unitTextDuration
                value       : vTreatmentCreate.bloodPressureMeasureInterval
                minimum     : vTreatmentCreate.bloodPressureMeasureIntervalMin
                maximum     : vTreatmentCreate.bloodPressureMeasureIntervalMax
                step        : vTreatmentCreate.bloodPressureMeasureIntervalRes
                onPressed   : vTreatmentCreate.bloodPressureMeasureInterval = value
                onReleased  : vTreatmentCreate.bloodPressureMeasureInterval = value
            }

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

    Connections { target: vTreatmentCreate
        onResetCreateTreatment: {
            _bloodFlowRate          .active = false
            _dialysateFlowRate      .active = false
            _duration               .active = false
            _heparinDispensingRate  .active = false

            _heparinBolusVolume     .active = false
            _heparinStopTime        .active = false

            _salineBolus            .active = false
            _acidConcentrate        .setActive(false)
            _bicarbonateConcentrate .setActive(false)
            _dialyzerType           .setActive(false)

            _dialysateTemperature   .active = false
            _bloodPressureInterval  .active = false
            _rinsebackFlowRate      .active = false

            // TODO : Will be addressed in sub-task DEN-6686.
            // _arterialPressureLimits.isLowActive = false
            // _arterialPressureLimits.isHighActive = false
            // _venousPressureLimits.isLowActive = false
            // _venousPressureLimits.isHighActive = false

        }

        onFwValidationSuccess: {
            _bloodFlowRate                     .valid = true
            _dialysateFlowRate                 .valid = true
            _duration                          .valid = true
            _heparinDispensingRate             .valid = true

            _heparinBolusVolume                .valid = true
            _heparinStopTime                   .valid = true

            _salineBolus                       .valid = true
            //_acidConcentrate                   .setValid    (true)
            // _bicarbonateConcentrate            .setValid    (true)
            // _dialyzerType                      .setValid    (true)
            //
            // _dialysateTemperature              .setValid    (true)
            // _bloodPressureInterval  .setValid    (true)
            // _rinsebackFlowRate                 .setValid    (true)

            // TODO : Will be addressed in sub-task DEN-6686.
            // _arterialPressureLimits            .setLowValid (true)
            // _arterialPressureLimits            .setHighValid(true)
            // _venousPressureLimits              .setLowValid (true)
            // _venousPressureLimits              .setHighValid(true)
        }

        onFwValidationFailed                            : { }
        onBloodFlowRate_ValidationFailed                :  _bloodFlowRate           .valid          = false
        onDialysateFlowRate_ValidationFailed            : _dialysateFlowRate        .valid          = false
        onDuration_ValidationFailed                     : _duration                 .valid          = false
        onHeparinDispensingRate_ValidationFailed        : _heparinDispensingRate    .valid          = false
        onHeparinBolusVolume_ValidationFailed           : _heparinBolusVolume       .valid          = false
        onHeparinStopTime_ValidationFailed              : _heparinStopTime          .valid          = false
        onSalineBolusVolume_ValidationFailed            : _salineBolus              .valid          = false
        onAcidConcentrate_ValidationFailed              : _acidConcentrate          .valid          = false
        onBicarbonateConcentrate_ValidationFailed       : _bicarbonateConcentrate   .valid          = false
        onDialyzerType_ValidationFailed                 : _dialyzerType             .valid          = false
        onDialysateTemp_ValidationFailed                : _dialysateTemperature     .valid          = false
//        onArterialPressureLimitLow_ValidationFailed     : _arterialPressureLimits   .setLowValid    ( false )
//        onArterialPressureLimitHigh_ValidationFailed    : _arterialPressureLimits   .setHighValid   ( false )
//        onVenousPressureLimitLow_ValidationFailed       : _venousPressureLimits     .setLowValid    ( false )
//        onVenousPressureLimitHigh_ValidationFailed      : _venousPressureLimits     .setHighValid   ( false )
        onBloodPressureMeasureInterval_ValidationFailed : _bloodPressureInterval    .valid          = false
        onRinsebackFlowRate_ValidationFailed            : _rinsebackFlowRate        .valid          = false
        onScrollToParameter: {
            let prefix = "data."
            let contentYMargin = 80
            let newContentY = 0

            if (parameter === prefix+"bloodFlowRate") {
                newContentY = _bloodFlowRate.mapToItem(_bloodFlowRate.parent, 0, 0).y
            }
            else if (parameter === prefix+"dialysateFlowRate") {
                newContentY = _dialysateFlowRate.mapToItem(_dialysateFlowRate.parent, 0, 0).y
            }
            else if (parameter === prefix+"duration") {
                newContentY = _duration.mapToItem(_duration.parent, 0, 0).y
            }
            else if (parameter === prefix+"heparinDispensingRate") {
                newContentY = _heparinDispensingRate.mapToItem(_heparinDispensingRate.parent, 0, 0).y
            }
            else if (parameter === prefix+"heparinBolusVolume") {
                newContentY = _heparinBolusVolume.mapToItem(_heparinBolusVolume.parent, 0, 0).y
            }
            else if (parameter === prefix+"heparinStopTime") {
                newContentY = _heparinStopTime.mapToItem(_heparinStopTime.parent, 0, 0).y
            }
            else if (parameter === prefix+"salineBolus") {
                newContentY = _salineBolus.mapToItem(_salineBolus.parent, 0, 0).y
            }
            else if (parameter === prefix+"acidConcentrate") {
                newContentY = _acidConcentrate.mapToItem(_acidConcentrate.parent, 0, 0).y
            }
            else if (parameter === prefix+"bicarbonateConcentrate") {
                newContentY = _bicarbonateConcentrate.mapToItem(_bicarbonateConcentrate.parent, 0, 0).y
            }
            else if (parameter === prefix+"dialyzerType") {
                newContentY = _dialyzerType.mapToItem(_dialyzerType.parent, 0, 0).y
            }
            else if (parameter === prefix+"dialysateTemp") {
                newContentY = _dialysateTemperature.mapToItem(_dialysateTemperature.parent, 0, 0).y
            }
            else if (parameter === prefix+"arterialPressureLimitLow" ||
                     parameter === prefix+"arterialPressureLimitHigh") {
                newContentY = _arterialPressureLimits.mapToItem(_arterialPressureLimits.parent, 0, 0).y
            }
            else if (parameter === prefix+"venousPressureLimitLow" ||
                     parameter === prefix+"venousPressureLimitHigh") {
                newContentY = _venousPressureLimits.mapToItem(_venousPressureLimits.parent, 0, 0).y
            }
            else if (parameter === prefix+"bloodPressureMeasureInterval") {
                newContentY = _bloodPressureInterval.mapToItem(_bloodPressureInterval.parent, 0, 0).y
            }
            else if (parameter === prefix+"rinsebackFlowRate") {
                newContentY = _rinsebackFlowRate.mapToItem(_rinsebackFlowRate.parent, 0, 0).y
            }
            else {
                return
            }
            _flickable.contentY = newContentY - contentYMargin
        }
    }
}
