/*!
 *
 * 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    TreatmentHome.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      10-Jan-2024
 * \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:/dialogs"
import "qrc:/pages/treatment/sections"

/*!
 * \brief   ManagerHome is the screen
 * which is the default screen in the "Manager" stack
 */
ScreenItem { id: _root
    property int          bloodFlow_MeasuredFlow_Precision: 0
    property int dialysateInletFlow_MeasuredFlow_Precision: 0

    signal backClicked()

    signal sectionFlowClicked()
    signal sectionVitalsClicked()
    signal sectionPressuresClicked()
    signal sectionSalineClicked()
    signal sectionTimeClicked()
    signal sectionUltrafiltrationClicked()

    readonly property int cellWidth : Variables.treatmentCellWidth
    readonly property int cellHeight: Variables.treatmentCellHeight
    Column { id: _column
        objectName:    "column"

        spacing: Variables.defaultMargin

        anchors {
            fill: parent
            margins: Variables.defaultMargin
            topMargin : 30
        }

        Row { id: _topRow
            objectName:    "topRow"

            spacing: Variables.defaultMargin
            height: cellHeight

            TreatmentTime { id: _treatmentTime
                objectName  : "treatmentTime"
                width       : (cellWidth * 2) - (Variables.defaultMargin * 2)
                height      : cellHeight
                onClicked   : sectionTimeClicked()
            }

            TreatmentSaline { id: _treatmentSaline
                objectName  : "treatmentSaline"
                width       : cellWidth + Variables.defaultMargin
                height      : cellHeight
                onEditClicked: sectionSalineClicked()
            }

            TreatmentVitals { id: _treatmentVitals
                objectName      : "treatmentVitals"
                width           : cellWidth + Variables.defaultMargin
                height          : cellHeight
                onEditClicked   : sectionVitalsClicked()
            }

            TreatmentPressures { id: _treatmentPressures
                objectName      : "treatmentPressures"
                width           : (cellWidth * 3) - (Variables.defaultMargin * 2) // without auto layouts needs to fine tune
                height          : cellHeight
                onEditClicked   : sectionPressuresClicked()
            }
        }

        Row { id: _bottomRow
            objectName  : "bottomRow"
            spacing     : Variables.defaultMargin
            height      : cellHeight

            TreatmentUltrafiltration { id: _treatmentUltrafiltration;
                objectName      : "treatmentUltrafiltration"
                width           : cellWidth * 3
                height          : cellHeight
                header.setEditActive: ! isTreatmentEnd  // LEAHI-PRS-376

                onEditClicked   : sectionUltrafiltrationClicked()
            }

            TreatmentFlows { id: _treatmentFlows
                objectName          : "treatmentFlows"
                width               : cellWidth * 4
                height              : cellHeight
                header.setEditActive: ! isTreatmentEnd  // LEAHI-PRS-376
                editEnabled         : ! _lockDialog.isLocked
                onEditClicked   : sectionFlowClicked()
                onLockClicked   :  {
                    _lockDialog.isLocked =  editEnabled
                    _lockDialog.showDialog (
                                editEnabled ? qsTr("Treatment Parameters Unlocked") :
                                              qsTr("Treatment Parameters Locked"),
                                750
                                )
                }
            }
        }
    }

    NotificationBarSmall { id: _notification
        objectName          : "_notification"
        anchors.bottom      : parent.bottom
        height              : 60
        visible             : ! _alarmBar.visible && text

        CloseButton { id: _dismissButton
            objectName: "_dismissButton"
            anchors {
                top     : parent.top
                bottom  : parent.bottom
                right   : parent.right
                margins : 20
            }
            width       : height
            visible     : true
            onClicked   : { _notification.text = "" }
        }
    }

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

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

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

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

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

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