/*!
 *
 * Copyright (c) 2020-2024 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    TreatmentAdjustmentFlow.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      11-Apr-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  22-Mar-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

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

/*!
 * \brief   TreatmentAdjustmentFlow.qml is the screen
 *          To adjust the treatment blood/dialysate flow
 */
TreatmentAdjustmentBase { id: _root
    objectName: "_TreatmentAdjustmentFlow"  // SquishQt testability

    readonly property alias treatmentModality       : _treatmentModalityComboBox.currentIndex
    readonly property alias bloodFlowRate           : _bloodFlowRate.value
    readonly property alias dialysateFlowRate       : _dialysateFlowRate.value
    readonly property alias dialysateTemperature    : _dialysateTemperature.value
    readonly property alias hepatitus               : _hepatitusStatusControl.currentIndex

    readonly property alias acidConcentrate         : _acidConcentrateComboBox.currentIndex
    readonly property alias bicarbConcentrate       : _bicarbConcentrateComboBox.currentIndex
    readonly property alias sodium                  : _sodium.value
    readonly property alias bicarbonate             : _bicarbonate.value

    width       : 1540
    height      : 865
    titleText   : qsTr("Treatment Parameters")

    QtObject { id: _private
        objectName: "_private"
        readonly property int containerWidth        : (_root.width - (Variables.defaultMargin * 5))/2
        readonly property int titleFontPixelSize    : Fonts.fontPixelContainerTitleSmall
        readonly property int unitFontPixelSize     : Fonts.fontPixelContainerUnitSmall
    }

    onAboutToShow: {
        _treatmentModalityComboBox.currentIndex = vTreatmentCreate.treatmentModality
        _bloodFlowRate.value                    = vTreatmentParametersSetPoint.bloodFlow
        _dialysateFlowRate.value                = vTreatmentParametersSetPoint.dialysateFlow.toFixed(Variables.dialysateFlowPrecision)
        _dialysateTemperature.value             = Variables.notSetVariable(vTreatmentParametersSetPoint.dialysateTemp.toFixed(Variables.dialysateTempPrecision))
        _hepatitusStatusControl.currentIndex    = vTreatmentCreate.hepatitusBStatus
        _acidConcentrateComboBox.currentIndex   = vTreatmentCreate.acidConcentrate
        _bicarbConcentrateComboBox.currentIndex = vTreatmentCreate.dryBicarbCartSize
        _sodium.value                           = vTreatmentCreate.sodium
        _bicarbonate.value                      = vTreatmentCreate.bicarbonate
    }

    Connections {  target: _acidConcentrateAdjustment
        function onAccepted () {
            vTreatmentCreate.acidConcentrate      = _acidConcentrateComboBox.find(_acidConcentrateAdjustment.adjustment)
            _acidConcentrateComboBox.currentIndex = vTreatmentCreate.acidConcentrate
        }
    }

    Item { id: _contentItem
        objectName: "_contentItem"
        anchors {
            top: parent.top
            bottom: notification.top
            left: parent.left
            right: parent.right
        }

        Item { id: _contentArea
            objectName: "_contentArea"
            anchors {
                top         : parent.top
                bottom      : _confirmButton.top
                left        : parent.left
                leftMargin  : Variables.defaultMargin * 2
                right       : parent.right
                rightMargin : anchors.leftMargin
            }

            Row { id: _controlRow
                objectName          : "_controlRow"
                anchors.centerIn    : parent
                spacing             : Variables.defaultMargin

                Column { id: _controlLeftColumn
                    objectName  : "_controlLeftColumn"
                    spacing     : Variables.defaultMargin

                    LabelUnitContainer { id: _treatmentModality
                        text            : qsTr("Treatment Modality")
                        width               : _private.containerWidth

                        contentItem : BaseComboBox { id: _treatmentModalityComboBox
                            anchors.rightMargin : Variables.defaultMargin * 2
                            anchors.leftMargin  : anchors.rightMargin
                            anchors.topMargin   : Variables.defaultMargin / 2
                            anchors.bottomMargin: anchors.topMargin
                            isActive            : true
                            currentIndex        : vTreatmentCreate.treatmentModality
                            model               : vTreatmentRanges.treatmentModalityOptions
                            centerHorizontally  : true
                        }
                    }

                    LabelUnitValueAdjuster { id: _bloodFlowRate
                        objectName          : "_bloodFlowRate"
                        width               : _private.containerWidth
                        text                : qsTr("Blood Flow Rate")
                        titleFontSize       : _private.titleFontPixelSize
                        unitText            : Variables.unitTextFlowRate
                        unitFontSize        : _private.unitFontPixelSize
                        isActive            : true
                        decimal             : Variables.bloodFlowPrecision
                        minimum             : vTreatmentRanges.bloodFlowRateMin
                        maximum             : vTreatmentRanges.bloodFlowRateMax
                        step                : vTreatmentRanges.bloodFlowRateRes

                        onDidChange         : function(vValue) { value = vValue }
                    }

                    LabelUnitValueAdjuster { id: _dialysateFlowRate
                        objectName          : "_dialysateFlowRate"
                        width               : _private.containerWidth
                        text                : qsTr("Dialysate Flow Rate")
                        titleFontSize       : _private.titleFontPixelSize
                        unitText            : Variables.unitTextFlowRate
                        unitFontSize        : _private.unitFontPixelSize
                        isActive            : true
                        decimal             : Variables.dialysateFlowPrecision
                        minimum             : vTreatmentRanges.dialysateFlowRateMin
                        maximum             : vTreatmentRanges.dialysateFlowRateMax
                        step                : vTreatmentRanges.dialysateFlowRateRes
                        canOff              : true

                        onDidChange         : function(vValue) { value = vValue }
                    }

                    LabelUnitValueAdjuster { id: _dialysateTemperature
                        objectName          : "_dialysateTemperature"
                        width               : _private.containerWidth
                        text                : qsTr("Dialysate Temperature")
                        titleFontSize       : _private.titleFontPixelSize
                        unitText            : Variables.unitTextTemperature
                        unitFontSize        : _private.unitFontPixelSize
                        isActive            : true
                        decimal             : Variables.dialysateTempPrecision
                        minimum             : vTreatmentRanges.dialysateTempMin
                        maximum             : vTreatmentRanges.dialysateTempMax
                        step                : vTreatmentRanges.dialysateTempRes

                        onDidChange         : function(vValue) { value = vValue }
                    }

                    LabelUnitContainer { id: _hepatitusStatus
                        text        : qsTr("Hepatitus Status")
                        width               : _private.containerWidth

                        contentItem : ValueAdjusterCustom { id: _hepatitusStatusControl
                            value           : vTreatmentCreate.hepatitusBStatus
                            defaultValue    : vTreatmentCreate.hepatitusBStatus
                            model           : vTreatmentRanges.hepatitisStatus
                        }
                    }
                }

                Column { id: _controlRightColumn
                    objectName: "_controlRightColumn"
                    spacing: Variables.defaultMargin

                    LabelUnitContainer { id: _acidConcentrate
                        objectName          : "_acidConcentrate"
                        width               : _private.containerWidth
                        height              : Variables.adjustmentLabelUnitContainerHeight
                        text                : qsTr("Acid Concentrate")
                        titleFontSize       : _private.titleFontPixelSize
                        unitFontSize        : _private.unitFontPixelSize
        //            showEdit        : _root.editingEnabled // Hide 👋📋 TODO Phase 2
                        onEditClicked       : _acidConcentrateAdjustment.open()

                        contentItem: BaseComboBox { id: _acidConcentrateComboBox
                            objectName          : "_acidConcentrateComboBox"
                            anchors {
                                topMargin   : Variables.defaultMargin / 2
                                bottomMargin: anchors.topMargin
                                leftMargin  : Variables.defaultMargin * 2
                                rightMargin : anchors.leftMargin
                            }
                            font.pixelSize      : _private.titleFontPixelSize
                            isActive            : true
                            currentIndex        : vTreatmentCreate.acidConcentrate
                            model               : vTreatmentRanges.acidConcentrateOptions
                        }
                    }

                    LabelUnitContainer { id: _bicarbConcentrate
                        objectName          : "_bicarbConcentrate"
                        width               : _private.containerWidth
                        height              : Variables.adjustmentLabelUnitContainerHeight
                        text                : qsTr("Bicarbonate Concentrate Size")
                        titleFontSize       : _private.titleFontPixelSize
                        unitFontSize        : _private.unitFontPixelSize

                        contentItem: BaseComboBox { id: _bicarbConcentrateComboBox
                            objectName          : "_bicarbConcentrateComboBox"
                            anchors {
                                topMargin   : Variables.defaultMargin / 2
                                bottomMargin: anchors.topMargin
                                leftMargin  : Variables.defaultMargin * 2
                                rightMargin : anchors.leftMargin
                            }
                            font.pixelSize      : _private.titleFontPixelSize
                            isActive            : true
                            currentIndex        : vTreatmentCreate.dryBicarbCartSize
                            model               : vTreatmentRanges.dryBicabCartridgeSizeOptions
                        }
                    }

                    LabelUnitValueAdjuster { id: _sodium
                        text            : qsTr("Sodium (Na)")
                        unitText        : Variables.unitTextConcentration
                        width           : _private.containerWidth
                        minimum         : vTreatmentRanges.dialysateSodiumMin
                        maximum         : vTreatmentRanges.dialysateSodiumMax
                        step            : vTreatmentRanges.dialysateSodiumRes
                        defaultValue    : vTreatmentRanges.dialysateSodiumDef
                        value           : vTreatmentCreate.sodium
                        isActive        : true

                        onDidChange     : function(vValue) { value = vValue }
                    }

                    LabelUnitValueAdjuster { id: _bicarbonate
                        text            : qsTr("Bicarbonate")
                        unitText        : Variables.unitTextConcentration
                        width           : _private.containerWidth
                        minimum         : vTreatmentRanges.dialysateBicarbonateMin
                        maximum         : vTreatmentRanges.dialysateBicarbonateMax
                        step            : vTreatmentRanges.dialysateBicarbonateRes
                        defaultValue    : vTreatmentRanges.dialysateBicarbonateDef
                        value           : vTreatmentCreate.bicarbonate
                        isActive        : true

                        onDidChange     : function(vValue) { value = vValue }
                    }
                }
            }
        }

        TouchRect { id: _confirmButton
            objectName  : "_confirmButton"
            anchors {
                bottom          : parent.bottom
                bottomMargin    : Variables.defaultMargin
                horizontalCenter: parent.horizontalCenter
            }
            width       : Variables.defaultButtonWidth
            height      : Variables.defaultButtonHeight
            text {
                text        : qsTr("Confirm")
                font.weight : Font.Medium
            }
            isDefault   : true

            onClicked   : { _root.confirmClicked() }
        }
    }
}
