/*!
 *
 * Copyright (c) 2021-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    PreTreatmentUltrafiltration.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      08-Mar-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/pretreatment"

/*!
 * \brief   Pre-Treatment ultrafiltration screen
 * \details which contains a slider to let user set the ultratiltration volume.
 */
PreTreatmentBase { id: _root
    objectName: "_PreTreatmentUltrafiltration"

    readonly property alias  ufVolume    : _volumeGoalAdjuster.value

    // TODO in cleanup create shared UF qml component to use here and TreatmentAdjustmentUltrafiltrationEdit.qml -- this is all mostly same code
    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
        }
    }

    Item { id: _contentArea
        objectName: "_contentArea"
        anchors {
            top         : _root.title.bottom
            topMargin   : Variables.defaultMargin * -1 //with current resolution this makes the screen look nice
            horizontalCenter: parent.horizontalCenter
        }
        width               : Variables.ultrafiltrationProgressBarWidth

        Column { id: _contentColumn
            objectName  : "_contentColumn"
            anchors.fill: parent
            width       : parent.width
            spacing     : 80

            Text { id: _ultrafiltrationTitle
                color                   : Colors.textMain
                font.pixelSize          : Fonts.fontPixelContainerTitle
                text                    : qsTr("Set Ultrafiltration Volume %1").arg(Variables.unitTextVolume)
                anchors.horizontalCenter: parent.horizontalCenter
            }

            ProgressBar { id: _maxVolumeBar
                objectName      : "_maxVolumeBar"
                width           : parent.width
                height          : 20
                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
                    }
                    z       : parent.z + 1
                    color   : Colors.ufProgressBarFill
                    radius  : parent.radius
                }
            }

            LabelUnitValueAdjuster { id: _volumeGoalAdjuster
                objectName          : "_volumeGoalAdjuster"
                width               : parent.width
                height              : Variables.adjustmentLabelUnitContainerHeight
                text                : qsTr("Ultrafiltration")
                unitText            : Variables.unitVolume
                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
                    }
                }
            }

            Line { id: _divider
                thickness               : 2
                length                  : _root.width * .8
                color                   : Colors.checkListLine
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Text { id: _dyalistateTitle
                color                   : Colors.textMain
                font.pixelSize          : Fonts.fontPixelContainerTitle
                text                    : qsTr("Test dialysate sample per your clinic's instructions")
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Column {
                spacing: 10
                width               : parent.width

                LabelUnitContainer { id: _theoriticalCondictivityContainer
                    objectName      : "_theoriticalCondictivityContainer"
                    width               : parent.width
                    height              : Variables.adjustmentLabelUnitContainerHeight
                    text                : qsTr("Theoretical Conductivity")
                    unitText            : Variables.unitTextDialCond
                    contentItem : Text {  id: _theoriticalCondictivityText
                        anchors.centerIn: parent
                        text            : "13.7" // TODO add actual value
                        color           : Colors.offWhite
                        font.pixelSize  : Fonts.fontPixelValueControl
                    }
                }

                LabelUnitContainer { id: _independentConductivityReadingContainer
                    objectName      : "_independentConductivityReadingContainer"
                    width               : parent.width
                    height              : Variables.adjustmentLabelUnitContainerHeight
                    text                : qsTr("Independent Conductivity Reading")
                    unitText            : Variables.unitTextDialCond
                    contentItem : Text {  id: _independentConductivityReadingCText
                        anchors.centerIn: parent
                        text            : "13.7" // TODO add actual value
                        color           : Colors.offWhite
                        font.pixelSize  : Fonts.fontPixelValueControl
                    }
                }
            }
        }
    }
}
