@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.offMemoryWarning as uni_offMemoryWarning
import io.dcloud.uniapp.extapi.onMemoryWarning as uni_onMemoryWarning
open class GenPagesAPIMemoryMemory : 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 state: PageState
        get() {
            return unref(this.`$exposed`["state"]) as PageState
        }
        set(value) {
            setRefValue(this.`$exposed`, "state", value)
        }
    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)
        }
    open var addMemoryPressure: () -> Unit
        get() {
            return unref(this.`$exposed`["addMemoryPressure"]) as () -> Unit
        }
        set(value) {
            setRefValue(this.`$exposed`, "addMemoryPressure", value)
        }
    open var releaseMemory: () -> Unit
        get() {
            return unref(this.`$exposed`["releaseMemory"]) as () -> Unit
        }
        set(value) {
            setRefValue(this.`$exposed`, "releaseMemory", value)
        }
    companion object {
        @Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
        var setup: (__props: GenPagesAPIMemoryMemory, __setupCtx: SetupContext) -> Any? = fun(__props, __setupCtx): Any? {
            val __expose = __setupCtx.expose
            val __ins = getCurrentInstance()!!
            val _ctx = __ins.proxy as GenPagesAPIMemoryMemory
            val _cache = __ins.renderCache
            val state = reactive(PageState(status = "未开始监听", records = _uA(
                "等待系统触发内存告警"
            ), listening = false, reservedMB = 0))
            var memoryWarningNativeCallback: OnMemoryWarningCallback? = null
            var memoryPressureBuffers: UTSArray<ArrayBuffer> = _uA()
            val memoryBlockSizeMB: Number = 32
            fun gen_appendRecord_fn(message: String): Unit {
                state.records.unshift(message)
                if (state.records.length > 8) {
                    state.records.splice(8)
                }
            }
            val appendRecord = ::gen_appendRecord_fn
            fun gen_formatLevel_fn(level: Number?): String {
                if (level == null) {
                    return "当前平台未返回等级"
                }
                if (level == 5) {
                    return "LOW(5)"
                }
                if (level == 10) {
                    return "MEDIUM(10)"
                }
                if (level == 15) {
                    return "CRITICAL(15)"
                }
                return "LEVEL(" + level + ")"
            }
            val formatLevel = ::gen_formatLevel_fn
            fun gen_reserveMemoryChunk_fn(sizeMB: Number): Unit {
                val buffer = ArrayBuffer(sizeMB * 1048576)
                val bytes = Uint8Array(buffer)
                var index: Number = 0
                while(index < bytes.length){
                    bytes[index] = 1
                    index += 4096
                }
                memoryPressureBuffers.push(buffer)
                state.reservedMB += sizeMB
            }
            val reserveMemoryChunk = ::gen_reserveMemoryChunk_fn
            fun gen_startListen_fn(): Unit {
                if (state.listening == true) {
                    appendRecord("监听已开启，无需重复注册")
                    return
                }
                val callback: OnMemoryWarningCallback = fun(res: MemoryWarningCallbackResult): Unit {
                    var levelText = "当前平台未返回等级"
                    val level = res.level ?: null
                    if (level != null) {
                        levelText = formatLevel(level)
                    }
                    val message = "收到内存告警：" + levelText
                    state.status = message
                    appendRecord(message)
                    console.log("onMemoryWarning", res)
                }
                memoryWarningNativeCallback = callback
                uni_onMemoryWarning(callback)
                state.listening = true
                state.status = "监听中，等待系统触发内存告警"
                appendRecord("已注册 uni.onMemoryWarning")
            }
            val startListen = ::gen_startListen_fn
            fun gen_stopListen_fn(): Unit {
                if (memoryWarningNativeCallback == null) {
                    uni_offMemoryWarning(null)
                    state.listening = false
                    state.status = "监听已停止"
                    appendRecord("已清空所有监听器")
                    return
                }
                val nativeCallback = memoryWarningNativeCallback!!
                uni_offMemoryWarning(nativeCallback)
                memoryWarningNativeCallback = null
                state.listening = false
                state.status = "监听已停止"
                appendRecord("已移除当前页面监听器")
            }
            val stopListen = ::gen_stopListen_fn
            fun gen_addMemoryPressure_fn(): Unit {
                try {
                    reserveMemoryChunk(memoryBlockSizeMB)
                    val message = "手动加压成功，当前约占用 " + state.reservedMB + "MB"
                    state.status = message
                    appendRecord("新增 32MB 内存占用")
                }
                 catch (e: Throwable) {
                    val message = "加压失败，当前约占用 " + state.reservedMB + "MB"
                    state.status = message
                    appendRecord(message)
                    console.error("addMemoryPressure", e)
                }
            }
            val addMemoryPressure = ::gen_addMemoryPressure_fn
            fun gen_releaseMemory_fn(): Unit {
                memoryPressureBuffers = _uA()
                state.reservedMB = 0
                state.status = "已释放示例页主动占用的内存"
                appendRecord(state.status)
            }
            val releaseMemory = ::gen_releaseMemory_fn
            onLoad(fun(_options){
                memoryWarningNativeCallback = null
                memoryPressureBuffers = _uA()
                state.listening = false
                state.reservedMB = 0
                state.status = "未开始监听"
            }
            )
            onUnload(fun(){
                if (memoryWarningNativeCallback != null) {
                    val nativeCallback = memoryWarningNativeCallback!!
                    uni_offMemoryWarning(nativeCallback)
                    memoryWarningNativeCallback = null
                }
                memoryPressureBuffers = _uA()
                state.reservedMB = 0
            }
            )
            __expose(_uM("state" to state, "startListen" to startListen, "stopListen" to stopListen, "addMemoryPressure" to addMemoryPressure, "releaseMemory" to releaseMemory))
            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-h3"), "内存告警监听 uni.onMemoryWarning"),
                    _cE("text", _uM("class" to "margin-v notice"), "注意：内存不足时，系统可能直接结束应用或 activity，不一定会先触发本告警。"),
                    _cE("text", _uM("class" to "margin-v"), "当前状态：" + _tD(unref(state).status), 1),
                    _cE("text", _uM("class" to "margin-v"), "主动占用内存：" + _tD(unref(state).reservedMB) + "MB", 1),
                    _cE("button", _uM("class" to "margin-v", "onClick" to startListen), "开始监听"),
                    _cE("button", _uM("class" to "margin-v", "onClick" to stopListen), "停止监听"),
                    _cE("button", _uM("class" to "margin-v", "onClick" to addMemoryPressure), "增加 32MB 内存占用"),
                    _cE("button", _uM("class" to "margin-v", "onClick" to releaseMemory), "释放已占用内存"),
                    _cE("text", _uM("class" to "uni-h4"), "最近事件："),
                    _cE("view", _uM("class" to "log-list"), _uA(
                        _cE(Fragment, null, RenderHelpers.renderList(unref(state).records, fun(item, index, __index, _cached): Any {
                            return _cE("text", _uM("class" to "log-item", "key" to index), _tD(item), 1)
                        }
                        ), 128)
                    ))
                ), 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)), "notice" to _pS(_uM("color" to "#550000", "fontSize" to 14, "fontStyle" to "italic")), "log-list" to _pS(_uM("marginTop" to 8, "paddingTop" to 10, "paddingRight" to 10, "paddingBottom" to 10, "paddingLeft" to 10, "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")), "log-item" to _pS(_uM("fontSize" to 14, "marginTop" to 4, "marginRight" to 0, "marginBottom" to 4, "marginLeft" to 0)))
            }
        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()
    }
}
