package com.dong.nocomposedemo
import platform.PerformanceAnalysisKit.HiLog.OH_LOG_PrintMsg
import kotlin.experimental.ExperimentalNativeApi


@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.experimental.ExperimentalNativeApi::class)
@CName("hiLogPrintMsg")
fun hiLogPrintMsg(type: UInt, level: UInt, domain: UInt, tag: String, message: String): Int {
    return try {
        OH_LOG_PrintMsg(type, level, domain, tag, message)
    } catch (_: Throwable) {
        -1
    }

}


@OptIn(ExperimentalNativeApi::class)
@CName("pirntSingleHiLog")
actual fun pirntSingleHiLog(){
    OH_LOG_PrintMsg(0u, 3u, 4660u, "TestTag", "单次日志打印");

}

@OptIn(ExperimentalNativeApi::class)
@CName("pirntAllLevelHiLog")
actual fun pirntAllLevelHiLog(){
// 输出 DEBUG 级别
    // LOG_APP = 0 (应用日志类型)
    // LOG_DEBUG = 3 (调试级别)
    // domain = 0x1234 (任意自定义值，范围0x0000-0xFFFF)
    // tag = "TestTag" (自定义标签)
    OH_LOG_PrintMsg(0u, 3u, 4660u, "TestTag", "This is a DEBUG log from PrintMsg");

    // 输出 INFO 级别
    // LOG_INFO = 4 (信息级别)
    OH_LOG_PrintMsg(0u, 4u, 4660u, "TestTag", "This is an INFO log from PrintMsg");

    // 输出 WARN 级别
    // LOG_WARN = 5 (警告级别)
    OH_LOG_PrintMsg(0u, 5u, 4660u, "TestTag", "This is a WARN log from PrintMsg");

    // 输出 ERROR 级别
    // LOG_ERROR = 6 (错误级别)
    OH_LOG_PrintMsg(0u, 6u, 4660u, "TestTag", "This is an ERROR log from PrintMsg");

    // 输出 FATAL 级别
    // LOG_FATAL = 7 (致命级别)
    OH_LOG_PrintMsg(0u, 7u, 4660u, "TestTag", "This is a FATAL log from PrintMsg");
}

@OptIn(ExperimentalNativeApi::class)
@CName("pirntRandomHiLog")
actual fun pirntRandomHiLog() {
    val levels = listOf(3u, 4u, 5u, 6u, 7u) // DEBUG, INFO, WARN, ERROR, FATAL
    val randomLevel = levels.random()
    OH_LOG_PrintMsg(0u, randomLevel, 4660u, "TestTag", "随机日志：级别=$randomLevel")
}

@OptIn(ExperimentalNativeApi::class)
@CName("FaultOut")
actual fun FaultOut() {
    val array = intArrayOf(1, 2, 3)
    // 故意访问越界索引，触发崩溃
    println(array[5])
    
    // 备用崩溃方式：空指针异常
    val nullStr: String? = null
    println(nullStr!!.length)
}