package com.dong.demo013.debug

/**
 * 调试用深层数据：根为 **7** 个整型 + **7** 个字符串 + 5 个复杂根；沿主路径连续 **7 层** [BranchLayer]，
 * 再进入 [LeafLayer]；每层 [BranchLayer] / [LeafLayer] 为 **7 整型 + 7 字符串 + 5 个复杂子**（侧叶为叶层）。
 * [TerminalComplex] 内基础变量亦为 **7 整型 + 7 字符串**（与层数一致）。
 *
 * 说明：同层子树若全部满深度展开为指数级，故采用「主轴 n0 逐级加深 + 同层 n1..n4 为 [LeafLayer]」。
 */

sealed class ComplexNode

/** 最底层复杂对象：仅基础变量（7 整型 + 7 字符串），与主轴层数一致 */
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 s0: String, val s1: String, val s2: String, val s3: String, val s4: String, val s5: String, val s6: String,
)

/** 主轴叶层（depth=7）：7 整型 + 7 字符串 + 5 个 [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 q0: String, val q1: String, val q2: String, val q3: String, val q4: String, val q5: String, val q6: String,
    val t0: TerminalComplex,
    val t1: TerminalComplex,
    val t2: TerminalComplex,
    val t3: TerminalComplex,
    val t4: TerminalComplex,
) : ComplexNode()

/** 第 0..6 层（共 7 层）：7 整型 + 7 字符串 + 5 个 [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 q0: String, val q1: String, val q2: String, val q3: String, val q4: String, val q5: String, val q6: String,
    val n0: ComplexNode,
    val n1: ComplexNode,
    val n2: ComplexNode,
    val n3: ComplexNode,
    val n4: ComplexNode,
) : ComplexNode()

/** 根：7 个整型 + 7 个字符串 + 5 个复杂根（各自主轴 7 层分支 + 叶） */
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 plainStr0: String,
    val plainStr1: String,
    val plainStr2: String,
    val plainStr3: String,
    val plainStr4: String,
    val plainStr5: String,
    val plainStr6: String,
    val complex0: ComplexNode,
    val complex1: ComplexNode,
    val complex2: ComplexNode,
    val complex3: ComplexNode,
    val complex4: 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} .. plainInt6=${root.plainInt6}")
        println("plainStr0=${root.plainStr0} .. plainStr6=${root.plainStr6}")
        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,
        plainStr0 = "r0", plainStr1 = "r1", plainStr2 = "r2", plainStr3 = "r3",
        plainStr4 = "r4", plainStr5 = "r5", plainStr6 = "r6",
        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),
    )
}

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..n4 -> 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()
    }
}

/** 主轴 [BranchLayer] 最大 depth（含）；depth 7 起为 [LeafLayer] */
private const val MAX_BRANCH_DEPTH = 6

/** 每层 / 根 / [TerminalComplex] 的基础整型、字符串个数，与主轴层数（MAX_BRANCH_DEPTH+1）一致 */
private const val PLAIN_FIELD_COUNT = MAX_BRANCH_DEPTH + 1

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 = IntArray(PLAIN_FIELD_COUNT) { idx -> base + idx }
    val q = Array(PLAIN_FIELD_COUNT) { 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],
        q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3], q4 = q[4], q5 = q[5], q6 = q[6],
        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),
    )
}

private fun leafLayer(depth: Int, treeSeed: Int): LeafLayer {
    val base = treeSeed * 1_000 + depth * 50
    val p = IntArray(PLAIN_FIELD_COUNT) { idx -> base + idx }
    val q = Array(PLAIN_FIELD_COUNT) { 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],
        q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3], q4 = q[4], q5 = q[5], q6 = q[6],
        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),
    )
}

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,
        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",
    )
}

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