package com.dong.demo013.debug

/**
 * 调试用深层数据：根为 10 个普通变量 + 10 个复杂对象；沿主路径嵌套 **>=10 层** [BranchLayer]，
 * 每一层 [BranchLayer] / [LeafLayer] 均含 **10 个普通字段 + 10 个复杂子对象**。
 *
 * 说明：若每层 10 个子树全部满深度展开，节点数为指数级，无法在源码/内存中构造。
 * 此处采用「主轴 n0 逐级加深 + 同层 n1..n9 为已达最深规格的 [LeafLayer]」的写法，
 * 在调试器里仍可展开到 >=10 层，且每层都能看到 10+10 字段。
 */

sealed class ComplexNode

/** 最底层复杂对象：仅普通变量，不再挂子树（视作该层 10 个「终端复杂对象」） */
data class TerminalComplex(
    val i0: Int, val i1: Int, val i2: Int, val i3: Int, val i4: Int,
    val i5: Int, val i6: Int, val i7: Int, val i8: Int, val i9: Int,
    val s0: String, val s1: String, val s2: String, val s3: String, val s4: String,
    val s5: String, val s6: String, val s7: String, val s8: String, val s9: String,
)

/** 第 11 层（索引 10）：10 普通 + 10 个 [TerminalComplex] */
data class LeafLayer(
    val p0: Int, val p1: Int, val p2: Int, val p3: Int, val p4: Int,
    val p5: Int, val p6: Int, val p7: Int, val p8: Int, val p9: Int,
    val q0: String, val q1: String, val q2: String, val q3: String, val q4: String,
    val q5: String, val q6: String, val q7: String, val q8: String, val q9: String,
    val t0: TerminalComplex,
    val t1: TerminalComplex,
    val t2: TerminalComplex,
    val t3: TerminalComplex,
    val t4: TerminalComplex,
    val t5: TerminalComplex,
    val t6: TerminalComplex,
    val t7: TerminalComplex,
    val t8: TerminalComplex,
    val t9: TerminalComplex,
) : ComplexNode()

/** 第 0..9 层：10 普通 + 10 个 [ComplexNode] 子对象 */
data class BranchLayer(
    val p0: Int, val p1: Int, val p2: Int, val p3: Int, val p4: Int,
    val p5: Int, val p6: Int, val p7: Int, val p8: Int, val p9: Int,
    val q0: String, val q1: String, val q2: String, val q3: String, val q4: String,
    val q5: String, val q6: String, val q7: String, val q8: String, val q9: String,
    val n0: ComplexNode,
    val n1: ComplexNode,
    val n2: ComplexNode,
    val n3: ComplexNode,
    val n4: ComplexNode,
    val n5: ComplexNode,
    val n6: ComplexNode,
    val n7: ComplexNode,
    val n8: ComplexNode,
    val n9: ComplexNode,
) : ComplexNode()

/** 根：10 个普通变量 + 10 个复杂根对象（各自一棵深度 >=10 的子树） */
data class DebugTenLevelRoot(
    val plainInt0: Int,
    val plainInt1: Int,
    val plainInt2: Int,
    val plainInt3: Int,
    val plainInt4: Int,
    val plainInt5: Int,
    val plainInt6: Int,
    val plainInt7: Int,
    val plainInt8: Int,
    val plainInt9: Int,
    val plainStr0: String,
    val plainStr1: String,
    val plainStr2: String,
    val plainStr3: String,
    val plainStr4: String,
    val plainStr5: String,
    val plainStr6: String,
    val plainStr7: String,
    val plainStr8: String,
    val plainStr9: String,
    val complex0: ComplexNode,
    val complex1: ComplexNode,
    val complex2: ComplexNode,
    val complex3: ComplexNode,
    val complex4: ComplexNode,
    val complex5: ComplexNode,
    val complex6: ComplexNode,
    val complex7: ComplexNode,
    val complex8: ComplexNode,
    val complex9: ComplexNode,
)

object DebugTenLevelData {

    /** 供断点观察的完整样本 */
    val sample: DebugTenLevelRoot = buildDebugTenLevelRoot()

    /**
     * 打印示例（控制台 / Logcat）：
     * ```
     * DebugTenLevelData.printSampleExampleToConsole()
     * ```
     * 也可直接 `println(DebugTenLevelData.sample)`（整棵 data class 字符串会非常长）。
     */
    fun printSampleExampleToConsole() {
        val root = sample
        println("--- DebugTenLevelRoot 普通变量 ---")
        println("plainInt0=${root.plainInt0} .. plainInt9=${root.plainInt9}")
        println("plainStr0=${root.plainStr0} .. plainStr9=${root.plainStr9}")
        println("--- 主轴 complex0 -> n0 -> n0 ... -> LeafLayer（每层只跟 n0）---")
        printNodeChain(root.complex0, depthLabel = 0)
        println("--- 任一叶上终端对象示例 complex0...Leaf.t0 ---")
        println(leafTerminalPreview(root.complex0))
    }

    fun buildDebugTenLevelRoot(): DebugTenLevelRoot = DebugTenLevelRoot(
        plainInt0 = 1000, plainInt1 = 1001, plainInt2 = 1002, plainInt3 = 1003, plainInt4 = 1004,
        plainInt5 = 1005, plainInt6 = 1006, plainInt7 = 1007, plainInt8 = 1008, plainInt9 = 1009,
        plainStr0 = "r0", plainStr1 = "r1", plainStr2 = "r2", plainStr3 = "r3", plainStr4 = "r4",
        plainStr5 = "r5", plainStr6 = "r6", plainStr7 = "r7", plainStr8 = "r8", plainStr9 = "r9",
        complex0 = branchLayer(depth = 0, treeSeed = 0),
        complex1 = branchLayer(depth = 0, treeSeed = 1),
        complex2 = branchLayer(depth = 0, treeSeed = 2),
        complex3 = branchLayer(depth = 0, treeSeed = 3),
        complex4 = branchLayer(depth = 0, treeSeed = 4),
        complex5 = branchLayer(depth = 0, treeSeed = 5),
        complex6 = branchLayer(depth = 0, treeSeed = 6),
        complex7 = branchLayer(depth = 0, treeSeed = 7),
        complex8 = branchLayer(depth = 0, treeSeed = 8),
        complex9 = branchLayer(depth = 0, treeSeed = 9),
    )
}

private fun printNodeChain(node: ComplexNode, depthLabel: Int) {
    val pad = "  ".repeat(depthLabel)
    when (node) {
        is BranchLayer -> {
            println("${pad}[$depthLabel] BranchLayer p0=${node.p0} q0=${node.q0} | n1..n9 -> LeafLayer（略）")
            printNodeChain(node.n0, depthLabel + 1)
        }
        is LeafLayer -> {
            println("${pad}[$depthLabel] LeafLayer p0=${node.p0} q0=${node.q0}")
            println("${pad}    t0: i0=${node.t0.i0} s0=${node.t0.s0} | t1.i0=${node.t1.i0} ...")
        }
    }
}

private fun leafTerminalPreview(node: ComplexNode): String {
    var cur: ComplexNode = node
    while (cur is BranchLayer) {
        cur = cur.n0
    }
    return when (cur) {
        is LeafLayer -> "Leaf.t0 = TerminalComplex(i0=${cur.t0.i0}, s0=${cur.t0.s0})"
        else -> cur.toString()
    }
}

private const val MAX_BRANCH_DEPTH = 9

private fun branchLayer(depth: Int, treeSeed: Int): ComplexNode {
    if (depth > MAX_BRANCH_DEPTH) {
        return leafLayer(depth = depth, treeSeed = treeSeed)
    }
    val base = treeSeed * 10_000 + depth * 100
    val p = Array(10) { idx -> base + idx }
    val q = Array(10) { idx -> "L${depth}_s${treeSeed}_$idx" }
    val deeper = branchLayer(depth = depth + 1, treeSeed = treeSeed)
    return BranchLayer(
        p0 = p[0], p1 = p[1], p2 = p[2], p3 = p[3], p4 = p[4],
        p5 = p[5], p6 = p[6], p7 = p[7], p8 = p[8], p9 = p[9],
        q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3], q4 = q[4],
        q5 = q[5], q6 = q[6], q7 = q[7], q8 = q[8], q9 = q[9],
        n0 = deeper,
        n1 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 1),
        n2 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 2),
        n3 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 3),
        n4 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 4),
        n5 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 5),
        n6 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 6),
        n7 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 7),
        n8 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 8),
        n9 = leafLayer(depth = MAX_BRANCH_DEPTH + 1, treeSeed = treeSeed * 10 + 9),
    )
}

private fun leafLayer(depth: Int, treeSeed: Int): LeafLayer {
    val base = treeSeed * 1_000 + depth * 50
    val p = Array(10) { idx -> base + idx }
    val q = Array(10) { idx -> "leaf_d${depth}_t${treeSeed}_$idx" }
    return LeafLayer(
        p0 = p[0], p1 = p[1], p2 = p[2], p3 = p[3], p4 = p[4],
        p5 = p[5], p6 = p[6], p7 = p[7], p8 = p[8], p9 = p[9],
        q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3], q4 = q[4],
        q5 = q[5], q6 = q[6], q7 = q[7], q8 = q[8], q9 = q[9],
        t0 = terminalComplex(treeSeed * 100 + 0),
        t1 = terminalComplex(treeSeed * 100 + 1),
        t2 = terminalComplex(treeSeed * 100 + 2),
        t3 = terminalComplex(treeSeed * 100 + 3),
        t4 = terminalComplex(treeSeed * 100 + 4),
        t5 = terminalComplex(treeSeed * 100 + 5),
        t6 = terminalComplex(treeSeed * 100 + 6),
        t7 = terminalComplex(treeSeed * 100 + 7),
        t8 = terminalComplex(treeSeed * 100 + 8),
        t9 = terminalComplex(treeSeed * 100 + 9),
    )
}

private fun terminalComplex(seed: Int): TerminalComplex {
    val b = seed * 11
    return TerminalComplex(
        i0 = b, i1 = b + 1, i2 = b + 2, i3 = b + 3, i4 = b + 4,
        i5 = b + 5, i6 = b + 6, i7 = b + 7, i8 = b + 8, i9 = b + 9,
        s0 = "tc_${seed}_0", s1 = "tc_${seed}_1", s2 = "tc_${seed}_2", s3 = "tc_${seed}_3", s4 = "tc_${seed}_4",
        s5 = "tc_${seed}_5", s6 = "tc_${seed}_6", s7 = "tc_${seed}_7", s8 = "tc_${seed}_8", s9 = "tc_${seed}_9",
    )
}

// 使用示例：var xx = DebugTenLevelData.sample
// 若不需要改引用，更推荐：val xx = DebugTenLevelData.sample
