@file:Suppress("UNCHECKED_CAST", "USELESS_CAST", "INAPPLICABLE_JVM_NAME", "UNUSED_ANONYMOUS_PARAMETER", "NAME_SHADOWING", "UNNECESSARY_NOT_NULL_ASSERTION")
package uni.UNIuniappx
import io.dcloud.uniapp.*
import io.dcloud.uniapp.extapi.*
import io.dcloud.uniapp.framework.*
import io.dcloud.uniapp.runtime.*
import io.dcloud.uniapp.vue.*
import io.dcloud.uniapp.vue.shared.*
import io.dcloud.unicloud.*
import io.dcloud.uts.*
import io.dcloud.uts.Map
import io.dcloud.uts.Set
import io.dcloud.uts.UTSAndroid
import io.dcloud.uniapp.extapi.closeBLEConnection as uni_closeBLEConnection
import io.dcloud.uniapp.extapi.closeBluetoothAdapter as uni_closeBluetoothAdapter
import io.dcloud.uniapp.extapi.createBLEConnection as uni_createBLEConnection
import io.dcloud.uniapp.extapi.hideLoading as uni_hideLoading
import io.dcloud.uniapp.extapi.navigateTo as uni_navigateTo
import io.dcloud.uniapp.extapi.offBluetoothDeviceFound as uni_offBluetoothDeviceFound
import io.dcloud.uniapp.extapi.onBluetoothDeviceFound as uni_onBluetoothDeviceFound
import io.dcloud.uniapp.extapi.openBluetoothAdapter as uni_openBluetoothAdapter
import io.dcloud.uniapp.extapi.showLoading as uni_showLoading
import io.dcloud.uniapp.extapi.showModal as uni_showModal
import io.dcloud.uniapp.extapi.showToast as uni_showToast
import io.dcloud.uniapp.extapi.startBluetoothDevicesDiscovery as uni_startBluetoothDevicesDiscovery
import io.dcloud.uniapp.extapi.stopBluetoothDevicesDiscovery as uni_stopBluetoothDevicesDiscovery
open class GenPagesIndexIndex : BasePage {
    constructor(__ins: ComponentInternalInstance, __renderer: String?) : super(__ins, __renderer) {
        onLoad(fun(_: OnLoadOptions) {
            this.initBluetooth()
        }
        , __ins)
        onUnload(fun() {
            this.stopScan()
            uni_closeBluetoothAdapter()
            uni_offBluetoothDeviceFound(this.onDeviceFound)
        }
        , __ins)
    }
    @Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
    override fun `$render`(): Any? {
        val _ctx = this
        val _cache = this.`$`.renderCache
        return _cE("view", _uM("class" to "container"), _uA(
            _cE("view", _uM("class" to "title-bar"), _uA(
                _cE("text", _uM("class" to "title"), "连接设备"),
                _cE("button", _uM("class" to "scan-btn", "disabled" to _ctx.isScanning, "onClick" to _ctx.toggleScan), _tD(if (_ctx.isScanning) {
                    "停止扫描"
                } else {
                    "开始扫描"
                }
                ), 9, _uA(
                    "disabled",
                    "onClick"
                ))
            )),
            if (isTrue(_ctx.isScanning)) {
                _cE("view", _uM("key" to 0, "class" to "status-tip"), _uA(
                    _cE("text", null, "正在扫描设备..."),
                    _cE("view", _uM("class" to "loading"))
                ))
            } else {
                if (_ctx.foundDevices.length === 0) {
                    _cE("view", _uM("key" to 1, "class" to "status-tip"), _uA(
                        _cE("text", null, "未发现设备，请点击\"开始扫描\"")
                    ))
                } else {
                    _cC("v-if", true)
                }
            }
            ,
            _cE("view", _uM("class" to "device-list"), _uA(
                _cE(Fragment, null, RenderHelpers.renderList(_ctx.foundDevices, fun(device, index, __index, _cached): Any {
                    return _cE("view", _uM("class" to "device-item", "key" to device.deviceId, "onClick" to fun(){
                        _ctx.connectDevice(device)
                    }
                    ), _uA(
                        _cE("view", _uM("class" to "device-name"), _uA(
                            _cE("text", null, _tD(device.name || "未知设备"), 1),
                            if (isTrue(device.is_bj)) {
                                _cE("view", _uM("key" to 0, "class" to "is_bj"), "吧唧")
                            } else {
                                _cC("v-if", true)
                            }
                            ,
                            _cE("text", _uM("class" to "device-id"), _tD(device.deviceId), 1)
                        )),
                        _cE("view", _uM("class" to "device-rssi"), _uA(
                            _cE("text", null, "信号: " + _tD(device.rssi) + " dBm", 1),
                            _cE("view", _uM("class" to "rssi-bar", "style" to _nS(_uM("width" to _ctx.getRssiWidth(device.rssi)))), null, 4)
                        )),
                        if (isTrue(device.connected)) {
                            _cE("view", _uM("key" to 0, "class" to "device-status"), _uA(
                                _cE("text", _uM("class" to "connected"), "已连接")
                            ))
                        } else {
                            _cC("v-if", true)
                        }
                    ), 8, _uA(
                        "onClick"
                    ))
                }
                ), 128)
            ))
        ))
    }
    open var foundDevices: UTSArray<Any?> by `$data`
    open var isScanning: Boolean by `$data`
    open var bluetoothEnabled: Boolean by `$data`
    open var connectedDeviceId: String by `$data`
    @Suppress("USELESS_CAST")
    override fun data(): Map<String, Any?> {
        return _uM("foundDevices" to _uA(), "isScanning" to false, "bluetoothEnabled" to false, "connectedDeviceId" to "")
    }
    open var initBluetooth = ::gen_initBluetooth_fn
    open fun gen_initBluetooth_fn() {
        uni_openBluetoothAdapter(OpenBluetoothAdapterOptions(success = fun(_){
            this.bluetoothEnabled = true
        }
        , fail = fun(err){
            this.bluetoothEnabled = false
            uni_showModal(ShowModalOptions(title = "蓝牙开启失败", content = "请检查设备蓝牙是否开启", showCancel = false))
            console.error("蓝牙初始化失败:", err, " at pages/index/index.uvue:88")
        }
        ))
    }
    open var toggleScan = ::gen_toggleScan_fn
    open fun gen_toggleScan_fn() {
        if (!this.bluetoothEnabled) {
            this.initBluetooth()
            return
        }
        if (this.isScanning) {
            this.stopScan()
        } else {
            this.startScan()
        }
    }
    open var startScan = ::gen_startScan_fn
    open fun gen_startScan_fn() {
        this.isScanning = true
        this.foundDevices = _uA()
        uni_startBluetoothDevicesDiscovery(StartBluetoothDevicesDiscoveryOptions(services = _uA(), allowDuplicatesKey = false, success = fun(_){
            uni_showToast(ShowToastOptions(title = "开始扫描", icon = "none"))
            uni_onBluetoothDeviceFound(this.onDeviceFound)
        }
        , fail = fun(err){
            this.isScanning = false
            uni_showToast(ShowToastOptions(title = "扫描失败", icon = "none"))
            console.error("扫描失败:", err, " at pages/index/index.uvue:124")
        }
        ))
        setTimeout(fun(){
            if (this.isScanning) {
                this.stopScan()
            }
        }
        , 5000)
    }
    open var stopScan = ::gen_stopScan_fn
    open fun gen_stopScan_fn() {
        if (!this.isScanning) {
            return
        }
        uni_stopBluetoothDevicesDiscovery(StopBluetoothDevicesDiscoveryOptions(success = fun(_){
            this.isScanning = false
            if (this.foundDevices.length == 0) {
                uni_showToast(ShowToastOptions(title = "\u6682\u672A\u626B\u63CF\u5230\u4EFB\u4F55\u8BBE\u5907", icon = "none"))
                return
            }
            uni_showToast(ShowToastOptions(title = "\u626B\u63CF\u5B8C\u6210\uFF0C\u53D1\u73B0" + this.foundDevices.length + "\u53F0\u8BBE\u5907", icon = "none"))
        }
        , fail = fun(err){
            console.error("停止扫描失败:", err, " at pages/index/index.uvue:154")
        }
        ))
    }
    open var onDeviceFound = ::gen_onDeviceFound_fn
    open fun gen_onDeviceFound_fn(res) {
        val devices = res.devices || _uA()
        devices.forEach(fun(device){
            if (!device.deviceId) {
                return
            }
            val isExist = this.foundDevices.some(fun(d): Boolean {
                return d.deviceId === device.deviceId
            }
            )
            if (isExist) {
                return
            }
            var is_bj = false
            val advData = Uint8Array(device.advertisData)
            val devicenameData = advData.slice(2, 6)
            val devicename = String.fromCharCode(*devicenameData.toTypedArray())
            if (devicename == "dzbj") {
                is_bj = true
            }
            this.foundDevices.push(_uO("is_bj" to is_bj, "name" to (device.name || "未知设备"), "deviceId" to device.deviceId, "rssi" to device.RSSI, "connected" to (device.deviceId === this.connectedDeviceId)))
        }
        )
    }
    open var getRssiWidth = ::gen_getRssiWidth_fn
    open fun gen_getRssiWidth_fn(rssi): String {
        val minRssi: Number = -100
        val maxRssi: Number = -30
        var ratio = (rssi - minRssi) / (maxRssi - minRssi)
        ratio = Math.max(0, Math.min(1, ratio))
        return "" + ratio * 100 + "%"
    }
    open var connectDevice = ::gen_connectDevice_fn
    open fun gen_connectDevice_fn(device) {
        var that = this
        this.stopScan()
        uni_showLoading(ShowLoadingOptions(title = "连接中..."))
        uni_createBLEConnection(CreateBLEConnectionOptions(deviceId = device.deviceId, success = fun(res) {
            that.foundDevices = that.foundDevices.map(fun(d){
                return UTSJSONObject.assign(UTSJSONObject(), d, object : UTSJSONObject() {
                    var connected = d.deviceId === device.deviceId
                })
            }
            )
            uni_hideLoading()
            uni_showToast(ShowToastOptions(title = "\u5DF2\u8FDE\u63A5" + device.name, icon = "none"))
            uni_navigateTo(NavigateToOptions(url = "../connect/connect?deviceId=" + device.deviceId, fail = fun(err){
                uni_showToast(ShowToastOptions(title = "\u8FDE\u63A5\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5", icon = "error"))
                uni_closeBLEConnection(CloseBLEConnectionOptions(deviceId = device.deviceId, success = fun(_) {
                    console("断开连接成功")
                }
                ))
            }
            ))
        }
        , fail = fun(_) {
            uni_hideLoading()
            uni_showToast(ShowToastOptions(title = "\u8FDE\u63A5\u5931\u8D25", icon = "error"))
        }
        ))
    }
    companion object {
        val styles: Map<String, Map<String, Map<String, Any>>> by lazy {
            _nCS(_uA(
                styles0
            ), _uA(
                GenApp.styles
            ))
        }
        val styles0: Map<String, Map<String, Map<String, Any>>>
            get() {
                return _uM("container" to _pS(_uM("paddingTop" to "16rpx", "paddingRight" to "16rpx", "paddingBottom" to "16rpx", "paddingLeft" to "16rpx", "backgroundColor" to "#f5f5f5")), "title-bar" to _pS(_uM("display" to "flex", "justifyContent" to "space-between", "alignItems" to "center", "paddingTop" to "20rpx", "paddingRight" to 0, "paddingBottom" to "20rpx", "paddingLeft" to 0, "borderBottomWidth" to 1, "borderBottomStyle" to "solid", "borderBottomColor" to "#eeeeee", "marginBottom" to "20rpx", "marginTop" to "80rpx")), "title" to _pS(_uM("fontSize" to "36rpx", "fontWeight" to "bold", "color" to "#333333")), "scan-btn" to _pS(_uM("backgroundColor" to "#00c900", "color" to "#FFFFFF", "paddingTop" to "0rpx", "paddingRight" to "24rpx", "paddingBottom" to "0rpx", "paddingLeft" to "24rpx", "borderTopLeftRadius" to "8rpx", "borderTopRightRadius" to "8rpx", "borderBottomRightRadius" to "8rpx", "borderBottomLeftRadius" to "8rpx", "fontSize" to "28rpx", "marginRight" to "5%", "backgroundColor:disabled" to "#cccccc")), "status-tip" to _pS(_uM("textAlign" to "center", "paddingTop" to "40rpx", "paddingRight" to 0, "paddingBottom" to "40rpx", "paddingLeft" to 0, "color" to "#666666", "fontSize" to "28rpx")), "loading" to _pS(_uM("width" to "30rpx", "height" to "30rpx", "borderTopWidth" to "3rpx", "borderRightWidth" to "3rpx", "borderBottomWidth" to "3rpx", "borderLeftWidth" to "3rpx", "borderTopStyle" to "solid", "borderRightStyle" to "solid", "borderBottomStyle" to "solid", "borderLeftStyle" to "solid", "borderTopColor" to "rgba(0,0,0,0)", "borderRightColor" to "#007aff", "borderBottomColor" to "#007aff", "borderLeftColor" to "#007aff", "marginTop" to "20rpx", "marginRight" to "auto", "marginBottom" to "20rpx", "marginLeft" to "auto", "animation" to "spin 1s linear infinite")), "device-list" to _pS(_uM("display" to "flex", "flexDirection" to "column", "gap" to "16rpx")), "device-item" to _pS(_uM("backgroundColor" to "#FFFFFF", "borderTopLeftRadius" to "12rpx", "borderTopRightRadius" to "12rpx", "borderBottomRightRadius" to "12rpx", "borderBottomLeftRadius" to "12rpx", "paddingTop" to "24rpx", "paddingRight" to "24rpx", "paddingBottom" to "24rpx", "paddingLeft" to "24rpx", "boxShadow" to "0 2rpx 8rpx rgba(0,0,0,0.1)", "cursor" to "pointer")), "device-name" to _pS(_uM("display" to "flex", "justifyContent" to "space-between", "marginBottom" to "16rpx")), "is_bj" to _pS(_uM("paddingTop" to 3, "paddingRight" to 8, "paddingBottom" to 3, "paddingLeft" to 8, "borderTopLeftRadius" to 5, "borderTopRightRadius" to 5, "borderBottomRightRadius" to 5, "borderBottomLeftRadius" to 5, "color" to "#FFFFFF", "backgroundColor" to "rgba(30,228,156,0.4)", "position" to "relative", "right" to 30)), "device-id" to _pS(_uM("fontSize" to "24rpx", "color" to "#999999")), "device-rssi" to _pS(_uM("display" to "flex", "flexDirection" to "column", "gap" to "8rpx")), "rssi-bar" to _pS(_uM("height" to "8rpx", "backgroundColor" to "#eeeeee", "borderTopLeftRadius" to "4rpx", "borderTopRightRadius" to "4rpx", "borderBottomRightRadius" to "4rpx", "borderBottomLeftRadius" to "4rpx", "overflow" to "hidden", "content::after" to "''", "height::after" to "100%", "backgroundImage::after" to "linear-gradient(to right, #ff4d4f, #faad14, #52c41a)", "backgroundColor::after" to "rgba(0,0,0,0)")), "device-status" to _pS(_uM("marginTop" to "16rpx", "textAlign" to "right")), "connected" to _pS(_uM("fontSize" to "26rpx", "color" to "#07c160", "backgroundColor" to "#f0fff4", "paddingTop" to "4rpx", "paddingRight" to "16rpx", "paddingBottom" to "4rpx", "paddingLeft" to "16rpx", "borderTopLeftRadius" to "12rpx", "borderTopRightRadius" to "12rpx", "borderBottomRightRadius" to "12rpx", "borderBottomLeftRadius" to "12rpx")), "@FONT-FACE" to _uM("0" to _uM()))
            }
        var inheritAttrs = true
        var inject: Map<String, Map<String, Any?>> = _uM()
        var emits: Map<String, Any?> = _uM()
        var props = _nP(_uM())
        var propsNeedCastKeys: UTSArray<String> = _uA()
        var components: Map<String, CreateVueComponent> = _uM()
    }
}
