/*!
 *
 * 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    TreatmentUltrafiltration.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      26-Sep-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

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

/*!
 * \brief   Treatment Screen Ultrafiltration section
 */

TreatmentSection { id: _root
    readonly property bool  isIsolatedUf: vTDTreatmentStates.txIsolatedUF

    readonly property real minimum  : vTreatmentRanges.treatmentRanges_Ultrafiltration_Volume_Min
    readonly property real maximum  : isIsolatedUf ? vTreatmentIsolatedUF.setVolume         : vTreatmentUltrafiltration.setVolume
    readonly property real value    : isIsolatedUf ? vTreatmentIsolatedUF.volumeDelivered   : vTreatmentUltrafiltration.volumeRemoved
    readonly property real rate     : isIsolatedUf ? vTreatmentIsolatedUF.rate              : vTreatmentUltrafiltration.targetRate
    readonly property real paused   : isIsolatedUf ? vTDTreatmentStates.isoUfPaused         : vTreatmentUltrafiltration.ufPaused
    readonly property real running  : isIsolatedUf ? vTDTreatmentStates.isoUfRunning        : vTreatmentUltrafiltration.ufRunning

    readonly property int topTextFontSize       : 25
    readonly property int bottomTextFontSize    : 60
    readonly property bool isUfCompleted        : value >= maximum && maximum > 0

    readonly property string statusText         :   isUfCompleted   ? qsTr ("Ultrafiltration Complete")                                                     :
                                                    paused          ? isIsolatedUf ? qsTr ("Isolated UF Paused")        : qsTr ("Ultrafiltration Paused")   :
                                                    running         ? isIsolatedUf ? qsTr ("Isolated UF in Progress")   : ""                                : ""

    readonly property color statusColor         :   isUfCompleted   ? Colors.statusTextComplete             :
                                                    paused          ? Colors.statusPaused                   :
                                                    running         ? isIsolatedUf ? Colors.statusActive    : Colors.transparent : Colors.transparent
    header.showPauseResume          : true
    header.isPaused                 : _root.paused
    header.title                    : qsTr("Ultrafiltration") + " (" + qsTr("UF") + ")"
    contentArea.anchors.topMargin   : Variables.defaultMargin

    onPauseResumeClicked: _root.paused ? vTreatmentAdjustmentUltrafiltrationState.doResume() :
                                         vTreatmentAdjustmentUltrafiltrationState.doPause()

    contentItem: Row { id: _row
        spacing     : Variables.defaultMargin * 6
        rightPadding: Variables.defaultMargin
        leftPadding : Variables.defaultMargin

        Column { id: _leftColumn
            spacing : Variables.defaultMargin * 6
            width   : contentItem.width * 0.2
            height  : _root.height

            LabelValue { id: _ufVolume
                height                  : 40
                width                   : contentItem.width * 0.2

                topText                 : qsTr("UF Volume Goal")
                topTextFont.pixelSize   : topTextFontSize
                topTextFont.weight      : Font.Medium

                bottomText              : _root.maximum.toFixed(Variables.ultrafiltrationPrecision)
                bottomTextFont.pixelSize: bottomTextFontSize
                bottomTextFont.weight   : Font.Normal
                leftAlign               : true
                unitText                : Variables.unitVolume
                showCenterText          : true
                centerText              : _root.isIsolatedUf ? qsTr("Isolated UF") :
                                                               vTreatmentIsolatedUF.volumeDelivered > 0 ? qsTr("Isolated UF + Target UF") :
                                                                                                          ""
            }

            LabelValue { id: _ufRate
                height                  : 40
                width                   : contentItem.width * 0.2

                topText                 : qsTr("UF Rate")
                topTextFont.pixelSize   : topTextFontSize
                topTextFont.weight      : Font.Medium

                bottomText              :  isTreatmentEnd || _root.isUfCompleted ? qsTr("OFF") :
                                                                                   _root.rate.toFixed(Variables.ultrafiltrationPrecision)
                bottomTextFont.pixelSize: bottomTextFontSize
                bottomTextFont.weight   : Font.Normal
                bottomTextColor         : _root.paused ? Colors.statusTextPaused : Colors.textTextRectLabel
                leftAlign               : true
                unitText                : Variables.unitTextRate
                bottomTextTopMargin     : Variables.defaultMargin
            }
        }

        Column { id: _rightColumn
            topPadding  : Variables.defaultMargin
            spacing     : Variables.defaultMargin * 5
            width       : contentItem.width
            height      : _root.height

            Item { id: _statusItem
                width   : _rightColumn.width * 0.6
                height  : 40
                Rectangle { id: _status
                    anchors.verticalCenter  : parent.verticalCenter
                    anchors.right           : parent.right
                    color                   : _root.statusColor
                    width                   : _statusText.contentWidth + Variables.defaultMargin * 2
                    height                  : _statusText.contentHeight + Variables.defaultMargin
                    radius                  : height

                    Text { id: _statusText
                        anchors.centerIn: parent
                        font.pixelSize  : 18
                        font.weight     : Font.DemiBold
                        text            : _root.statusText
                        color           : Colors.offWhite
                    }
                }
            }

            ProgressBar { id: _progressbar
                enabled : ! isSBInProgress

                width   : contentItem.width * 0.6
                height  : 20

                decimal : Variables.ultrafiltrationPrecision
                minimum : _root.minimum
                maximum : _root.maximum
                value   : _root.value
                unitText: Variables.unitVolume
                color   : _root.paused ? Colors.pauseColor : Colors.progressBarUltrafiltration
                radius          : height
                showMarker      : false

                minText.color: "#818181"
                minText.font {
                    pixelSize   : 18
                    weight      : Font.Normal
                }

                maxText.color: "#818181"
                maxText.font {
                    pixelSize   : 18
                    weight      : Font.Normal
                }

                Text { id: _valueText
                    anchors {
                        bottom      : parent.top
                        bottomMargin   : Variables.defaultMargin
                        right       : parent.right
                    }
                    text            : _root.value.toFixed(Variables.ultrafiltrationPrecision) + " " + Variables.unitVolume
                    color           :  _root.paused ? Colors.statusTextPaused : Colors.offWhite
                    font.pixelSize  : 40
                    font.weight     : Font.DemiBold
                }

                Text { id: _volumeRemovedText
                    anchors {
                        top         : parent.bottom
                        topMargin   : Variables.defaultMargin * 2.5
                        right       : parent.right
                    }
                    text            : qsTr("UF Volume Removed")
                    color           : Colors.textTextRectLabel
                    font.pixelSize  : 20
                    font.weight     : Font.Medium
                }
            }
        }
    }
}
