/*!
 *
 * 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    SettingsBluetoothCuff.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      23-Nov-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  06-Sep-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
import Gui.Actions 0.1

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

/*!
 * \brief   SettingsBluetoothCuff is used to scan for Omron Bluetooth Cuff
 *          devices and to let user to connect to it to be able to read the Vital measurements.
 */
SettingsBase                { id: _root
    itemIndex : SettingsStack.Bluetooth
    confirmVisible  : false
    Image { id: _image
        width   : 640
        height  : 480
        source: "qrc:/images/iCuff-Omron-BP7000"
        anchors {
            top             : parent.top
            topMargin       : _root.topMarginContent
            right           : parent.right
            rightMargin     : 0 // the image itself has some margins //  kept to make sure it was intentional.
        }
    }
    Column          { id    : _deviceColumn
        property int labelHeight: 25

        anchors.top         : _image.top
        anchors.left        : parent.left
        anchors.leftMargin  : 50
        spacing             : 10
        Label       { id    : _deviceTitle
            text            : qsTr("Devices")
        }
        ListView { id: _deviceList
            ScrollBar {
                anchors.fill: _deviceList
                flickable   : _deviceList
            }
            clip    : true
            spacing : 5
            width   : 500
            height  : 300
            model   : vBluetooth
            delegate: TouchRect { id: _deviceTouchrect
                readonly property color selectedColor: Colors.borderButtonSelected
                readonly property color   pairedColor: Colors.borderButton
                readonly property color   normalColor: Colors.borderButtonUnselected
                property bool selectedDevice: addr === vBluetooth.pairedAddr
                clip    : true
                radius  : 10
                width   : 500 - 10 // give room to scrollbar
                height  : 70
                color   : Colors.transparent
                border.color: selectedDevice ? selectedColor : (pair ? pairedColor : normalColor)
                text {
                    text: addr + '\n' + name
                    anchors.horizontalCenter: undefined
                    anchors.left: _deviceTouchrect.left
                    anchors.leftMargin: 5
                }
                onClicked: {
                    vBluetooth.didDeviceSelect(addr, name)
                }
                ProgressCircle {
                    minimum : 0
                    maximum : 100
                    value   : vBluetooth.pairedBatt
                    color   : _deviceTouchrect.border.color
                    visible : selectedDevice
                    diameter    : 25
                    anchors.margins: 5
                    anchors.top: parent.top
                    anchors.right: parent.right
                    Text {
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        color   : Colors.white
                        anchors.fill: parent
                        font.pixelSize: 10
                        text: vBluetooth.pairedBatt
                    }
                }
            }
        }
    }

    TouchRect { id          : _scanButton
        anchors.bottom      : parent.bottom
        anchors.bottomMargin: Variables.mainMenuHeight * 2 + Variables.minVGap
        anchors.left        : parent.left
        anchors.leftMargin  : 100
        text.text           : qsTr("SCAN")
        width               : 300
        isDefault           : false
        enabled             : vBluetooth.scanEnabled && vBluetooth.isInvalid
        onClicked           : {
            vDevice.doInitBluetoothPairedReset()
        }
    }

    Connections { target: vDevice
        function onBluetoothPairedResetChanged  ( vValue ) {
            // DEBUG: console.debug("bluetoothPairedReset", vbluetoothPairedReset, vDevice.accepted, vDevice.reason, vDevice.status)
            // NOTE : vbluetoothPairedReset eq vDevice.reason
            if ( vDevice.accepted ) {
                vBluetooth.doScan()
            }
        }
    }

    WaitDone { id           : _scanIndication
        anchors.horizontalCenter    : parent.horizontalCenter
        anchors.verticalCenter      : _scanButton.verticalCenter
        diameter            : _scanButton.height
        visible             : ! vBluetooth.scanEnabled && ! vBluetooth.isInvalid
        // done    : _delegate.stepDone
    }
    notificationText        : vBluetooth.notification || vDevice.status
    Component.onCompleted   : {
        vDevice.doInitBluetoothPairedQuery()
    }
}
