// 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
    readonly property bool heparinFeatured  : vSettings.heparinSyringePump
    readonly property string patientID      : _pretreatmentPatientIDEntry.text
    property bool isHDFFeatured             : vTreatmentCreate.treatmentModality === HeaderBar.Mode_HDF
    property bool isHeparinOn               :  _heparinRxControl.value === PreTreatmentCreateTreatmentSettings.ON

    property bool editingEnabled            : true
    property int columnSpacing              : 15

    spacing      : Variables.defaultMargin * 3

    enum HeparinEnabled {
        OFF,
        ON
    }

    signal clearHeparinDuration()

    onIsHDFFeaturedChanged: {
        if ( ! isHDFFeatured ) {
            vTreatmentCreate.hdfTreatmentMode           = 0
            vTreatmentCreate.substitutionFluidVolume    = 0
        }
    }

    function setPresetParameters () {
        vTreatmentCreate.dialysateTempSet = true
        vTreatmentCreate.dialysateTemp = vTreatmentRanges.dialysateTempDef
        _heparinRxControl.value = 0
        _heparinRx.reset()
    }

    function refreshAll () {
        _hdfTreatmentModeControl        .refresh()
        _subFluidVolume                 .refresh()
        _bloodFlowRate                  .refresh()
        _dialysateFlowRate              .refresh()
        _duration                       .refresh()
        _ufVolume                       .refresh()
        _heparinRxAdjustment            .refresh()
        _dialysateTemperature           .refresh()
    }

    function clear () {
        vTreatmentCreate.patientID      = ""
        _hdfTreatmentModeControl        .clear()
        _subFluidVolume                 .clear()
        _bloodFlowRate                  .clear()
        _dialysateFlowRate              .clear()
        _duration                       .clear()
        _ufVolume                       .clear()
        _heparinRxAdjustment            .clear()
        _acidConcentrateComboBox        .clear()
        _bicarbonateConcentrateComboBox .clear()
        _dialyzerTypeComboBox           .clear()
        _dialysateTemperature           .clear()
    }

    function ready () {

        return (_root.isHDFFeatured ?   _hdfTreatmentModeControl.isActive       && _hdfTreatmentMode          .valid                &&
                                        _subFluidVolume         .isActive       && _subFluidVolume            .valid    : true )    &&
                _bloodFlowRate                                  .isActive       && _bloodFlowRate             .valid                &&
                _dialysateFlowRate                              .isActive       && _dialysateFlowRate         .valid                &&
                _duration                                       .isActive       && _duration                  .valid                &&
                _ufVolume                                       .isActive       && _ufVolume                  .valid                &&
                (_root.isHeparinOn && _root. heparinFeatured ?   _heparinRxAdjustment.confirmReady()                    : true )    &&
                _acidConcentrateComboBox                        .isActive       && _acidConcentrate           .valid                &&
                _bicarbonateConcentrateComboBox                 .isActive       && _bicarbonateConcentrate    .valid                &&
                _dialyzerTypeComboBox                           .isActive       && _dialyzerType              .valid                &&
                _dialysateTemperature                           .isActive       && _dialysateTemperature      .valid
    }

    Column {  id: _leftColumn
        spacing : _root.columnSpacing

        LabelUnitContainer { id: _patientID
            text                            : qsTr("Patient ID")
            contentArea.anchors.leftMargin  : 120

            contentItem : TextEntry { id: _pretreatmentPatientIDEntry
                clip                        : true
                textInput.width             : parent.width - Variables.defaultMargin * 4
                text                        : vTreatmentCreate.patientID
                anchors.centerIn            : parent
                textInput.maximumLength     : 20  // LEAHI-PRS-236
                textInput.rightPadding      : Variables.defaultMargin
                textInput.leftPadding       : Variables.defaultMargin * 14
                textInput.inputMethodHints  : Qt.ImhPreferLowercase
                textInput.echoMode          : TextInput.Normal
                textInput.validator         : RegExpValidator { regExp: Variables.regExp_PatientID }
                line.visible                : false
                enabled                     : _root.editingEnabled
                onEditingFinished           : vTreatmentCreate.patientID = text

                Text { id: _patientIDPlaceHolderText
                    text                : Variables.emptyEntry
                    anchors.fill        : parent
                    rightPadding        : 125
                    horizontalAlignment : Text.AlignRight
                    font.pixelSize      : Fonts.fontPixelValueControl
                    color               : Colors.offWhite
                    visible             :   _pretreatmentPatientIDEntry.textInput.text.length === 0 &&
                                          ! _pretreatmentPatientIDEntry.textInput.activeFocus
                }
            }
        }

        LabelUnitContainer { id: _treatmentModality
            text            : qsTr("Treatment Modality")
            valid           : ! vTreatmentCreate.treatmentModalityRejectReason

            contentItem : BaseComboBox { id: _treatmentModalityComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : true      // PRS-215
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.treatmentModality
                model               : vTreatmentRanges.treatmentModalityOptions
                centerHorizontally  : true

                onActivated         : {
                    if ( ! _treatmentModality.valid ) { vTreatmentCreate.treatmentModalityRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.treatmentModality    = _treatmentModalityComboBox.currentIndex
                }
            }
        }

        LabelUnitContainer { id: _hdfTreatmentMode
            text            : _root.isHDFFeatured ? qsTr("HDF Operating Mode") : ""
            valid           : ! vTreatmentCreate.hDFTreatmentModeRejectReason
            height          : _root.isHDFFeatured ? _root.cellHeight : 0

            contentItem : ValueAdjusterCustom { id: _hdfTreatmentModeControl
                value       : vTreatmentCreate.hdfTreatmentMode
                model       : vTreatmentRanges.hdfTreatmentModeOptions
                isActive    : vTreatmentCreate.hdfTreatmentModeSet
                editable    : _root.editingEnabled
                visible     : _root.isHDFFeatured

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

                onDidChange     : function(vValue) {
                    if ( ! _hdfTreatmentMode.valid ) { vTreatmentCreate.hDFTreatmentModeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.hdfTreatmentModeSet = true
                    vTreatmentCreate.hdfTreatmentMode    = vValue
                }
            }
            Behavior on height  { NumberAnimation   { duration: 250 } }
        }

        LabelUnitValueAdjuster { id: _subFluidVolume
            text            : _root.isHDFFeatured ? qsTr("Substitution Fluid Volume") : ""
            width           : _root.cellWidth
            height          : _root.isHDFFeatured ? _root.cellHeight : 0
            contentItem.visible: _root.isHDFFeatured
            unitText        : _root.isHDFFeatured ? Variables.unitVolume : ""
            valid           : ! vTreatmentCreate.substitutionFluidVolumeRejectReason
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.substitutionVolumeMin
            maximum         : vTreatmentRanges.substitutionVolumeMax
            step            : vTreatmentRanges.substitutionVolumeRes
            defaultValue    : vTreatmentRanges.substitutionVolumeDef
            decimal         : Variables.substitutionPrecision
            value           : vTreatmentCreate.substitutionFluidVolume
            isActive        : vTreatmentCreate.substitutionFluidVolumeSet

            onDidActiveChange   : function(vState) {  vTreatmentCreate.substitutionFluidVolumeSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _ufVolume.valid ) { vTreatmentCreate.substitutionFluidVolumeRejectReason = Variables.noRejectReason }
                vTreatmentCreate.substitutionFluidVolume = vValue
            }
            Behavior on height  { NumberAnimation   { duration: 250 } }
        }

        LabelUnitValueAdjuster { id: _bloodFlowRate
            text            : qsTr("Blood Flow Rate")
            unitText        : Variables.unitTextFlowRate
            valid           : ! vTreatmentCreate.bloodFlowRateRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.bloodFlowRateMin
            maximum         : vTreatmentRanges.bloodFlowRateMax
            step            : vTreatmentRanges.bloodFlowRateRes
            defaultValue    : vTreatmentRanges.bloodFlowRateDef
            value           : vTreatmentCreate.bloodFlowRate
            isActive        : vTreatmentCreate.bloodFlowRateSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.bloodFlowRateSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _bloodFlowRate.valid ) { vTreatmentCreate.bloodFlowRateRejectReason = Variables.noRejectReason }
                vTreatmentCreate.bloodFlowRate = vValue
            }
        }

        LabelUnitValueAdjuster { id: _dialysateFlowRate
            text            : qsTr("Dialysate Flow Rate")
            unitText        : Variables.unitTextFlowRate
            valid           : ! vTreatmentCreate.dialysateFlowRateRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.dialysateFlowRateMin
            maximum         : vTreatmentRanges.dialysateFlowRateMax
            step            : vTreatmentRanges.dialysateFlowRateRes
            defaultValue    : vTreatmentRanges.dialysateFlowRateDef
            value           : vTreatmentCreate.dialysateFlowRate
            isActive        : vTreatmentCreate.dialysateFlowRateSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.dialysateFlowRateSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _dialysateFlowRate.valid ) { vTreatmentCreate.dialysateFlowRateRejectReason = Variables.noRejectReason }
                vTreatmentCreate.dialysateFlowRate = vValue
            }
        }

        LabelUnitValueAdjuster { id: _duration
            text            : qsTr("Duration")
            unitText        : Variables.unitTextDuration
            valid           : ! vTreatmentCreate.treatmentDurationRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.treatmentDurationMin
            maximum         : vTreatmentRanges.treatmentDurationMax
            step            : vTreatmentRanges.treatmentDurationRes
            defaultValue    : vTreatmentRanges.treatmentDurationDef
            value           : vTreatmentCreate.treatmentDuration
            isActive        : vTreatmentCreate.treatmentDurationSet
            onDidActiveChange: function(vState) {  vTreatmentCreate.treatmentDurationSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _duration.valid ) { vTreatmentCreate.treatmentDurationRejectReason = Variables.noRejectReason }
                vTreatmentCreate.treatmentDuration = vValue

                // set heparin time to clear when set
                clearHeparinDuration()
            }
        }
    }

    Column { id: _rightColumn
        spacing : _root.columnSpacing

        LabelUnitValueAdjuster { id: _ufVolume
            text            : qsTr("Ultrafiltration Volume")
            width           : _root.cellWidth
            height          : _root.cellHeight
            unitText        : Variables.unitVolume
            showEdit        : true
            needsAttention  : vTreatmentCreate.ufPreWeightRejectionReason || vTreatmentCreate.ufEstimatedTargetWeightRejectionReason
            valid           : ! vTreatmentCreate.ufVolumeRejectionReason
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.ultrafiltrationVolumeMin
            maximum         : vTreatmentRanges.ultrafiltrationVolumeMax
            step            : vTreatmentRanges.ultrafiltrationVolumeRes
            defaultValue    : vTreatmentRanges.ultrafiltrationVolumeDef
            decimal         : Variables.ultrafiltrationPrecision
            value           : vTreatmentCreate.ultrafiltrationVolume
            isActive        : vTreatmentCreate.ultrafiltrationVolumeSet

            onDidActiveChange   : function(vState) {  vTreatmentCreate.ultrafiltrationVolumeSet = vState }
            onDidChange         : function(vValue) {
                if ( ! _ufVolume.valid ) { vTreatmentCreate.ufVolumeRejectionReason = Variables.noRejectReason }
                vTreatmentCreate.ultrafiltrationVolume = vValue
            }

            onEditClicked   :_ufVolumeAdjustment.open()
        }

        LabelUnitContainer { id: _heparinRx
            text            : qsTr("Heparin Rx")
            valid           : ! vTreatmentCreate.heparinDeliveryDurationRejectReason    &&
                              ! vTreatmentCreate.heparinTypeRejectReason                &&
                              ! vTreatmentCreate.heparinBolusVolumeRejectReason         &&
                              ! vTreatmentCreate.heparinDispensingRateRejectReason
            visible         : _root.heparinFeatured
            showEdit        : true
            needsAttention  : ! _heparinRxAdjustment.confirmReady()
            editEnabled     : _heparinRxControl.value === PreTreatmentCreateTreatmentSettings.ON

            function reset() {
                vTreatmentCreate.heparinBolusVolume       = 0
                vTreatmentCreate.heparinDispensingRate    = 0
                vTreatmentCreate.heparinDeliveryDuration  = 0
            }

            contentItem : ValueAdjusterCustom { id: _heparinRxControl
                defaultValue    : 0
                value           : 0
                editable        : _root.editingEnabled
                model           : [qsTr("OFF"), qsTr("ON")]
                onDidChange     : function(vValue) {
                    value = vValue
                    if ( value ) {
                        _heparinRxAdjustment.open()
                    }
                    else {
                        reset()
                    }
                }
            }

            onEditClicked   : _heparinRxAdjustment.open()
        }

        LabelUnitContainer { id: _acidConcentrate
            text            : qsTr("Acid Concentrate")
//            showEdit        : _root.editingEnabled // Hide 👋📋 TODO Phase 2
            onEditClicked   : _acidConcentrateAdjustment.open()
            valid           : ! vTreatmentCreate.acidConcentrateConversionFactorRejectionReason && ! vTreatmentCreate.acidConcentrateIndexRejectionReason

            contentItem : BaseComboBox { id: _acidConcentrateComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.acidConcentrateSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.acidConcentrate
                centerHorizontally  : true
                model               : vTreatmentRanges.acidConcentrateOptions
                onClear             : {
                    vTreatmentRanges.doClearAcidConcentrate(vTreatmentCreate.acidConcentrateSet)
                    vTreatmentCreate.acidConcentrateSet = false
                }

                onActivated         : {
                    if ( ! _acidConcentrate.valid ) {   vTreatmentCreate.acidConcentrateConversionFactorRejectionReason = Variables.noRejectReason
                                                        vTreatmentCreate.acidConcentrateIndexRejectionReason            = Variables.noRejectReason }
                    vTreatmentCreate.acidConcentrate    = _acidConcentrateComboBox.currentIndex
                    vTreatmentCreate.acidConcentrateSet = true
                    vTreatmentCreate.acidConcentrateConversionFactor = vTreatmentRanges.acidConcentrateModel.get(vTreatmentCreate.acidConcentrate).value
                }
            }
        }

        LabelUnitContainer { id: _bicarbonateConcentrate
            text        : qsTr("Bicarbonate Concentrate Size")
            valid       : ! vTreatmentCreate.dryBicarbCartSizeRejectReason

            contentItem : BaseComboBox { id: _bicarbonateConcentrateComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.dryBicarbCartSizeSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.dryBicarbCartSize
                model               : vTreatmentRanges.dryBicabCartridgeSizeOptions
                centerHorizontally  : true
                onClear             : vTreatmentCreate.dryBicarbCartSizeSet = false
                onActivated         : {
                    if ( ! _bicarbonateConcentrate.valid ) { vTreatmentCreate.dryBicarbCartSizeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.dryBicarbCartSize    = _bicarbonateConcentrateComboBox.currentIndex
                    vTreatmentCreate.dryBicarbCartSizeSet = true
                }
            }
        }

        LabelUnitContainer { id: _dialyzerType
            text        : qsTr("Dialyzer Type")
            valid       : ! vTreatmentCreate.dialyzerTypeRejectReason

            contentItem : BaseComboBox { id: _dialyzerTypeComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.dialyzerTypeSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.dialyzerType
                model               : vTreatmentRanges.dialyzerTypeOptions
                onClear             : vTreatmentCreate.dialyzerTypeSet = false
                onActivated         : {
                    if ( ! _dialyzerType.valid ) { vTreatmentCreate.dialyzerTypeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.dialyzerType    = _dialyzerTypeComboBox.currentIndex
                    vTreatmentCreate.dialyzerTypeSet = true
                }
            }
        }

        LabelUnitValueAdjuster { id: _dialysateTemperature
            text        : qsTr("Dialysate Temperature")
            unitText    : Variables.unitTextTemperature
            valid       : ! vTreatmentCreate.dialysateTempRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.dialysateTempMin
            maximum         : vTreatmentRanges.dialysateTempMax
            step            : vTreatmentRanges.dialysateTempRes
            defaultValue    : vTreatmentRanges.dialysateTempDef
            value           : vTreatmentCreate.dialysateTemp
            decimal         : Variables.dialysateTempPrecision
            isActive        : vTreatmentCreate.dialysateTempSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.dialysateTempSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _dialysateTemperature.valid ) { vTreatmentCreate.dialysateTempRejectReason = Variables.noRejectReason }
                vTreatmentCreate.dialysateTemp = vValue
            }
        }
    }
}
