/*!
 *
 * Copyright (c) 2021-2023 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)      12-May-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 int labelWidth         : 200
    property int unitWidth          : 100
    property int entryWidth         : 100
    property int titleIndent        : 25
    property int topMarginContent   : 200
    property int contentRectHeight  : 0

    property alias firstInput       : _bloodPressureSystolic

    property alias systolic         : _bloodPressureSystolic.text
    property alias diastolic        : _bloodPressureDiastolic.text
    property alias heartRate        : _heartRate.text

    readonly property bool isValid  :
        _bloodPressureSystolic  .isValid
    &&  _bloodPressureDiastolic .isValid
    &&  _heartRate              .isValid
    &&  _private.isBloodPressureCompareValid

    signal clicked()

    width   : _container.width
    height  : _container.height

    function setFocus(vShowKeyboard) {
        if(vShowKeyboard) {
            firstInput.textInput.forceActiveFocus()
            _keyboard.setVisible(true)
        }
    }

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

    anchors.horizontalCenter: parent.horizontalCenter
    y : Qt.inputMethod.visible && _keyboard.visible ? _root.topMarginContent : ( ( _root.contentRectHeight - _container.height ) / 2 )
    Behavior on y { NumberAnimation { duration: Variables.keybardAnimationDuration } }

    Row { id: _container
        spacing: 50
        anchors.centerIn: parent
        Column { spacing         : 25
            leftPadding     :  _root.titleIndent
            Row { spacing     : 10
                TextEntry { id  : _bloodPressureSystolic
                    text        :  Variables.notSetVariable(vTreatmentVitals.systolic, 0)
                    label.text  :  qsTr("Blood Pressure")
                    label.width : _root.labelWidth
                    validator   : IntValidator { bottom: vTreatmentVitals.systolicMin;  top : vTreatmentVitals.systolicMax }
                    onClicked   : _root.clicked()
                    nextInput   : _bloodPressureDiastolic
                    textInput.color: (textInput.acceptableInput && _private.isBloodPressureCompareValid) ? Colors.textMain : Colors.red
                }
                Label {
                    text    : "/"
                    width   : 10
                }
                TextEntry { id  : _bloodPressureDiastolic
                    text        :  Variables.notSetVariable(vTreatmentVitals.diastolic, 0)
                    label.width : 0
                    validator   : IntValidator { bottom: vTreatmentVitals.diastolicMin;  top : vTreatmentVitals.diastolicMax }
                    onClicked   : _root.clicked()
                    nextInput   : _heartRate
                    textInput.color: (textInput.acceptableInput && _private.isBloodPressureCompareValid) ? Colors.textMain : Colors.red
                }
                Label {
                    text    : qsTr("mmHg")
                    width   : unitWidth
                }
            }
            Row { spacing     : 10
                TextEntry { id  : _heartRate
                    text        :  Variables.notSetVariable(vTreatmentVitals.heartRate, 0)
                    label.text  :  qsTr("Heart Rate")
                    label.width : _root.labelWidth
                    onClicked   : _root.clicked()
                    validator   : IntValidator { bottom: vTreatmentVitals.heartRateMin;  top : vTreatmentVitals.heartRateMax }
                }
                Label {
                    text    : qsTr("BPM")
                    width   : unitWidth
                }
            }
        }
    }
}
