import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Analyser

Item {
    id: control
    RowLayout {
        anchors.fill: parent
        enabled: App.connected
        Column {
            GroupBox {
                title: "删除用户"
                GridLayout {
                    columns: 2
                    Label {
                        text: qsTr("用户ID")
                    }
                    TextField {
                        id: deleteUserId
                        implicitWidth: 100
                    }
                    Button {
                        text: "删除"
                        onClicked: App.deleteUser(parseInt(deleteUserId.text))
                    }
                    Button {
                        text: "删除所有"
                        onClicked: App.deleteAll()
                    }
                }
            }
            GroupBox {
                title: "其它"
                enabled: App.connected
                GridLayout {
                    columns: 2
                    Button {
                        text: "复位"
                        onClicked: App.module.reset()
                    }
                    Button {
                        text: "状态查询"
                        onClicked: App.module.requestCurrentStatus()
                    }
                    Button {
                        text: "ID查询"
                        onClicked: App.module.requestUniqueId()
                    }
                    Button {
                        id: otaButton
                        text: "OTA升级"
                        onClicked: loader.active = true
                    }
                    Row {
                        Label {
                            text: qsTr("日志")
                        }
                        Switch {
                            id: debugMode
                            onToggled: App.module.setDebugEnabled(debugMode.checked)
                        }
                    }
                }
            }
        }

        GroupBox {
            title: "图片注册"
            visible: true
            GridLayout {
                columns: 2
                TextField {
                    id: imagePath
                    Layout.columnSpan: 2
                    implicitWidth: 200
                    placeholderText: "请选择图片"
                    onPressed: {
                        fileDialog.open()
                    }
                }
                Label {
                    text: qsTr("用户姓名")
                }
                TextField {
                    id: imageEnrollName
                    implicitWidth: 100
                    text: "测试下发"
                }
                Label {
                    text: qsTr("循环")
                }
                Switch {
                    checked: App.imageUploadPersistenceMode
                    onToggled: {
                        App.imageUploadPersistenceMode = checked
                    }
                }
                Button {
                    text: "注册"
                    onClicked: App.uploadImage(imagePath.text, imageEnrollName.text, 0)
                }
                Button {
                    text: "识别"
                    onClicked: App.uploadImage(imagePath.text, imageEnrollName.text, 1)
                }
            }
        }
    }

    MouseArea {
        id: otaMouseArea
        width: otaButton.width
        height: otaButton.height
        enabled: !App.connected
        onClicked: {
            loader.active = true
        }
    }

    onVisibleChanged: {
        if (!visible)
            return
        Qt.callLater(() => {
            otaMouseArea.x = otaButton.mapToItem(control, 0, 0).x
            otaMouseArea.y = otaButton.mapToItem(control, 0, 0).y
        })
    }
}
