/*!
 *
 * Copyright (c) 2020-2025 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    TreatmentAdjustmentVitals.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      28-Aug-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  28-Aug-2025
 *
 */

// Qt
import QtQuick 2.12

// Project
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"

TreatmentAdjustmentBase { id: _root
    contentItem.objectName: "TreatmentAdjustmentVitals"
    titleText           : qsTr("Vitals")
    titlePixelSize      : Fonts.fontPixelSection
    height              : Variables.smallDialogHeight
    width               : Variables.smallDialogWidth
    x                   : numPad.isOpened ? Math.round((Variables.applicationWidth - width) / 2) - 200 :
                                            Math.round((Variables.applicationWidth - width) / 2)
    notification.textfontSize   : 20
    notification.visible        : vTreatmentVitals.lastRead || vTreatmentAdjustmentVitals.adjustment_ReasonText
    notification.anchors.rightMargin: numPad.isOpened ? 10 : 0 // fix margins when numpad is open and notifiction is displayed

    backgroundItemZ             :  numPad.isOpened ? 0 : -1
    backgroundItem  : [ NumPad  { id: _numPad
        x       : _root.width - _numPad.width
        y       : 0
        onCancel: _numPad.hide()
    } ]

    property alias  numPad      : _numPad

    readonly property string intervalTextString     :  qsTr("Interval:" )
                                                     + " " + (vTreatmentVitals.interval ? (vTreatmentVitals.interval + qsTr("min")) : qsTr("OFF"))
                                                     + " , "
                                                     + qsTr("Last Read:")
                                                     + " " +  vTreatmentVitals.lastRead
    readonly property string rejectionTextString    : qsTr("Rejected Reason: %1").arg(vTreatmentAdjustmentVitals.adjustment_ReasonText)
    readonly property string notificationTextString : vTreatmentAdjustmentVitals.adjustment_ReasonText ?    rejectionTextString :
                                                                                                            intervalTextString

    function update(vSystolic, vDiastolic ,vHeartRate) {
        _bphrEntry.systolic   = vSystolic   ? vSystolic  : ""
        _bphrEntry.diastolic  = vDiastolic  ? vDiastolic : ""
        _bphrEntry.heartRate  = vHeartRate  ? vHeartRate : ""
    }

    function confirm() {
        numPad.hide()
        _root.close()
        vTreatmentVitals.doConfirm(
                    _bphrEntry.systolic   ,
                    _bphrEntry.diastolic  ,
                    _bphrEntry.heartRate
                    )
        _headerBar.vitalsRecorded()
    }

    onOpened: {
        notificationText = notificationTextString
        vTreatmentVitals.doTimerStop()
    }

    onClosed: {
        vTreatmentVitals.doTimerStart()
    }

    onCloseClicked: {
        numPad.hide()
        numPad.visible = false
        vTreatmentVitals.doSkip()
    }

    BPHREntry { id: _bphrEntry 
        anchors {
            fill: parent
            topMargin: Variables.defaultMargin * 2
        }
    }

    Column { id: _buttonRow
        spacing: Variables.defaultMargin * 1.5

        anchors {
            bottom          : _root.contentItem.bottom
            bottomMargin    : Variables.defaultMargin * 5
            horizontalCenter: _root.contentItem.horizontalCenter
        }

        ConfirmButton { id : _confirmButton
            width       : Variables.defaultButtonWidth
            height      : Variables.defaultButtonHeight
            enabled     : _bphrEntry.isValid

            anchors {
                top             : undefined
                right           : undefined
                horizontalCenter: parent.horizontalCenter
                margins         : 0

            }
            onClicked   : _root.confirm()
        }

        Item { id: _separator
            width                   : Variables.adjustmentLabelUnitContainerWidth
            height                  : 40
            anchors.horizontalCenter: parent.horizontalCenter

            Rectangle { id: _leftLine
                anchors{
                    verticalCenter  : parent.verticalCenter
                    left            : parent.left
                    right           : _text.left
                }
                height  : 1
                color   : Colors.panelBorderColor
            }

            Text { id: _text
                anchors.centerIn: parent
                text                : qsTr("OR")
                width               : 100
                horizontalAlignment : Text.AlignHCenter
                color               : Colors.offWhite
                font.pixelSize      : 25
            }

            Rectangle { id: _rightLine
                anchors{
                    verticalCenter  : parent.verticalCenter
                    left            : _text.right
                    right           : parent.right
                }
                height  : 1
                color   : Colors.panelBorderColor
            }
        }

        TouchRect { id : _measureVitalsButton
            width                   : Variables.defaultButtonWidth
            height                  : Variables.defaultButtonHeight
            isDefault               : true
            anchors.horizontalCenter: parent.horizontalCenter

            text    {
                text            : qsTr("Measure Vitals")
                font.pixelSize  : Fonts.fontPixelConfirm
                font.weight     : Font.Medium
            }

            onClicked:  vTreatmentAdjustmentVitals.doRequest()
        }

        Behavior on anchors.bottomMargin { NumberAnimation { duration: 300} }
    }

    Connections { target: vTreatmentVitals
        // On Timeout - Vitals Request
        function onDidTimeout () {
            if ( ! vTreatmentVitals.interval ) {
                // TODO: interval set to OFF but 15 min default timer still running
                return
            }

            // On Timeout request vitas if BP cuff is enabled or shjow popup for manual entry
            if ( vTreatmentVitals.enableBPCuff ) {
                vTreatmentAdjustmentVitals.doRequest()
                vTreatmentVitals.doTimerStart()
            }
            else {
                _root.open ()
                _bphrEntry.systolic  = ""
                _bphrEntry.diastolic = ""
                _bphrEntry.heartRate = ""
            }
        }

        // Vitals Reading Ready
        function onAdjustmentTriggered  ( vValue ) {
            _root.update  ( vTreatmentVitals.systolic__rt, vTreatmentVitals.diastolic_rt, vTreatmentVitals.heartRate_rt )
            _root.confirm()
        }
    }

    Connections { target: vTreatmentAdjustmentVitals
        // Vitals Response
        function onAdjustmentTriggered  ( vValue ) {
            // on rejected prompt user to enter vitals manually
            if ( ! vTreatmentAdjustmentVitals.adjustment_Accepted ) {
                _root.open ()
                _bphrEntry.systolic  = ""
                _bphrEntry.diastolic = ""
                _bphrEntry.heartRate = ""
            }
            notificationText = notificationTextString
        }

        // FW no response timeout
        function onDidTimeout ( ) { vTreatmentVitals.doSkip(); }
    }

    Behavior on x               { NumberAnimation { duration: Variables.keybardAnimationDuration } }
    Behavior on backgroundItemZ { NumberAnimation { duration: Variables.keybardAnimationDuration } }
}
