/*!
 *
 * 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 isDisabled           : vTDTreatmentStates.sbMaxReached
    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  :    ( isDisabled || isWaitPump )   ?   _private.textStart :
                                        isStarted                   ?   _private.textStop  :
                                                                        _private.textStart

    header.title    : qsTr("Saline" )
    header.showEdit : false

    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 + ")"
        readonly property string mesgDisabled       : qsTr("Maximum cumulative saline bolus volume delivered")
        readonly property string mesgWaitForPump    : "" // qsTr("Saline Bolus is starting") // Not sure if we shouls show any message at this point
        readonly property string mesgRejectReason   : vTreatmentAdjustmentSaline.adjustment_ReasonText
    }

    VTreatmentSaline  { id: vTreatmentSaline }

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

        Text { id: _descriptionText
            text            : qsTr("Total Saline Delivered")
            height          : Variables.contentHeight
            font.pixelSize  : Fonts.fontPixelFluidText
            color           : Colors.fluidText
            horizontalAlignment: Text.AlignHCenter
        }

        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         : valueCumulative
                value           : valueDelivered
                decimal         : _root.valueDecimal
                radius          : 9
            }

            Label { id: _valueDelivered
                property real value: valueDelivered

                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           : _fluidProgressBar.width
            height          : Variables.contentHeight + 10
            radius          : 9
            text.textFormat : Text.RichText
            text.text       : buttonText
            isDefault       : true

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

            // TODO:
//        notification {
//            visible : true
//            text    : isDisabled ? _private.mesgDisabled    :
//                      isWaitPump ? _private.mesgWaitForPump :
//                      _private.mesgRejectReason
//        }
//    }
}
