// Qt
import QtQuick 2.15
import QtQuick.Controls 2.15


//  Qml
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"


Row { id: _root
    readonly property int cellWidth         : Variables.createRxLabelUnitContainerWidth
    readonly property int cellHeight        : Variables.createRxLabelUnitContainerHeight
    property bool editingEnabled            : true
    property int columnSpacing              : 15

    spacing      : Variables.defaultMargin * 3

    function setPresetParameters () {
        vTreatmentCreate.sodiumSet                          = true
        vTreatmentCreate.bicarbonateSet                     = true
        vTreatmentCreate.fluidBolusVolumeSet                = true
        vTreatmentCreate.rinsebackVolumeSet                 = true
        vTreatmentCreate.bloodPressureMeasureIntervalSet    = true
        vTreatmentCreate.hepatitusBStatusSet                = true

        vTreatmentCreate.sodium                         = vTreatmentRanges.dialysateSodiumDef
        vTreatmentCreate.bicarbonate                    = vTreatmentRanges.dialysateBicarbonateDef
        vTreatmentCreate.fluidBolusVolume               = vTreatmentRanges.salineBolusVolumeDef
        vTreatmentCreate.rinsebackVolume                = vTreatmentRanges.rinsebackVolumeDef
        vTreatmentCreate.bloodPressureMeasureInterval   = vTreatmentRanges.bloodPressureMeasureInterval[vTreatmentRanges.vitalsInterval]
        vTreatmentCreate.hepatitusBStatus               = 0
    }

    function refreshAll () {
        _sodium                         .refresh()
        _bicarbonate                    .refresh()
        _fluidBolusVolume               .refresh()
        _primeRinsebackVolume           .refresh()
        _bpMeasurementIntervalControl   .refresh()
        _hepatitusStatusControl         .refresh()
    }

    function clear () {
        _sodium                         .clear()
        _bicarbonate                    .clear()
        _fluidBolusVolume               .clear()
        _primeRinsebackVolume           .clear()
        _bpMeasurementIntervalControl   .clear()
        _hepatitusStatusControl   .     clear()
    }

    function ready () {
        return  _sodium                         .isActive    && _sodium                 .valid  &&
                _bicarbonate                    .isActive    && _bicarbonate            .valid  &&
                _fluidBolusVolume               .isActive    && _fluidBolusVolume       .valid  &&
                _primeRinsebackVolume           .isActive    && _primeRinsebackVolume   .valid  &&
                _bpMeasurementIntervalControl   .isActive    && _bpMeasurementInterval  .valid  &&
                _hepatitusStatusControl         .isActive    && _hepatitusStatus        .valid
    }

    Column {  id: _leftColumn
        spacing : _root.columnSpacing

        LabelUnitValueAdjuster { id: _sodium
            text            : qsTr("Sodium (Na)")
            unitText        : Variables.unitTextConcentration
            valid           : ! vTreatmentCreate.sodiumRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.dialysateSodiumMin
            maximum         : vTreatmentRanges.dialysateSodiumMax
            step            : vTreatmentRanges.dialysateSodiumRes
            defaultValue    : vTreatmentRanges.dialysateSodiumDef
            value           : vTreatmentCreate.sodium
            isActive        : vTreatmentCreate.sodiumSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.sodiumSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _sodium.valid ) { vTreatmentCreate.sodiumRejectReason = Variables.noRejectReason }
                vTreatmentCreate.sodium = vValue
            }
        }

        LabelUnitValueAdjuster { id: _bicarbonate
            text            : qsTr("Bicarbonate")
            unitText        : Variables.unitTextConcentration
            valid           : ! vTreatmentCreate.bicarbonateRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.dialysateBicarbonateMin
            maximum         : vTreatmentRanges.dialysateBicarbonateMax
            step            : vTreatmentRanges.dialysateBicarbonateRes
            defaultValue    : vTreatmentRanges.dialysateBicarbonateDef
            value           : vTreatmentCreate.bicarbonate
            isActive        : vTreatmentCreate.bicarbonateSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.bicarbonateSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _bicarbonate.valid ) { vTreatmentCreate.bicarbonateRejectReason = Variables.noRejectReason }
                vTreatmentCreate.bicarbonate = vValue
            }
        }

        LabelUnitValueAdjuster { id: _fluidBolusVolume
            text            : qsTr("Fluid Bolus Volume")
            unitText        : Variables.unitTextFluid
            valid           : ! vTreatmentCreate.fluidBolusVolumeRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.salineBolusVolumeMin
            maximum         : vTreatmentRanges.salineBolusVolumeMax
            step            : vTreatmentRanges.salineBolusVolumeRes
            defaultValue    : vTreatmentRanges.salineBolusVolumeDef
            value           : vTreatmentCreate.fluidBolusVolume
            isActive        : vTreatmentCreate.fluidBolusVolumeSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.fluidBolusVolumeSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _fluidBolusVolume.valid ) { vTreatmentCreate.fluidBolusVolumeRejectReason = Variables.noRejectReason }
                vTreatmentCreate.fluidBolusVolume = vValue
            }
        }

        LabelUnitValueAdjuster { id: _primeRinsebackVolume
            text            : qsTr("Prime Rinseback Volume")
            unitText        : Variables.unitTextFluid
            valid           : ! vTreatmentCreate.rinsebackVolumeRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.rinsebackVolumeMin
            maximum         : vTreatmentRanges.rinsebackVolumeMax
            step            : vTreatmentRanges.rinsebackVolumeRes
            defaultValue    : vTreatmentRanges.rinsebackVolumeDef
            value           : vTreatmentCreate.rinsebackVolume
            isActive        : vTreatmentCreate.rinsebackVolumeSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.rinsebackVolumeSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _primeRinsebackVolume.valid ) { vTreatmentCreate.rinsebackVolumeRejectReason = Variables.noRejectReason }
                vTreatmentCreate.rinsebackVolume = vValue
            }
        }
    }

    Column { id: _rightColumn
        spacing : _root.columnSpacing

        LabelUnitContainer { id: _bpMeasurementInterval
            text        : qsTr("Vitals Interval")
            unitText    : Variables.unitTextDuration
            valid       : ! vTreatmentCreate.bloodPressureMeasureIntervalRejectReason

            contentItem : ValueAdjusterCustom { id: _bpMeasurementIntervalControl
                editable        : _root.editingEnabled
                isActive        : vTreatmentCreate.bloodPressureMeasureIntervalSet
                defaultValue    : vTreatmentRanges.vitalsInterval
                value           : vTreatmentRanges.bloodPressureMeasureInterval.indexOf(vTreatmentCreate.bloodPressureMeasureInterval.toString())
                model           : vTreatmentRanges.bloodPressureMeasureInterval
                canOff          : true

                onDidActiveChange: function(vState) {  vTreatmentCreate.bloodPressureMeasureIntervalSet = vState }

                onDidChange     : function(vValue) {
                    if ( ! _bpMeasurementInterval.valid ) {
                        vTreatmentCreate.bloodPressureMeasureIntervalRejectReason = Variables.noRejectReason
                    }

                    vTreatmentCreate.bloodPressureMeasureInterval       = vTreatmentRanges.bloodPressureMeasureInterval[vValue]
                }
            }
        }

        LabelUnitContainer { id: _hepatitusStatus
            text        : qsTr("Hepatitus Status")
            valid       : ! vTreatmentCreate.hepatitusBStatusRejectReason

            contentItem : ValueAdjusterCustom { id: _hepatitusStatusControl
                editable        : _root.editingEnabled
                isActive        : vTreatmentCreate.hepatitusBStatusSet
                value           : vTreatmentCreate.hepatitusBStatus
                defaultValue    : vTreatmentCreate.hepatitusBStatus
                model           : vTreatmentRanges.hepatitisStatus

                onDidActiveChange: function(vState) {  vTreatmentCreate.hepatitusBStatusSet = vState }

                onDidChange     : function(vValue) {
                    if ( ! _hepatitusStatus.valid ) {
                        vTreatmentCreate.hepatitusBStatusRejectReason = Variables.noRejectReason
                    }

                    vTreatmentCreate.hepatitusBStatus       = vValue
                }
            }
        }
    }
}
