/*!
 *
 * 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 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, Variables.ultrafiltrationPrecision)
        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
        }
        setVolume           : _volumeGoalAdjuster.value
        volumeRemoved       : _private.volumeRemoved
        volumeRemovedColor  : Colors.ufVolumeGoalText
    }

    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

            ProgressBar { id: _maxVolumeBar
                objectName      : "_maxVolumeBar"
                width           : parent.width
                height          : Variables.ultraFiltrationProgressBarHeight
                marker.visible  : false
                bgColor         : Colors.ufAdjustmentProgressBarBg
                color           : Colors.ufAdjustmentDeltaFill
                radius          : Variables.ultrafiltrationProgressBarRadius
                minText {
                    font {
                        pixelSize   : Fonts.fontPixelUltrafiltrationMinMaxLabel
                        weight      : Font.Normal
                    }
                    color           : Colors.progressBarMinMax
                    text            : minimum.toFixed(Variables.ultrafiltrationPrecision) + " " + Variables.unitVolume
                }
                maxText {
                    font {
                        pixelSize   : minText.font.pixelSize
                        weight      : minText.font.weight
                    }
                    color           : Colors.progressBarMinMax
                    text            : _private.maximum.toFixed(Variables.ultrafiltrationPrecision) + " " + Variables.unitVolume
                }
                minimum : _private.minimum
                maximum : _private.maximum
                decimal : Variables.ultrafiltrationPrecision
                value   : _volumeGoalAdjuster.value

                Rectangle { id: _removedFill
                    objectName: "_removedFill"
                    anchors {
                        top         : parent.top
                        bottom      : parent.bottom
                        left        : parent.left
                        right       : _lowMarker.right
                        // adjust margin so fill completely covers the underlying slider
                        leftMargin  : -1
                    }
                    z       : parent.z + 1
                    color   : Colors.ufProgressBarFill
                    radius  : parent.radius
                }

                RangeMarker { id: _lowMarker
                    objectName      : "_lowMarker"
                    anchors.bottom  : parent.bottom
                    x               : (_maxVolumeBar.width * ((value - _maxVolumeBar.minimum) / (_maxVolumeBar.maximum - _maxVolumeBar.minimum))) - ((width+1)/2)
                    z               : _removedFill.z + 1
                    width           : Variables.ultrafiltrationRangeMarkerWidth
                    height          : Variables.rangeMarkerHeight
                    hasHandle       : false
                    valueOnTop      : true
                    decimal         : Variables.ultrafiltrationPrecision
                    value           : Math.max(_private.volumeRemoved, _private.minimum)
                    text {
                        anchors.bottomMargin: Variables.ultrafiltrationRangeMarkerTextMargin
                        font {
                            pixelSize: Fonts.fontPixelUltrafiltrationRangeMarker
                            weight: Font.DemiBold
                        }
                    }
                }
            }

            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)
        }
    }
}
