/*!
 *
 * Copyright (c) 2020-2025 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    TreatmentAdjustmentUltrafiltrationEdit.qml
 * \author  (last)      Stephen Quong
 * \date    (last)      08-Oct-2025
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  15-May-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

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

/*!
 * \brief   TreatmentAdjustmentUltrafiltrationEdit.qml is the screen
 *          To adjust the treatment ultrafiltration
 */
Item { id: _root
    objectName: "_TreatmentAdjustmentUltrafiltrationEdit"  // SquishQt testability

    QtObject { id: _private
        objectName: "_private"
        readonly property int  precision        : Variables.ultrafiltrationPrecision
        readonly property real minimum          : calculatePrecisionValue(   vTreatmentRanges.treatmentRanges_Ultrafiltration_Volume_Min )
        readonly property real maximum          : calculatePrecisionValue(   vTreatmentRanges.treatmentRanges_Ultrafiltration_Volume_Max )
        readonly property real volumeRes        : calculatePrecisionValue(   vTreatmentRanges.ultrafiltrationVolumeRes                   )
        readonly property real setVolume        : calculatePrecisionValue(   vTreatmentUltrafiltration.setVolume                         )
        readonly property real volumeRemoved    : calculatePrecisionValue(   vTreatmentUltrafiltration.volumeRemoved                     )
        readonly property int  multiplier       : Math.pow(10, precision)

        function calculatePrecisionValue(value) {
            return Math.round(value * multiplier) / multiplier
        }
    }

    signal   continueClicked(real vVolume)

    function reset() {
        _volumeGoalAdjuster.value = _private.setVolume
    }

    TreatmentAdjustmentUltrafiltrationMetrics { id: _ufMetrics
        objectName          : "_ufMetrics"
        anchors {
            top             : parent.top
            topMargin       : Variables.defaultMargin
            horizontalCenter: parent.horizontalCenter
        }
        leftLabel   : qsTr("UF Volume Removed")
        leftUnit    : Variables.unitVolume
        leftValue   : _private.volumeRemoved.toFixed(_private.precision)
        rightLabel  : qsTr("UF Volume Goal")
        rightUnit   : Variables.unitVolume
        rightValue  : _volumeGoalAdjuster.value.toFixed(_private.precision)
    }

    Item { id: _contentArea
        objectName: "_contentArea"
        anchors {
            top         : _ufMetrics.bottom
            bottom      : _continueButton.top
            left        : parent.left
            right       : parent.right
        }

        Column { id: _contentColumn
            objectName          : "_contentColumn"
            anchors.centerIn    : parent
            width               : Variables.ultrafiltrationProgressBarWidth
            spacing             : 80

            ProgressBarEx { id: _maxVolumeBar
                objectName      : "_maxVolumeBar"
                width           : parent.width
                decimal : Variables.ultrafiltrationPrecision
                minimum : _private.minimum
                maximum : _private.maximum
                value   : _private.volumeRemoved
                valueEx : _volumeGoalAdjuster.value
            }

            LabelUnitContainer { id: _volumeGoalContainer
                objectName          : "_volumeGoalContainer"
                width               : parent.width
                height              : _ufMetrics.height
                text                : qsTr("UF Volume Goal")
                unitText            : Variables.unitVolume
                contentItem : ValueAdjuster { id: _volumeGoalAdjuster
                    objectName      : "_volumeGoalAdjuster"
                    textColor       : Colors.ufVolumeGoalText
                    isActive        : true
                    decimal         : Variables.ultrafiltrationPrecision
                    minimum         : Math.ceil(_private.volumeRemoved / step) * step
                    maximum         : Math.floor(_private.maximum / step) * step
                    step            : _private.volumeRes
                    value           : { value = _private.setVolume } // set without binding

                    onDidChange     : {
                        value = _private.calculatePrecisionValue(vValue)
                    }
                    onMinimumChanged: {
                        if (value < minimum) {
                            value = minimum
                        }
                    }
                    onMaximumChanged: {
                        if (value > maximum) {
                            value = maximum
                        }
                    }
                }
            }
        }
    }

    TouchRect { id: _continueButton
        objectName  : "_continueButton"
        anchors {
            bottom          : parent.bottom
            bottomMargin    : Variables.defaultMargin
            horizontalCenter: parent.horizontalCenter
        }
        width       : Variables.ultrafiltrationButtonWidth
        height      : Variables.ultrafiltrationButtonHeight
        text {
            text        : qsTr("Continue")
            font.weight : Font.Medium
        }
        isDefault   : true
        enabled     : _volumeGoalAdjuster.value !== _private.setVolume

        onClicked   : {
            continueClicked(_volumeGoalAdjuster.value)
        }
    }
}
