/*!
 *
 * 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    TreatmentAdjustmentDurationEdit.qml
 * \author  (last)      Stephen Quong
 * \date    (last)      11-Nov-2025
 * \author  (original)  Stephen Quong
 * \date    (original)  11-Nov-2025
 *
 */

// Qt
import QtQuick 2.12

// Project

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

/*!
 * \brief   TreatmentAdjustmentDurationEdit.qml is the screen
 *          to edit the treatment duration
 */
Item { id: _root
    objectName: "_TreatmentAdjustmentDurationEdit"

    QtObject { id: _private
        objectName: "_private"
        readonly property int  ufMultiplier         : Math.pow(10, Variables.ultrafiltrationPrecision)
        readonly property real ufVolumeRes          : calculatePrecisionValue(   vTreatmentRanges.ultrafiltrationVolumeRes, ufMultiplier  )
        readonly property real ufSetVolume          : calculatePrecisionValue(   vTreatmentUltrafiltration.setVolume      , ufMultiplier  )
        readonly property real ufVolumeRemoved      : calculatePrecisionValue(   vTreatmentUltrafiltration.volumeRemoved  , ufMultiplier  )
        readonly property int  treatmentTimeTotal   : vTreatmentTime.time_Total

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

    signal continueClicked(int vNewTreatmentDuration)

    function reset() {
        _newTreatmentDuration.value = _private.treatmentTimeTotal
    }

    Row { id: _infoRow
        objectName: "_infoRow"

        readonly property int cellWidth: (_infoRow.width - (_infoRow.spacing * 2)) / 3

        anchors {
            top: parent.top
            topMargin: Variables.defaultMargin * 2
            left: parent.left
            leftMargin: Variables.defaultMargin * 2
            right: parent.right
            rightMargin: anchors.leftMargin
        }
        spacing: Variables.defaultMargin

        LabelUnitText { id: _ufVolumeRemoved
            objectName  : "_ufVolumeRemoved"
            width       : _infoRow.cellWidth
            label       : qsTr("UF Volume Removed")
            unit        : Variables.unitVolume
            value       : _private.ufVolumeRemoved
        }

        LabelUnitText { id: _ufVolumeGoal
            objectName  : "_ufVolumeGoal"
            width       : _infoRow.cellWidth
            label       : qsTr("UF Volume Goal")
            unit        : Variables.unitVolume
            value       : _private.ufSetVolume
        }

        LabelUnitText { id: _timeElapsed
            objectName  : "_timeElapsed"
            width       : _infoRow.cellWidth
            label       : qsTr("Treatment Time Elapsed")
            unit        : Variables.unitTextDuration
            value       : vTreatmentTime.time_Elapsed
        }
    }

    Rectangle { id: _divider
        objectName: "_divider"
        anchors {
            top: _infoRow.bottom
            topMargin: Variables.defaultMargin * 3
            left: parent.left
            leftMargin: 50
            right: parent.right
            rightMargin: anchors.leftMargin
        }
        height: 2
        color: Colors.panelBorderColor

    }

    LabelUnitValueAdjuster { id: _newTreatmentDuration
        objectName          : "_newTreatmentDuration"
        anchors {
            top: _divider.bottom
            topMargin: _divider.anchors.topMargin
            horizontalCenter: parent.horizontalCenter
        }
        text                : qsTr("New Treatment Duration")
        titleFontSize       : Fonts.fontPixelContainerTitleSmall
        unitText            : Variables.unitTextDuration
        unitFontSize        : Fonts.fontPixelContainerUnitSmall
        isActive            : true
        minimum             : Math.ceil(vTreatmentTime.time_Elapsed / step) * step
        maximum             : Math.floor(vTreatmentRanges.treatmentRanges_Duration_Max / step) * step
        step                : vTreatmentRanges.treatmentDurationRes

        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.defaultButtonWidth
        height      : Variables.defaultButtonHeight
        text {
            text        : qsTr("Continue")
            font.weight : Font.Medium
        }
        isDefault   : true
        enabled     : _newTreatmentDuration.value !== _private.treatmentTimeTotal

        onClicked   : {
            _root.continueClicked(_newTreatmentDuration.value)
        }
    }
}
