/*!
 *
 * 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    TreatmentUltrafiltrationItem.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      27-Apr-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  01-Mar-2021
 *
 */

// Qt
import QtQuick 2.12
import QtQuick.Controls 2.12 // StackView

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"

/*!
 * \brief   the treatment ultrafiltration flow manager screen
 */
TreatmentAdjustmentBase { id: _root
    objectName: "_TreatmentAdjustmentUltrafiltration"

    // ultrafiltration state information bar properties
    readonly property bool   isUFPaused: vTreatmentUltrafiltration.ufPaused

    closeVisible        : true
    confirmVisible      : false
    backVisible         : _ufStack.stackView.depth > 1

    onAboutToShow       : { _ufStack.reset() }
    onClosed            : { _ufStack.reset() }
    onBackClicked       : { _ufStack.pop()   }

    information {
        visible         : true && information.text && ! notification.visible
        imageSource     : isUFPaused ?  "qrc:/images/iPauseOrange"      : ""
        text            : isUFPaused ?  qsTr("Ultrafiltration Paused")  : ""
        color           : Colors.ufNotificationBarBg
        textColor       : Colors.ufVolumeGoalText
        textfontSize    : Fonts.fontPixelUltrafiltrationAdjustmentNotification
        textfontWeight  : Font.Medium
    }

    StackItem { id: _ufStack
        objectName  : "_ufStack"
        anchors {
            top: parent.top
            bottom: information.top
            left: parent.left
            right: parent.right
        }
        visible     : true
        stackView {
            initialItem : _treatmentAdjustmentUltrafiltrationStart
            popEnter    : null
            popExit     : null
            pushEnter   : null
            pushExit    : null
        }

        TreatmentAdjustmentUltrafiltrationStart { id: _treatmentAdjustmentUltrafiltrationStart
            objectName  : "_treatmentAdjustmentUltrafiltrationStart"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Ultrafiltration Volume ") + Variables.unitTextUltrafiltrationVolume
                _root.notificationText  = ""
            }
            onPauseClicked          : {
                // send pause to TD and wait.
                // if accepted button changes, otherwise show error in notification
                vTreatmentAdjustmentUltrafiltrationState.doPause()
            }
            onEditClicked           : {
                _treatmentAdjustmentUltrafiltrationEdit.reset() // reset the slider to minimum value position
                _ufStack.page(_treatmentAdjustmentUltrafiltrationEdit)
            }
            onIsolatedUfClicked     : {
                _treatmentAdjustmentIsolatedUFDurationEdit.reset()
                _ufStack.page(_treatmentAdjustmentIsolatedUFDurationEdit)
            }
            onResumeClicked         : {
                // send resume to TD and wait.
                // if accepted button changes, otherwise show error in notification
                vTreatmentAdjustmentUltrafiltrationState.doResume()
            }
        }

        TreatmentAdjustmentUltrafiltrationEdit { id: _treatmentAdjustmentUltrafiltrationEdit
            objectName  : "_treatmentAdjustmentUltrafiltrationEdit"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Ultrafiltration Volume ") + Variables.unitTextUltrafiltrationVolume
                _root.notificationText  = ""
            }
            onContinueClicked       : function(vVolume) {
                // send Volume to TD and wait.
                // if accepted moves to confirm screen, otherwise show error in notification
                vTreatmentAdjustmentUltrafiltrationEdit.doAdjustment(vVolume)
            }
        }

        TreatmentAdjustmentUltrafiltrationConfirm { id: _treatmentAdjustmentUltrafiltrationConfirm
            objectName  : "_treatmentAdjustmentUltrafiltrationConfirm"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Confirm Ultrafiltration Volume ") + Variables.unitTextUltrafiltrationVolume
                _root.notificationText  = ""
            }
            onConfirmVolumeClicked  : {
                vTreatmentAdjustmentUltrafiltrationConfirm.doConfirm(vVolume)
            }
        }

        TreatmentAdjustmentIsolatedUFDurationEdit { id: _treatmentAdjustmentIsolatedUFDurationEdit
            objectName  : "_treatmentAdjustmentIsolatedUFDurationEdit"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Isolated Ultrafiltration Volume ") + Variables.unitTextUltrafiltrationVolume
                _root.notificationText  = ""
            }

            onContinueClicked       : function(vDuration) {
                // send Volume to TD and wait.
                // if accepted moves to confirm screen, otherwise show error in notification
                vTreatmentAdjustmentIsolatedUFDurationEdit.doAdjustment(vDuration)
            }
        }

        TreatmentAdjustmentIsolatedUFVolumeEdit { id: _treatmentAdjustmentIsolatedUFVolumeEdit
            objectName          : "_treatmentAdjustmentIsolatedUFVolumeEdit"
            visible             : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Isolated Ultrafiltration Volume ") + Variables.unitTextUltrafiltrationVolume
                _root.notificationText  = ""
            }
            onContinueClicked       : function(vDuration, vVolume) {
                // send Volume to TD and wait.
                // if accepted moves to confirm screen, otherwise show error in notification
                vTreatmentAdjustmentIsolatedUFVolumeEdit.doAdjustment(vDuration, vVolume)
            }
        }

        TreatmentAdjustmentIsolatedUFConfirm { id: _treatmentAdjustmentIsolatedUFConfirm
            objectName  : "_treatmentAdjustmentIsolatedUFConfirm"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Confirm Isolated Ultrafiltration Values")
                _root.notificationText  = ""
            }
            onConfirmVolumeClicked  : {
                vTreatmentAdjustmentIsolatedUFConfirm.doConfirm()
            }
        }

        Connections { target: vTreatmentAdjustmentUltrafiltrationState
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentUltrafiltrationState.adjustment_Accepted) {
                    notification.text = ""
                }
                else {
                    notification.text = vTreatmentAdjustmentUltrafiltrationState.adjustment_ReasonText
                }
            }
        }

        Connections { target: vTreatmentAdjustmentUltrafiltrationEdit
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentUltrafiltrationEdit.adjustment_Accepted) {
                    notification.text = ""

                    _treatmentAdjustmentUltrafiltrationConfirm.ultrafiltrationVolume    = vTreatmentAdjustmentUltrafiltrationEdit.volume
                    _treatmentAdjustmentUltrafiltrationConfirm.ultrafiltrationRate      = vTreatmentAdjustmentUltrafiltrationEdit.rate

                    _ufStack.page(_treatmentAdjustmentUltrafiltrationConfirm)
                }
                else {
                    notification.text = vTreatmentAdjustmentUltrafiltrationEdit.adjustment_ReasonText
                }
            }
        }

        Connections { target: vTreatmentAdjustmentUltrafiltrationConfirm
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentUltrafiltrationConfirm.adjustment_Accepted) {
                    notification.text = ""
                    close()
                }
                else {
                    notification.text = vTreatmentAdjustmentUltrafiltrationConfirm.adjustment_ReasonText
                }
            }
        }

        Connections { target: vTreatmentAdjustmentIsolatedUFDurationEdit
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentIsolatedUFDurationEdit.adjustment_Accepted) {
                    notification.text = ""

                    _treatmentAdjustmentIsolatedUFVolumeEdit.duration   = _treatmentAdjustmentIsolatedUFDurationEdit.duration
                    _treatmentAdjustmentIsolatedUFVolumeEdit.volumeMax  = vTreatmentAdjustmentIsolatedUFDurationEdit.volumeMax

                    _treatmentAdjustmentIsolatedUFVolumeEdit.reset()
                    _ufStack.page(_treatmentAdjustmentIsolatedUFVolumeEdit)
                }
                else {
                    notification.text = vTreatmentAdjustmentIsolatedUFDurationEdit.text()
                }
            }
        }

        Connections { target: vTreatmentAdjustmentIsolatedUFVolumeEdit
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentIsolatedUFVolumeEdit.adjustment_Accepted) {
                    notification.text = ""

                    _treatmentAdjustmentIsolatedUFConfirm.volume    = vTreatmentAdjustmentIsolatedUFVolumeEdit.volume
                    _treatmentAdjustmentIsolatedUFConfirm.duration  = vTreatmentAdjustmentIsolatedUFVolumeEdit.duration
                    _treatmentAdjustmentIsolatedUFConfirm.rate      = vTreatmentAdjustmentIsolatedUFVolumeEdit.rate

                    _ufStack.page(_treatmentAdjustmentIsolatedUFConfirm)
                }
                else {
                    notification.text = vTreatmentAdjustmentIsolatedUFVolumeEdit.text()
                }
            }
        }

        Connections { target: vTreatmentAdjustmentIsolatedUFConfirm
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentIsolatedUFConfirm.adjustment_Accepted) {
                    notification.text = ""
                    close()
                }
                else {
                    notification.text = vTreatmentAdjustmentIsolatedUFConfirm.text()
                }
            }
        }
    }
}
