/*!
 *
 * 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    BPHREntry.qml
 * \author  (last)      Vy
 * \date    (last)      02-Jun-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  21-Jun-2021
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   The Check list view is a dynamic check list items which gets list of strings as model
 * \details and dynamically creates list of items which can be later set as started/done to have
 *          bussy circle or check mark as done.
 */
 Item { id: _root
    property alias systolic         : _bloodPressureSystolic.text
    property alias diastolic        : _bloodPressureDiastolic.text
    property alias heartRate        : _heartRate.text

    readonly property int contentPadding    : Variables.defaultMargin * 7
    readonly property bool isValid          :
        _bloodPressureSystolic  .isValid
    &&  _bloodPressureDiastolic .isValid
    &&  _heartRate              .isValid
    &&  _private.isBloodPressureCompareValid

    width       : _container.width
    height      : _container.height

    function clear() {
        systolic = ""
        diastolic = ""
        heartRate = ""
    }

    function setInitialFocus() {
        numPad.setup(_bloodPressureSystolic,
                     qsTr("Systolic"),
                     vTreatmentVitals.systolicMin,
                     vTreatmentVitals.systolicMax,
                     Variables.unitTextBloodPressure)
    }

    QtObject { id: _private
        property bool isBloodPressureCompareValid : (parseInt(_bloodPressureDiastolic.text) <= parseInt(_bloodPressureSystolic.text))
    }

    Column { id: _container
        spacing         : 15
        leftPadding     : _root.contentPadding
        anchors.fill    : parent

        Item { id: _vitalsDataItem
            width       : Variables.adjustmentLabelUnitContainerWidth
            height      : Variables.adjustmentLabelUnitContainerHeight

            Image { id: _image
                anchors {
                    left        : parent.left
                    top         : parent.top
                    topMargin   : Variables.defaultMargin / 2
                }
                sourceSize.width    : Variables.notificationBarIconHeight
                sourceSize.height   : Variables.notificationBarIconHeight
                source              : "qrc:/images/iVitalsRed"
            }

            LabelValue { id: _bloodPressure
                anchors {
                    left            : _image.right
                    leftMargin      : Variables.defaultMargin
                    top             : parent.top
                }
                height      : parent.height / 3
                width       : parent.width / 6
                topTextFont.pixelSize   : Fonts.fontHeaderbarTitle
                topTextFont.weight      : Font.Bold
                topText     : Variables.notSetVariable(vTreatmentVitals.systolic, 2) + "/" + Variables.notSetVariable(vTreatmentVitals.diastolic, 2)
                bottomText  : Variables.unitTextBloodPressure
                bottomTextFont.pixelSize: Fonts.fontPixelContainerUnitSmall
                bottomTextFont.weight   : Font.Light
            }

            LabelValue { id: _heartBeat
                anchors {
                    left            : _bloodPressure.right
                    top             : parent.top
                }
                height      : parent.height / 3
                width       : parent.width / 6
                topTextFont.pixelSize   : Fonts.fontHeaderbarTitle
                topTextFont.weight      : Font.Bold
                topText     : Variables.notSetVariable(vTreatmentVitals.heartRate, 2)
                bottomText  : Variables.unitTextHeartBeat
                bottomTextFont.pixelSize: Fonts.fontPixelContainerUnitSmall
                bottomTextFont.weight   : Font.Light
            }

            Item {
                anchors {
                    right           : parent.right
                    verticalCenter  : parent.verticalCenter
                }
                height  : parent.height
                width   : parent.width / 3

                Row {id: _intervalRow
                    anchors {
                        bottom      : _lastRecordedRow.top
                        bottomMargin: Variables.defaultMargin / 2
                        left        : _lastRecordedRow.left
                    }
                    spacing: 5

                    Text { id: _interval
                        font.pixelSize      : Fonts.fontPixelTouchAreaTitle
                        font.weight         : Font.Medium
                        font.italic         : true
                        verticalAlignment   : Text.AlignBottom
                        color               : Colors.offWhite
                        text                : qsTr("Vitals Interval:")
                    }

                    Text { id: _intervalText
                        font.pixelSize      : Fonts.fontPixelTouchAreaTitle
                        font.weight         : Font.Normal
                        font.italic         : true
                        verticalAlignment   : Text.AlignBottom
                        color               : Colors.offWhite
                        text                : vTreatmentVitals.interval ? (vTreatmentVitals.interval + Variables.unitTextMins) : qsTr("OFF")
                    }
                }

                Row { id: _lastRecordedRow
                    anchors
                    {
                        bottom      : parent.bottom
                        bottomMargin: Variables.defaultMargin / 2
                        left        : parent.left
                    }
                    spacing: 5

                    Text { id: _lastRecorded

                        font.pixelSize      : Fonts.fontPixelTouchAreaTitle
                        font.weight         : Font.Medium
                        font.italic         : true
                        verticalAlignment   : Text.AlignBottom
                        color               : Colors.offWhite
                        text                : qsTr("Last Read:")
                    }

                    Text { id: _lastRecordedText
                        font.pixelSize      : Fonts.fontPixelTouchAreaTitle
                        font.weight         : Font.Normal
                        font.italic         : true
                        verticalAlignment   : Text.AlignBottom
                        color               : Colors.offWhite
                        text                : vTreatmentVitals.lastRead === "" ? Variables.emptyEntry :
                                                                                 vTreatmentVitals.lastRead
                    }
                }
            }
        }

        LabelUnitContainer { id: _bpContainer
            text        : qsTr("Blood Pressure")
            unitText    : Variables.unitTextBloodPressure
            width       : Variables.adjustmentLabelUnitContainerWidth
            height      : Variables.adjustmentLabelUnitContainerHeight

            contentItem : Row {
                anchors.centerIn: parent

                TextEntry { id  : _bloodPressureSystolic
                    text                    :  Variables.notSetVariable(vTreatmentVitals.systolic, 0)
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                    line.visible            : false
                    validator               : IntValidator { bottom: vTreatmentVitals.systolicMin;  top : vTreatmentVitals.systolicMax }
                    nextInput               : _bloodPressureDiastolic
                    textInput.color         : (textInput.acceptableInput && _private.isBloodPressureCompareValid) ? Colors.textMain : Colors.red
                    useQtNumPad             : false
                    showPlaceHolderText     : true

                    onClicked: numPad.open ( _bloodPressureSystolic,
                                                qsTr("Systolic"),
                                                vTreatmentVitals.systolicMin,
                                                vTreatmentVitals.systolicMax,
                                                Variables.unitTextBloodPressure)
                }
                Label {
                    text    : "/"
                    width   : 10
                }
                TextEntry { id  : _bloodPressureDiastolic
                    text                    :  Variables.notSetVariable(vTreatmentVitals.diastolic, 0)
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                    line.visible            : false
                    validator               : IntValidator { bottom: vTreatmentVitals.diastolicMin;  top : vTreatmentVitals.diastolicMax }
                    nextInput               : _heartRate
                    textInput.color         : (textInput.acceptableInput && _private.isBloodPressureCompareValid) ? Colors.textMain : Colors.red
                    useQtNumPad             : false
                    showPlaceHolderText     : true

                    onClicked : numPad.open ( _bloodPressureDiastolic,
                                               qsTr("Diastolic"),
                                                vTreatmentVitals.diastolicMin,
                                                _bloodPressureSystolic.textInput.acceptableInput &&
                                                _bloodPressureSystolic.text < vTreatmentVitals.diastolicMax ? _bloodPressureSystolic.text :
                                                                                                           vTreatmentVitals.diastolicMax,
                                             Variables.unitTextBloodPressure)
                }
            }
        }

        LabelUnitContainer { id: _heartRateContainer
            text        : qsTr("Heart Rate")
            unitText    : Variables.unitTextHeartBeat
            width       : Variables.adjustmentLabelUnitContainerWidth
            height      : Variables.adjustmentLabelUnitContainerHeight

            contentItem : TextEntry { id: _heartRate
                text                        :  Variables.notSetVariable(vTreatmentVitals.heartRate, 0)
                anchors.centerIn            : parent
                textInput.font.pixelSize    : Fonts.fontPixelValueControl
                line.visible                : false
                validator                   : IntValidator { bottom: vTreatmentVitals.heartRateMin;  top : vTreatmentVitals.heartRateMax }
                nextInput                   : _bloodPressureSystolic
                useQtNumPad                 : false
                showPlaceHolderText         : true

                onClicked   : numPad.open ( _heartRate,
                                           qsTr("Heart Rate"),
                                            vTreatmentVitals.heartRateMin,
                                            vTreatmentVitals.heartRateMax,
                                            Variables.unitTextHeartBeat)
            }
        }
    }
}
