@file:Suppress("UNCHECKED_CAST", "USELESS_CAST", "INAPPLICABLE_JVM_NAME", "UNUSED_ANONYMOUS_PARAMETER", "SENSELESS_COMPARISON", "NAME_SHADOWING", "UNNECESSARY_NOT_NULL_ASSERTION")
package uni.UNIHelloUniAppX
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 kotlin.properties.Delegates
import io.dcloud.uniapp.extapi.offAccelerometerChange as uni_offAccelerometerChange
import io.dcloud.uniapp.extapi.onAccelerometerChange as uni_onAccelerometerChange
import io.dcloud.uniapp.extapi.showToast as uni_showToast
import io.dcloud.uniapp.extapi.startAccelerometer as uni_startAccelerometer
import io.dcloud.uniapp.extapi.stopAccelerometer as uni_stopAccelerometer
open class GenPagesAPIAccelerometerShake : BasePage {
    constructor(__ins: ComponentInternalInstance, __renderer: String?) : super(__ins, __renderer) {
        onLoad(fun(_: OnLoadOptions) {
            stat_instance.onLoad(this)
        }
        , __ins)
        onPageShow(fun() {
            stat_instance.onShow(this)
        }
        , __ins)
        onPageHide(fun() {
            stat_instance.onHide(this)
        }
        , __ins)
        onUnload(fun() {
            stat_instance.onUnload(this)
        }
        , __ins)
    }
    companion object {
        @Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
        var setup: (__props: GenPagesAPIAccelerometerShake) -> Any? = fun(__props): Any? {
            val __ins = getCurrentInstance()!!
            val _ctx = __ins.proxy as GenPagesAPIAccelerometerShake
            val _cache = __ins.renderCache
            val statusText = ref("正在初始化传感器...")
            val deltaText = ref("0.00")
            val shakeCountText = ref("0")
            val thresholdText = ref("1.35")
            val sensitivityLabel = ref("中")
            val sensitivityValue = ref(3)
            val shakeCooldown: Number = 1200
            var lastMagnitude: Number? = null
            var lastShakeTime: Number = 0
            var shakeCount: Number = 0
            var shakeThreshold: Number = 1.35
            fun gen_formatNumber_fn(value: Number): String {
                return value.toFixed(2)
            }
            val formatNumber = ::gen_formatNumber_fn
            fun gen_createMagnitude_fn(x: Number, y: Number, z: Number): Number {
                return Math.sqrt(x * x + y * y + z * z)
            }
            val createMagnitude = ::gen_createMagnitude_fn
            fun gen_getSensitivityLabel_fn(value: Number): String {
                if (value <= 1) {
                    return "很低"
                }
                if (value == 2) {
                    return "较低"
                }
                if (value == 3) {
                    return "中"
                }
                if (value == 4) {
                    return "较高"
                }
                return "很高"
            }
            val getSensitivityLabel = ::gen_getSensitivityLabel_fn
            fun gen_syncSensitivity_fn(value: Number): Unit {
                sensitivityValue.value = value
                shakeThreshold = 2.1 - value * 0.25
                thresholdText.value = formatNumber(shakeThreshold)
                sensitivityLabel.value = getSensitivityLabel(value)
            }
            val syncSensitivity = ::gen_syncSensitivity_fn
            fun gen_triggerShake_fn(now: Number): Unit {
                lastShakeTime = now
                shakeCount = shakeCount + 1
                shakeCountText.value = "" + shakeCount
                statusText.value = "检测成功，继续摇晃可再次触发"
                uni_showToast(ShowToastOptions(title = "检测到摇一摇", icon = "none"))
            }
            val triggerShake = ::gen_triggerShake_fn
            val accelerometerListener: OnAccelerometerChangeCallback = fun(result: OnAccelerometerChangeCallbackResult){
                val magnitude = createMagnitude(result.x, result.y, result.z)
                if (lastMagnitude == null) {
                    lastMagnitude = magnitude
                    statusText.value = "请开始摇晃手机"
                    return
                }
                val delta = Math.abs(magnitude - (lastMagnitude as Number))
                deltaText.value = formatNumber(delta)
                lastMagnitude = magnitude
                val now = Date.now()
                val passed = now - lastShakeTime
                if (delta > shakeThreshold && passed > shakeCooldown) {
                    triggerShake(now)
                }
            }
            fun gen_onSensitivityChanging_fn(event: UniSliderChangeEvent): Unit {
                syncSensitivity(event.detail.value)
            }
            val onSensitivityChanging = ::gen_onSensitivityChanging_fn
            fun gen_onSensitivityChange_fn(event: UniSliderChangeEvent): Unit {
                syncSensitivity(event.detail.value)
                statusText.value = "当前灵敏度：" + sensitivityLabel.value + "，请继续摇晃手机"
            }
            val onSensitivityChange = ::gen_onSensitivityChange_fn
            fun gen_startListen_fn(): Unit {
                uni_startAccelerometer(StartAccelerometerOptions(interval = "game", success = fun(_){
                    statusText.value = "请开始摇晃手机"
                }
                , fail = fun(error){
                    statusText.value = if (error.errMsg != null) {
                        error.errMsg
                    } else {
                        "传感器启动失败"
                    }
                }
                ))
            }
            val startListen = ::gen_startListen_fn
            fun gen_stopListen_fn(): Unit {
                uni_stopAccelerometer(StopAccelerometerOptions(success = fun(_){
                    statusText.value = "已停止监听"
                }
                ))
            }
            val stopListen = ::gen_stopListen_fn
            onReady(fun(){
                syncSensitivity(sensitivityValue.value)
                uni_onAccelerometerChange(accelerometerListener)
                startListen()
            }
            )
            onShow(fun(){
                startListen()
            }
            )
            onHide(fun(){
                stopListen()
            }
            )
            onUnload(fun(){
                uni_offAccelerometerChange(accelerometerListener)
                uni_stopAccelerometer(null)
            }
            )
            return fun(): Any? {
                val _component_slider = resolveComponent("slider")
                return _cE("scroll-view", _uM("style" to _nS(_uM("flex" to "1", "padding" to "6px"))), _uA(
                    _cE("text", _uM("class" to "uni-h3"), "摇一摇"),
                    _cE("text", null, "握住手机并快速摇晃，会调用加速计传感器，依据加速度变化检测动作，并弹出提示。"),
                    _cE("text", _uM("class" to "log-text margin-v"), _tD(unref(statusText)), 1),
                    _cE("view", _uM("class" to "uni-row margin-v"), _uA(
                        _cE("view", _uM("class" to "metric-box"), _uA(
                            _cE("text", null, "瞬时变化"),
                            _cE("text", _uM("class" to "metric-value"), _tD(unref(deltaText)), 1)
                        )),
                        _cE("view", _uM("class" to "metric-box metric-box-space"), _uA(
                            _cE("text", null, "触发次数"),
                            _cE("text", _uM("class" to "metric-value"), _tD(unref(shakeCountText)), 1)
                        ))
                    )),
                    _cE("view", _uM("class" to "sensitivity-header"), _uA(
                        _cE("text", null, "灵敏度"),
                        _cE("text", _uM("class" to "sensitivity-value"), _tD(unref(sensitivityLabel)), 1)
                    )),
                    _cV(_component_slider, _uM("class" to "sensitivity-slider", "value" to unref(sensitivityValue), "min" to 1, "max" to 5, "step" to 1, "onChanging" to onSensitivityChanging, "onChange" to onSensitivityChange), null, 8, _uA(
                        "value"
                    )),
                    _cE("text", _uM("class" to "sensitivity-tip"), "当前触发阈值：" + _tD(unref(thresholdText)) + "，数值越低灵敏度越高，越容易触发。", 1)
                ), 4)
            }
        }
        val styles: Map<String, Map<String, Map<String, Any>>> by lazy {
            _nCS(_uA(
                styles0
            ))
        }
        val styles0: Map<String, Map<String, Map<String, Any>>>
            get() {
                return _uM("card-space" to _pS(_uM("marginTop" to 5, "marginRight" to 5, "marginBottom" to 5, "marginLeft" to 5)), "margin-v" to _pS(_uM("marginTop" to 5, "marginRight" to 0, "marginBottom" to 5, "marginLeft" to 0)), "log-text" to _pS(_uM("paddingTop" to 10, "paddingRight" to 10, "paddingBottom" to 10, "paddingLeft" to 10, "fontSize" to 14, "borderTopWidth" to 1, "borderRightWidth" to 1, "borderBottomWidth" to 1, "borderLeftWidth" to 1, "borderTopStyle" to "solid", "borderRightStyle" to "solid", "borderBottomStyle" to "solid", "borderLeftStyle" to "solid", "borderTopColor" to "#cccccc", "borderRightColor" to "#cccccc", "borderBottomColor" to "#cccccc", "borderLeftColor" to "#cccccc")), "metric-box" to _pS(_uM("flexGrow" to 1, "flexShrink" to 1, "flexBasis" to "0%", "paddingTop" to 14, "paddingRight" to 14, "paddingBottom" to 14, "paddingLeft" to 14)), "metric-box-space" to _pS(_uM("marginLeft" to 10)), "metric-value" to _pS(_uM("marginTop" to 4, "fontSize" to 30)), "sensitivity-header" to _pS(_uM("display" to "flex", "flexDirection" to "row", "justifyContent" to "space-between", "alignItems" to "center")), "sensitivity-slider" to _pS(_uM("marginTop" to 15, "marginRight" to 15, "marginBottom" to 15, "marginLeft" to 15)))
            }
        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()
    }
}
