/*!
 *
 * 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    TreatmentSaline.qml
 * \author  (last)      Vy
 * \date    (last)      18-May-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  10-Aug-2020
 *
 */

// Qt
import QtQuick 2.12

// Project
import VTreatmentSaline 0.1;

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/treatment"

/*!
 * \brief   Treatment Screen Saline Management section
 */
TreatmentSection { id: _root
    readonly property bool isStarted            : vTDTreatmentStates.sbRunning
    readonly property bool isIdle               : vTDTreatmentStates.sbIdle
    // other that max saline bolus delivered,
    // if saline started but still is waiting for pump the button is disabled so user won't tap multiple times.
    readonly property bool isWaitPump           : vTDTreatmentStates.sbWaitPump
    readonly property string unit               : Variables.unitTextFluid

    property int valueDecimal   : Variables.salinePrecision
    property int valueTarget    : vTreatmentSaline.target || vTreatmentAdjustmentSaline.target
    property int valueCumulative: vTreatmentSaline.cumulative
    property int valueDelivered : vTreatmentSaline.delivered
    property string buttonText  :   isIdle      ?   _private.textStart :
                                    isStarted   ?   _private.textStop  :
                                                    _root.buttonText

    header.title    : qsTr("Fluid Delivery" )

    QtObject { id: _private
        readonly property string textStart          : qsTr("Start Bolus") + "<b>" + textVolumeTarget + "<\b>"
        readonly property string textStop           : qsTr("Stop Bolus" ) + "<b>" + textVolumeTarget + "<\b>"
        readonly property string textVolumeTarget   : " (" + valueTarget + unit + ")"
    }

    contentItem: Column { id :_content
        spacing             : Variables.defaultMargin * 3

        Text { id: _descriptionText
            objectName: "salineDescriptionText"
            text            : isStarted ? ("<i>%1</i>").arg(qsTr("Delivering Fluid")) : qsTr("Total Fluid Delivered")
            height          : Variables.contentHeight
            font.pixelSize  : Fonts.fontPixelFluidText
            font.weight     : Font.Normal
            color           : isStarted ? Colors.progressBarSaline : Colors.fluidText
            horizontalAlignment: Text.AlignHCenter
            leftPadding     : Variables.defaultMargin
        }

        Item { id: _progressBarArea
            height      : 30
            width       : contentItem.width

            ProgressBar { id: _fluidProgressBar
                width           : _progressBarArea.width
                height          : Variables.progressbarFluidHeight * 3
                minText.visible : false
                maxText.visible : false
                marker.visible  : false
                color           : Colors.progressBarSaline
                maximum         : valueTarget
                value           : isStarted ? valueDelivered : 0
                decimal         : _root.valueDecimal
                radius          : 9
            }

            Label { id: _valueDelivered
                property real value: isStarted ? valueDelivered : valueCumulative

                anchors {
                    verticalCenter  : _valueDeliveredUnit.verticalCenter
                    right           : _valueDeliveredUnit.left
                    rightMargin     : 5
                }
                font.pixelSize  : 18
                font.weight     : Font.DemiBold
                text            : value
                width           : contentWidth
            }

            Label { id: _valueDeliveredUnit
                property real value: 0
                anchors {
                    bottom      : _fluidProgressBar.top
                    bottomMargin: Variables.defaultMargin
                    right       : _fluidProgressBar.right
                }
                font.pixelSize  : 18
                text            : _root.unit
                width           : contentWidth
            }
        }

        TouchRect { id: _startFluidButton
            width           : Variables.defaultButtonWidth
            height          : Variables.defaultButtonHeight
            radius          : 9
            text.textFormat : Text.RichText
            text.text       : buttonText
            isDefault       : true
            enabled         : isStarted || isIdle

            text.font {
                weight      : Font.DemiBold
                pixelSize   : 20
            }
            onPressed : {
                if ( isIdle    ) vTreatmentAdjustmentSaline.doStart() // IDLE
                if ( isStarted ) vTreatmentAdjustmentSaline.doStop () // IN_PROGRESS
            }
        }
    }
}
