// Qt
import QtQuick 2.12

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/pretreatment/create"
import "qrc:/pages/treatment/adjustments"

TreatmentAdjustmentBase { id : _root
    property int cellWidth          : Variables.createRxLabelUnitContainerWidth
    property int cellHeight         : Variables.createRxLabelUnitContainerHeight
    property bool editingEnabled    : false

    height          : Variables.smallDialogHeight
    width           : Variables.smallDialogWidth
    titleText       : qsTr("Heparin Rx")
    x               : Math.round((parent.width - width) / 2)
    y               : Math.round((parent.height - height) / 2)

    onCloseClicked  : _root.close()

    onVisibleChanged: {
        if ( visible ) {
            _heparinBolusVolume     .value  = vTreatmentCreate.heparinBolusVolume
            _heparinDispensingRate  .value  = vTreatmentCreate.heparinDispensingRate
            _heparinDeliveryDuration.value  = vTreatmentCreate.heparinDeliveryDuration
        }
    }

    function refresh () {
        _heparinBolusVolume     .refresh()
        _heparinDispensingRate  .refresh()
        _heparinDeliveryDuration.refresh()
    }

    function clear () {
        _heparinBolusVolume     .clear()
        _heparinDispensingRate  .clear()
        _heparinDeliveryDuration.clear()
    }

    function confirmReady () {
        return  _heparinBolusVolume     .isActive   && _heparinBolusVolume      .valid &&
                _heparinDispensingRate  .isActive   && _heparinDispensingRate   .valid &&
                _heparinDeliveryDuration.isActive   && _heparinDeliveryDuration .valid
    }

    Connections { target: vTDOpMode
        function onPreTreatmentChanged() { _root.close() }
    }

    Connections { target: _preTreatmentCreatePatientPrescription
        function onClearHeparinDuration () {
            if ( _heparinDeliveryDuration.enabled ) { _heparinDeliveryDuration.clear() }
        }
    }

    Column { id : _column
        spacing: 15

        anchors {
            verticalCenter      : _root.contentItem.verticalCenter
            verticalCenterOffset: Variables.defaultMargin * -3 // better align label with dialog height
            horizontalCenter    : _root.contentItem.horizontalCenter
        }

        LabelUnitContainer { id: _heparinType
            text                            : qsTr("Heparin Type")
            unitText                        : Variables.unitHeparinType
            contentArea.anchors.leftMargin  : 120

            contentItem : TextEntry { id: _heparinTypeTextEntry
                clip                        : true
                textInput.width             : parent.width - Variables.defaultMargin * 4
                text                        : vTreatmentRanges.heparinTypeOptions[vTreatmentCreate.heparinType] ?? ""
                anchors.centerIn            : parent
                textInput.rightPadding      : Variables.defaultMargin
                textInput.leftPadding       : Variables.defaultMargin * 14
                line.visible                : false
                enabled                     : false
                textInput.color             : Colors.offWhite
            }
        }

        LabelUnitValueAdjuster { id: _heparinBolusVolume
            text            :  qsTr("Heparin Bolus Volume")
            width           : _root.cellWidth
            height          : _root.cellHeight
            unitText        : Variables.unitTextFluid
            valid           : ! vTreatmentCreate.heparinBolusVolumeRejectReason
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.heparinBolusVolumeMin
            maximum         : vTreatmentRanges.heparinBolusVolumeMax
            step            : vTreatmentRanges.heparinBolusVolumeRes
            defaultValue    : vTreatmentRanges.heparinBolusVolumeDef
            value           : vTreatmentCreate.heparinBolusVolume
            decimal         : Variables.heparinPrecision
            canOff          : true
            isActive        : vTreatmentCreate.heparinBolusVolumeSet
            onDidActiveChange   : function(vState) {  vTreatmentCreate.heparinBolusVolumeSet = vState }

            onDidChange         : function(vValue) {
                if ( ! _heparinBolusVolume.valid ) { vTreatmentCreate.heparinBolusVolumeRejectReason = Variables.noRejectReason }
                value = vValue
            }
        }

        LabelUnitValueAdjuster { id: _heparinDispensingRate
            text            : qsTr("Heparin Delivery Rate")
            unitText        : Variables.unitTextDispensingRate
            valid           : ! vTreatmentCreate.heparinDispensingRateRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.heparinDispensingRateMin
            maximum         : vTreatmentRanges.heparinDispensingRateMax
            step            : vTreatmentRanges.heparinDispensingRateRes
            defaultValue    : vTreatmentRanges.heparinDispensingRateDef
            value           : vTreatmentCreate.heparinDispensingRate
            decimal         : Variables.heparinPrecision
            canOff          : true
            isActive        : vTreatmentCreate.heparinDispensingRateSet
            onDidActiveChange: function(vState) {  vTreatmentCreate.heparinDispensingRateSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _heparinDispensingRate.valid ) { vTreatmentCreate.heparinDispensingRateRejectReason = Variables.noRejectReason }
                value = vValue

                // set heparin time to clear when set OFF to value
                if ( value ) { _heparinDeliveryDuration.clear() }

                // if set to OFF set heparin stop time 0
                if ( ! value ) {
                    vTreatmentCreate.heparinDeliveryDurationSet = true
                    _heparinDeliveryDuration.value = 0
                }
            }

            onIsActiveChanged: {
                if ( ! value && isActive ) {
                    vTreatmentCreate.heparinDeliveryDurationSet = true
                    _heparinDeliveryDuration.value = 0
                }
            }
        }

        LabelUnitValueAdjuster { id: _heparinDeliveryDuration
            text            : qsTr("Heparin Delivery Duration")
            unitText        : Variables.unitTextDuration
            valid           : ! vTreatmentCreate.heparinDeliveryDurationRejectReason
            width           : _root.cellWidth
            height          : _root.cellHeight
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.heparinStopTimeMin
            maximum         : vTreatmentCreate.treatmentDuration
            step            : vTreatmentRanges.treatmentDurationRes
            defaultValue    : vTreatmentCreate.treatmentDuration
            value           : vTreatmentCreate.heparinDeliveryDuration
            enabled         : _heparinDispensingRate.value
            canOff          : true
            isActive        : vTreatmentCreate.heparinDeliveryDurationSet
            onDidActiveChange: function(vState) {  vTreatmentCreate.heparinDeliveryDurationSet = vState }
            onDidChange     : function(vValue) {
                if ( ! _heparinDeliveryDuration.valid ) { vTreatmentCreate.heparinDeliveryDurationRejectReason = Variables.noRejectReason }
                value = vValue
            }
        }
    }

    ConfirmButton { id : _confirmButton
        anchors {
            bottom          : _root.contentItem.bottom
            bottomMargin    : Variables.defaultMargin * 3
            horizontalCenter: _root.contentItem.horizontalCenter
        }

        width       : Variables.defaultButtonWidth
        height      : Variables.defaultButtonHeight
        enabled     : _heparinBolusVolume.isActive && _heparinDispensingRate.isActive && _heparinDeliveryDuration.isActive

        anchors {
            top     : undefined
            right   : undefined
            margins : 0
        }

        onClicked   : {
            vTreatmentCreate.heparinBolusVolume         = _heparinBolusVolume.value
            vTreatmentCreate.heparinDispensingRate      = _heparinDispensingRate.value
            vTreatmentCreate.heparinDeliveryDuration    = _heparinDeliveryDuration.value

            _root.close()
        }
    }
}
