/*!
 *
 * 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    TreatmentBloodPrime.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      13-Sep-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-Apr-2021
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   the in-treatment Blood Prime screen
 */
ScreenItem { id: _root
    objectName: "_TreatmentBloodPrime"

    property alias notification: _notification

    signal incrementRate  (real newValue)
    signal decrementRate  (real newValue)
    signal pause          ()
    signal resume         ()

    Connections { target: vTreatmentAdjustmentBloodFlowRate
        function onAdjustmentTriggered              ( vValue ) {
            if (vTreatmentAdjustmentBloodFlowRate.adjustment_Accepted) {
                _notification.text = ""
            }
            else {
                _notification.text = vTreatmentAdjustmentBloodFlowRate.text()
            }
        }
    }

    Column { id: _contentColumn
        objectName  : "_contentColumn"
        anchors {
            top                 : parent.top
            topMargin           : Variables.defaultMargin
            horizontalCenter    : parent.horizontalCenter
        }
        spacing     : Variables.defaultMargin * 2

        ProgressCircle { id: _volumeProgress
            objectName                  : "_volumeProgress"
            anchors.horizontalCenter    : parent.horizontalCenter
            thickness                   : 35
            circleShadowColor           : Colors.mainTreatmentLighterBlue
            runAnimation                : vTDTreatmentStates.bpPaused
            minimum                     : 0
            maximum                     : vTreatmentBloodPrime.target
            value                       : vTreatmentBloodPrime.current

            Text { id: _volumeText
                objectName      : "_volumeText"
                anchors.centerIn: parent
                height          : contentHeight
                font {
                    pixelSize   : 76
                    weight      : Font.Light
                }
                color           : Colors.textMain
                text            : vTreatmentBloodPrime.current.toFixed(0) + " " + qsTr(Variables.unitTextFluid)
            }

            TimeText { id: _timeout
                objectName      : "_timeout"
                anchors {
                    bottom          : _volumeText.top
                    bottomMargin    : Variables.defaultMargin * 2
                    horizontalCenter: parent.horizontalCenter
                }
                textPixelSize   : 30
                textWeight      : Font.Normal
                secondsVisible  : false
                seconds         : vTreatmentBloodPrime.countdown * 60
                visible         : vTreatmentBloodPrime.timeout
            }

            NotificationBarSmall { id: _pausedNotification
                objectName      : "_pausedNotification"
                anchors {
                    top                 : _volumeText.bottom
                    bottom              : undefined
                    topMargin           : _timeout.anchors.bottomMargin
                }
                visible         : vTDTreatmentStates.bpPaused
                height          : 25
                color           : "transparent"
                imageDiameter   : 25
                imageTopMargin  : 4
                imageSource     : "qrc:/images/iPauseOrange"
                text            : qsTr("Blood Prime Paused")
                textColor       : Colors.mainTreatmentOrange
                textfontSize    : 22
                textfontWeight  : Font.Medium
            }
        }

        Row { id: _bottomContentRow
            objectName                  : "_bottomContentRow"
            anchors.horizontalCenter    : parent.horizontalCenter
            spacing                     : Variables.defaultMargin

            TreatmentSection { id: _flowRateSection
                objectName              : "_flowRateSection"
                anchors.verticalCenter  : parent.verticalCenter
                width                   : 420
                height                  : 420
                header.title            : qsTr("Blood Flow Rate" )
                header.showEdit         : false
                header {
                    title               : qsTr("Rinseback Flow Rate")
                    showEdit            : false
                }

                RangedValue { id: _bloodFlowRangedValue
                    objectName  : "_bloodFlowRangedValue"
                    precision   : Variables.bloodFlowPrecision
                    rawMin      : vTreatmentRanges.bloodFlowRateMin
                    rawMax      : vTreatmentRanges.bloodFlowRateMax
                    rawStep     : vTreatmentRanges.bloodFlowRateRes
                    rawValue    : vTreatmentParametersSetPoint.bloodFlow
                }

                Item { id: _flowRateContentArea
                    objectName          : "_flowRateContentArea"
                    anchors {
                        top             : _flowRateSection.header.bottom
                        bottom          : _pauseResumeButton.top
                        left            : parent.left
                        right           : parent.right
                        margins         : Variables.defaultMargin
                    }

                    Text { id: _flowRateValue
                        objectName          : "_flowRateValue"
                        anchors.centerIn    : parent
                        width               : 200
                        height              : Variables.defaultButtonHeight
                        font {
                            pixelSize   : Fonts.fontPixelVitals
                            weight      : Font.Medium
                        }
                        color               : Colors.textTextRectLabel
                        horizontalAlignment : Text.AlignHCenter
                        verticalAlignment   : Text.AlignVCenter
                        text                : _bloodFlowRangedValue.value
                    }

                    Text { id: _flowRateUnit
                        objectName          : "_flowRateUnit"
                        anchors {
                            top                 : _flowRateValue.bottom
                            topMargin           : Variables.defaultMargin
                            horizontalCenter    : _flowRateValue.horizontalCenter
                        }
                        width               : contentWidth
                        height              : contentHeight
                        font {
                            pixelSize   : Fonts.fontPixelContainerUnit
                            weight      : Font.Thin
                        }
                        color               : Colors.offWhite
                        horizontalAlignment : Text.AlignHCenter
                        verticalAlignment   : Text.AlignVCenter
                        text                : Variables.unitTextFlowRate
                    }

                    ArrowButton { id: _decrementFlowRateButton
                        objectName  : "_decrementFlowRateButton"
                        anchors {
                            left            : parent.left
                            leftMargin      : Variables.defaultMargin * 3
                            verticalCenter  : _flowRateValue.verticalCenter
                        }
                        leftArrow   : true
                        enabled     : _bloodFlowRangedValue.canDecrement

                        onClicked   : { _root.decrementRate(_bloodFlowRangedValue.decrementedValue()) }
                    }

                    ArrowButton { id: _incrementFlowRateButton
                        objectName  : "_incrementFlowRateButton"
                        anchors {
                            right           : parent.right
                            rightMargin     : _decrementFlowRateButton.anchors.leftMargin
                            verticalCenter  : _flowRateValue.verticalCenter
                        }
                        rightArrow  : true
                        enabled     : _bloodFlowRangedValue.canIncrement

                        onClicked   : { _root.incrementRate(_bloodFlowRangedValue.incrementedValue()) }
                    }
                }

                TouchRect { id: _pauseResumeButton
                    objectName: "_pauseResumeButton"
                    anchors {
                        bottom          : parent.bottom
                        horizontalCenter: parent.horizontalCenter
                        margins         : Variables.defaultMargin * 1.5
                    }
                    width       : Variables.defaultButtonWidth
                    height      : Variables.defaultButtonHeight
                    text {
                        text        : vTDTreatmentStates.bpPaused ? qsTr("Resume Blood Prime") : qsTr("Pause Blood Prime")
                        font.weight : Font.Medium
                    }
                    isDefault   : true
                    enabled     : true

                    onClicked   : { vTDTreatmentStates.bpPaused ? _root.resume() : _root.pause() }
                }
            }

            TreatmentSaline { id: _treatmentSaline
                objectName              : "_treatmentSaline"
                anchors.verticalCenter  : parent.verticalCenter
                width                   : 285
                height                  : _flowRateSection.height
                header.showEdit         : false
            }

            TreatmentPressures { id: _treatmentPressures
                objectName              : "_treatmentPressures"
                anchors.verticalCenter  : parent.verticalCenter
                width                   : 750
                height                  : _flowRateSection.height
                header.showEdit         : false
            }
        }
    }

    NotificationBarSmall { id: _notification }
}
