@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.createSelectorQuery as uni_createSelectorQuery
import io.dcloud.uniapp.extapi.offGyroscopeChange as uni_offGyroscopeChange
import io.dcloud.uniapp.extapi.onGyroscopeChange as uni_onGyroscopeChange
import io.dcloud.uniapp.extapi.startGyroscope as uni_startGyroscope
import io.dcloud.uniapp.extapi.stopGyroscope as uni_stopGyroscope
open class GenPagesAPIGyroscopePanorama : 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)
    }
    open var startListen: () -> Unit
        get() {
            return unref(this.`$exposed`["startListen"]) as () -> Unit
        }
        set(value) {
            setRefValue(this.`$exposed`, "startListen", value)
        }
    open var stopListen: () -> Unit
        get() {
            return unref(this.`$exposed`["stopListen"]) as () -> Unit
        }
        set(value) {
            setRefValue(this.`$exposed`, "stopListen", value)
        }
    companion object {
        @Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
        var setup: (__props: GenPagesAPIGyroscopePanorama, __setupCtx: SetupContext) -> Any? = fun(__props, __setupCtx): Any? {
            val __expose = __setupCtx.expose
            val __ins = getCurrentInstance()!!
            val _ctx = __ins.proxy as GenPagesAPIGyroscopePanorama
            val _cache = __ins.renderCache
            val panoramaWidth: Number = 1400
            val pixelsPerRadian: Number = 320
            val statusText = ref("正在初始化陀螺仪...")
            val offsetText = ref("0 px")
            val velocityText = ref("0.0000")
            val panoramaStyle = ref("transform: translateX(0px)")
            val viewportWidth = ref(0)
            val callBackId = ref(0)
            var currentOffset: Number = 0.0
            var lastTimestamp: Number? = null
            fun gen_clamp_fn(value: Number, min: Number, max: Number): Number {
                if (value < min) {
                    return min
                }
                if (value > max) {
                    return max
                }
                return value
            }
            val clamp = ::gen_clamp_fn
            fun gen_updatePanoramaStyle_fn(): Unit {
                val rounded = Math.round(currentOffset)
                offsetText.value = "" + rounded + " px"
                panoramaStyle.value = "transform: translateX(-" + rounded + "px)"
            }
            val updatePanoramaStyle = ::gen_updatePanoramaStyle_fn
            fun gen_getMaxOffset_fn(): Number {
                val hiddenWidth = panoramaWidth - viewportWidth.value
                if (hiddenWidth > 0) {
                    return hiddenWidth
                }
                return 0
            }
            val getMaxOffset = ::gen_getMaxOffset_fn
            fun gen_measureViewport_fn(): Unit {
                uni_createSelectorQuery().select("#panoramaViewport").boundingClientRect().exec(fun(ret){
                    if (ret.length > 0) {
                        val rect = ret[0] as NodeInfo
                        val rectWidth = if (rect.width != null) {
                            rect.width!!
                        } else {
                            0
                        }
                        viewportWidth.value = Math.round(rectWidth)
                        currentOffset = clamp(currentOffset, 0, getMaxOffset())
                        updatePanoramaStyle()
                    }
                }
                )
            }
            val measureViewport = ::gen_measureViewport_fn
            val gyroscopeListener: OnGyroscopeChangeCallback = fun(result: OnGyroscopeChangeCallbackResult){
                velocityText.value = result.y.toFixed(4)
                if (viewportWidth.value <= 0) {
                    return
                }
                val now = Date.now()
                if (lastTimestamp == null) {
                    lastTimestamp = now
                    statusText.value = "请左右转动手机查看全景"
                    return
                }
                val deltaSeconds = (now - (lastTimestamp as Number)) / 1000
                lastTimestamp = now
                currentOffset = clamp(currentOffset + result.y * deltaSeconds * pixelsPerRadian, 0, getMaxOffset())
                statusText.value = "请左右转动手机查看全景"
                updatePanoramaStyle()
            }
            fun gen_startListen_fn(): Unit {
                uni_startGyroscope(StartGyroscopeOptions(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_stopGyroscope(StopGyroscopeOptions(success = fun(_){
                    statusText.value = "已停止监听"
                }
                ))
                lastTimestamp = null
            }
            val stopListen = ::gen_stopListen_fn
            onReady(fun(){
                measureViewport()
                uni_onGyroscopeChange(gyroscopeListener)
                startListen()
            }
            )
            onShow(fun(){
                measureViewport()
                startListen()
            }
            )
            onHide(fun(){
                stopListen()
            }
            )
            onResize(fun(_options){
                measureViewport()
            }
            )
            onUnload(fun(){
                uni_offGyroscopeChange(gyroscopeListener)
                uni_stopGyroscope(null)
            }
            )
            __expose(_uM("startListen" to startListen, "stopListen" to stopListen))
            return fun(): Any? {
                return _cE("scroll-view", _uM("style" to _nS(_uM("flex" to "1", "padding" to "6px"))), _uA(
                    _cE("text", _uM("class" to "uni-h2"), "全景浏览"),
                    _cE("text", null, "左右转动手机，超宽画卷会跟随陀螺仪横向移动，展示屏幕之外的内容。"),
                    _cE("text", _uM("class" to "uni-h3"), "当前状态"),
                    _cE("text", _uM("id" to "panoramaStatus", "class" to "margin-v"), _tD(unref(statusText)), 1),
                    _cE("view", _uM("class" to "metric-row"), _uA(
                        _cE("view", _uM("class" to "metric-box"), _uA(
                            _cE("text", _uM("class" to "metric-label"), "水平偏移"),
                            _cE("text", _uM("class" to "metric-value"), _tD(unref(offsetText)), 1)
                        )),
                        _cE("view", _uM("class" to "metric-box metric-box-space"), _uA(
                            _cE("text", _uM("class" to "metric-label"), "角速度 Y"),
                            _cE("text", _uM("class" to "metric-value"), _tD(unref(velocityText)), 1)
                        ))
                    )),
                    _cE("text", _uM("class" to "uni-h3"), "移动视窗"),
                    _cE("text", _uM("class" to "margin-v"), "将手机朝左或朝右缓慢转动，观察画面边缘不断切换的色块与文字。"),
                    _cE("view", _uM("id" to "panoramaViewport", "class" to "viewport"), _uA(
                        _cE("view", _uM("class" to "panorama-strip", "style" to _nS(unref(panoramaStyle))), _uA(
                            _cE("view", _uM("class" to "gradient-content"), _uA(
                                _cE("text", _uM("class" to "panel-title"), "渐变全景画卷"),
                                _cE("text", _uM("class" to "panel-copy"), "从红色过渡到黄色的超宽视界")
                            )),
                            _cE("view", _uM("class" to "end-tip"), _uA(
                                _cE("text", _uM("class" to "end-tip-text"), "到达尾部了"),
                                _cE("text", _uM("class" to "end-tip-sub"), "已展示全部内容")
                            ))
                        ), 4)
                    ))
                ), 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("margin-v" to _pS(_uM("marginTop" to 5, "marginRight" to 0, "marginBottom" to 5, "marginLeft" to 0)), "metric-row" to _pS(_uM("marginTop" to 5, "marginRight" to 0, "marginBottom" to 5, "marginLeft" to 0, "display" to "flex", "flexDirection" to "row")), "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, "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-space" to _pS(_uM("marginLeft" to 10)), "metric-value" to _pS(_uM("marginTop" to 4, "fontSize" to 30)), "viewport" to _pS(_uM("marginTop" to 5, "marginRight" to 0, "marginBottom" to 5, "marginLeft" to 0, "height" to 220, "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", "overflow" to "hidden")), "panorama-strip" to _pS(_uM("width" to 1400, "height" to 220, "backgroundImage" to "linear-gradient(to right, #ff4d4d, #fffa65)", "display" to "flex", "flexDirection" to "row", "position" to "relative")), "gradient-content" to _pS(_uM("width" to "100%", "height" to "100%", "paddingTop" to 20, "paddingRight" to 20, "paddingBottom" to 20, "paddingLeft" to 20, "display" to "flex", "flexDirection" to "column", "justifyContent" to "flex-end")), "panel-title" to _pS(_uM("fontSize" to 30, "color" to "#ffffff", "textShadow" to "1px 1px 2px rgba(0, 0, 0, 0.3)")), "panel-copy" to _pS(_uM("marginTop" to 6, "fontSize" to 14, "color" to "#ffffff")), "end-tip" to _pS(_uM("position" to "absolute", "right" to 40, "bottom" to 20, "display" to "flex", "flexDirection" to "column", "alignItems" to "flex-end")), "end-tip-text" to _pS(_uM("fontSize" to 24, "fontWeight" to "bold", "color" to "#333333")), "end-tip-sub" to _pS(_uM("fontSize" to 12, "color" to "#666666", "marginTop" to 4)))
            }
        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()
    }
}
