/*!
 *
 * 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
    }

    Item { id: _infoRow
        objectName: "_infoRow"

        readonly property int cellWidth: _root.width / 3.5
        height: _timeElapsed.height

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

        LabelUnitText { id: _timeElapsed
            objectName  : "_timeElapsed"
            width       : _infoRow.cellWidth
            label       : qsTr("Treatment Time Elapsed:")
            anchors.horizontalCenter: parent.horizontalCenter

            TimeText { id: _timeText
                objectName: "timeText"

                anchors {
                    right               : parent.right
                    rightMargin         : Variables.defaultMargin * 2
                    verticalCenter      : parent.verticalCenter
                    verticalCenterOffset: -2
                }
                seconds             : vTreatmentTime.time_Elapsed
                textWeight          : Font.Normal
                textPixelSize       : Fonts.fontPixelContainerTitle
                secondsLeftMargin   : 5
            }
        }
    }

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