/*!
 *
 * 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    TreatmentAdjustmentIsolatedUFEdit.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   TreatmentAdjustmentIsolatedUFDurationEdit.qml is the screen
 *          To adjust the treatment isolated UF duration
 */
Item { id: _root
    objectName: "_treatmentAdjustmentIsolatedUFDurationEdit"  // SquishQt testability

    property alias duration             : _durationAdjuster.value

    signal   continueClicked(int vDuration)

    function reset() {
        duration = vTreatmentIsolatedUF.duration
    }

    QtObject { id: _private
        objectName: "_private"
        readonly property int  precision        : Variables.ultrafiltrationPrecision
        readonly property real volumeRemoved    : calculatePrecisionValue(vTreatmentIsolatedUF.volumeDelivered)
        readonly property real volume           : calculatePrecisionValue(vTreatmentIsolatedUF.setVolume)
        readonly property real volumeMax        : Math.floor(_root.volumeMax / volumeRes) * volumeRes
        readonly property real volumeRes        : calculatePrecisionValue(vTreatmentRanges.ultrafiltrationVolumeRes)

        readonly property int  multiplier   : Math.pow(10, precision)
        function calculatePrecisionValue(value) {
            return Math.round(value * multiplier) / multiplier
        }
    }

    TreatmentAdjustmentUltrafiltrationMetrics { id: _ufMetrics
        objectName: "_ufMetrics"
        anchors {
            top             : parent.top
            topMargin       : Variables.defaultMargin
            horizontalCenter: parent.horizontalCenter
        }
        leftLabel   : qsTr("Isolated UF Volume Removed")
        leftUnit    : Variables.unitVolume
        leftValue   : _private.volumeRemoved.toFixed(_private.precision)
        rightLabel  : qsTr("Isolated UF Volume Goal")
        rightUnit   : Variables.unitVolume
        rightValue  : _private.volume.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
                verticalCenterOffset    : -(Variables.defaultMargin)
            }
            width       : Variables.ultrafiltrationProgressBarWidth
            spacing     : 80

            ProgressBarEx { id: _volumeProgress
                objectName      : "_volumeProgress"
                anchors {
                    left    : parent.left
                    right   : parent.right
                }
                decimal : Variables.ultrafiltrationPrecision
                minimum : 0
                maximum : _private.volume
                value   : _private.volumeRemoved
                valueEx : _private.volumeRemoved
            }

            LabelUnitValueAdjuster { id: _durationAdjuster
                objectName          : "_durationAdjuster"
                anchors {
                    horizontalCenter: parent.horizontalCenter
                }
                text                : qsTr("Isolated UF Duration")
                isActive            : true
                unitText            : Variables.unitTextDuration
                minimum             : vTreatmentRanges.isolatedUFDurationMin
                maximum             : vTreatmentRanges.isolatedUFDurationMax
                step                : vTreatmentRanges.isolatedUFDurationRes

                onDidChange         : function(vValue) {
                    value       = 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     : _durationAdjuster.value != vTreatmentIsolatedUF.duration

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