package com.dong.demo013.debug

/**
 * 十层调试样本（仅本文件，类型名与 `DebugTenLevelData.kt` 不重复）。
 *
 * - **10 层**（`levelIndex` 0..9），第 9 层为 [TenSampleFloorLeaf]。
 * - 每层：**10 个基础 Int**（`v0..v9`）+ **10 个复杂子节点**（`child0..child9` / `atom0..atom9`）。
 * - **child0**（或主链入口）：通向 **下一整层**；**child1..child9**：侧枝，深度 **1～5**（由层号+槽位算出的稳定值，等价「随机」）。
 */

sealed class TenSampleNode

/** 终端复杂对象：无再嵌套 */
data class TenSampleAtom(
    val a0: Int, val a1: Int, val a2: Int, val a3: Int, val a4: Int,
    val a5: Int, val a6: Int, val a7: Int, val a8: Int, val a9: Int,
    val t0: String, val t1: String, val t2: String, val t3: String, val t4: String,
    val t5: String, val t6: String, val t7: String, val t8: String, val t9: String,
)

/**
 * 第 9 层（最后一层）：10 个基础 Int + 10 个复杂终端。
 */
data class TenSampleFloorLeaf(
    val levelIndex: Int,
    val v0: Int, val v1: Int, val v2: Int, val v3: Int, val v4: Int,
    val v5: Int, val v6: Int, val v7: Int, val v8: Int, val v9: Int,
    val atom0: TenSampleAtom,
    val atom1: TenSampleAtom,
    val atom2: TenSampleAtom,
    val atom3: TenSampleAtom,
    val atom4: TenSampleAtom,
    val atom5: TenSampleAtom,
    val atom6: TenSampleAtom,
    val atom7: TenSampleAtom,
    val atom8: TenSampleAtom,
    val atom9: TenSampleAtom,
) : TenSampleNode()

/**
 * 第 0～8 层：10 个基础 Int + 10 个子节点。
 ****'child0 '进入主链下一层；"child1..child9" 为侧枝（深度 1～5）。
 */
data class TenSampleFloorInternal(
    val levelIndex: Int,
    val v0: Int, val v1: Int, val v2: Int, val v3: Int, val v4: Int,
    val v5: Int, val v6: Int, val v7: Int, val v8: Int, val v9: Int,
    val child0: TenSampleNode,
    val child1: TenSampleNode,
    val child2: TenSampleNode,
    val child3: TenSampleNode,
    val child4: TenSampleNode,
    val child5: TenSampleNode,
    val child6: TenSampleNode,
    val child7: TenSampleNode,
    val child8: TenSampleNode,
    val child9: TenSampleNode,
) : TenSampleNode()

/**
 * 侧枝中间层（深度仍 >1）：10 基础 + child0 继续缩深度，child1..9 为叶。
 */
data class TenSampleSideBranch(
    val sideDepthLeft: Int,
    val sideSeed: Int,
    val v0: Int, val v1: Int, val v2: Int, val v3: Int, val v4: Int,
    val v5: Int, val v6: Int, val v7: Int, val v8: Int, val v9: Int,
    val child0: TenSampleNode,
    val child1: TenSampleNode,
    val child2: TenSampleNode,
    val child3: TenSampleNode,
    val child4: TenSampleNode,
    val child5: TenSampleNode,
    val child6: TenSampleNode,
    val child7: TenSampleNode,
    val child8: TenSampleNode,
    val child9: TenSampleNode,
) : TenSampleNode()

/** 根：10 个基础 Int + 10 棵主入口子树 */
data class TenSampleRoot(
    val v0: Int, val v1: Int, val v2: Int, val v3: Int, val v4: Int,
    val v5: Int, val v6: Int, val v7: Int, val v8: Int, val v9: Int,
    val rootChild0: TenSampleNode,
    val rootChild1: TenSampleNode,
    val rootChild2: TenSampleNode,
    val rootChild3: TenSampleNode,
    val rootChild4: TenSampleNode,
    val rootChild5: TenSampleNode,
    val rootChild6: TenSampleNode,
    val rootChild7: TenSampleNode,
    val rootChild8: TenSampleNode,
    val rootChild9: TenSampleNode,
)

object DebugTenLevelSample {

    const val FLOOR_COUNT = 10
    const val SIDE_DEPTH_MAX = 5

    val sample: TenSampleRoot
        get() = buildRoot()

    fun buildRoot(): TenSampleRoot = TenSampleRoot(
        v0 = 10_000, v1 = 10_001, v2 = 10_002, v3 = 10_003, v4 = 10_004,
        v5 = 10_005, v6 = 10_006, v7 = 10_007, v8 = 10_008, v9 = 10_009,
        rootChild0 = buildMainFloor(levelIndex = 0, treeSeed = 100),
        rootChild1 = buildMainFloor(levelIndex = 0, treeSeed = 101),
        rootChild2 = buildMainFloor(levelIndex = 0, treeSeed = 102),
        rootChild3 = buildMainFloor(levelIndex = 0, treeSeed = 103),
        rootChild4 = buildMainFloor(levelIndex = 0, treeSeed = 104),
        rootChild5 = buildMainFloor(levelIndex = 0, treeSeed = 105),
        rootChild6 = buildMainFloor(levelIndex = 0, treeSeed = 106),
        rootChild7 = buildMainFloor(levelIndex = 0, treeSeed = 107),
        rootChild8 = buildMainFloor(levelIndex = 0, treeSeed = 108),
        rootChild9 = buildMainFloor(levelIndex = 0, treeSeed = 109),
    )

    /**
     * 侧枝深度：1～5，由层号与槽位决定（稳定、可复现）。
     */
    fun sideBranchDepth(levelIndex: Int, slotIndex: Int): Int {
        val x = (levelIndex * 17 + slotIndex * 31 + 13) % SIDE_DEPTH_MAX
        return 1 + x
    }

    fun printSampleExampleToConsole() {
        val root = sample
        println("--- TenSampleRoot v0..v9 ---")
        println("v0=${root.v0} .. v9=${root.v9}")
        println("--- 主链 rootChild0 -> child0 -> ... (10 层) ---")
        printMainSpine(root.rootChild0, 0)
        println("--- 叶上 atom0 预览 ---")
        println(leafAtomPreview(root.rootChild0))
    }
}

private fun printMainSpine(node: TenSampleNode, depth: Int) {
    val pad = "  ".repeat(depth)
    when (node) {
        is TenSampleFloorInternal -> {
            println("${pad}[L${node.levelIndex}] internal v0=${node.v0} | child0->下一层 child1..9侧枝")
            printMainSpine(node.child0, depth + 1)
        }
        is TenSampleFloorLeaf -> {
            println("${pad}[L${node.levelIndex}] leaf v0=${node.v0} atom0.a0=${node.atom0.a0}")
        }
        is TenSampleSideBranch -> {
            println("${pad}[side d=${node.sideDepthLeft}] v0=${node.v0}")
            printMainSpine(node.child0, depth + 1)
        }
    }
}

private fun leafAtomPreview(node: TenSampleNode): String {
    var cur: TenSampleNode = node
    while (true) {
        cur = when (cur) {
            is TenSampleFloorInternal -> cur.child0
            is TenSampleSideBranch -> cur.child0
            is TenSampleFloorLeaf -> return "atom0(a0=${cur.atom0.a0},t0=${cur.atom0.t0})"
        }
    }
}

private fun buildMainFloor(levelIndex: Int, treeSeed: Int): TenSampleNode {
    if (levelIndex >= DebugTenLevelSample.FLOOR_COUNT - 1) {
        return buildFloorLeaf(levelIndex = DebugTenLevelSample.FLOOR_COUNT - 1, treeSeed = treeSeed)
    }
    val base = treeSeed * 1_000 + levelIndex * 100
    val v = Array(10) { idx -> base + idx }
    val nextMain = buildMainFloor(levelIndex = levelIndex + 1, treeSeed = treeSeed)
    return TenSampleFloorInternal(
        levelIndex = levelIndex,
        v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3], v4 = v[4],
        v5 = v[5], v6 = v[6], v7 = v[7], v8 = v[8], v9 = v[9],
        child0 = nextMain,
        child1 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 1),
            seed = treeSeed * 10 + 1,
            levelIndex = levelIndex,
        ),
        child2 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 2),
            seed = treeSeed * 10 + 2,
            levelIndex = levelIndex,
        ),
        child3 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 3),
            seed = treeSeed * 10 + 3,
            levelIndex = levelIndex,
        ),
        child4 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 4),
            seed = treeSeed * 10 + 4,
            levelIndex = levelIndex,
        ),
        child5 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 5),
            seed = treeSeed * 10 + 5,
            levelIndex = levelIndex,
        ),
        child6 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 6),
            seed = treeSeed * 10 + 6,
            levelIndex = levelIndex,
        ),
        child7 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 7),
            seed = treeSeed * 10 + 7,
            levelIndex = levelIndex,
        ),
        child8 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 8),
            seed = treeSeed * 10 + 8,
            levelIndex = levelIndex,
        ),
        child9 = buildSideTree(
            depth = DebugTenLevelSample.sideBranchDepth(levelIndex, 9),
            seed = treeSeed * 10 + 9,
            levelIndex = levelIndex,
        ),
    )
}

/** 侧枝总层数 = [depth]（1～5），深度为 1 时直接叶。 */
private fun buildSideTree(depth: Int, seed: Int, levelIndex: Int): TenSampleNode {
    val d = depth.coerceIn(1, DebugTenLevelSample.SIDE_DEPTH_MAX)
    if (d <= 1) {
        return buildFloorLeaf(levelIndex = levelIndex, treeSeed = seed + 7_000)
    }
    val base = seed * 13 + levelIndex * 3
    val v = Array(10) { idx -> base + idx }
    val next = buildSideTree(depth = d - 1, seed = seed + 97, levelIndex = levelIndex)
    val leaves = Array(9) { k ->
        buildFloorLeaf(levelIndex = levelIndex, treeSeed = seed * 100 + k + 1)
    }
    return TenSampleSideBranch(
        sideDepthLeft = d,
        sideSeed = seed,
        v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3], v4 = v[4],
        v5 = v[5], v6 = v[6], v7 = v[7], v8 = v[8], v9 = v[9],
        child0 = next,
        child1 = leaves[0],
        child2 = leaves[1],
        child3 = leaves[2],
        child4 = leaves[3],
        child5 = leaves[4],
        child6 = leaves[5],
        child7 = leaves[6],
        child8 = leaves[7],
        child9 = leaves[8],
    )
}

private fun buildFloorLeaf(levelIndex: Int, treeSeed: Int): TenSampleFloorLeaf {
    val base = treeSeed * 50 + levelIndex
    val v = Array(10) { idx -> base + idx }
    return TenSampleFloorLeaf(
        levelIndex = levelIndex,
        v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3], v4 = v[4],
        v5 = v[5], v6 = v[6], v7 = v[7], v8 = v[8], v9 = v[9],
        atom0 = buildAtom(treeSeed * 10 + 0),
        atom1 = buildAtom(treeSeed * 10 + 1),
        atom2 = buildAtom(treeSeed * 10 + 2),
        atom3 = buildAtom(treeSeed * 10 + 3),
        atom4 = buildAtom(treeSeed * 10 + 4),
        atom5 = buildAtom(treeSeed * 10 + 5),
        atom6 = buildAtom(treeSeed * 10 + 6),
        atom7 = buildAtom(treeSeed * 10 + 7),
        atom8 = buildAtom(treeSeed * 10 + 8),
        atom9 = buildAtom(treeSeed * 10 + 9),
    )
}

private fun buildAtom(seed: Int): TenSampleAtom {
    val b = seed * 11
    return TenSampleAtom(
        a0 = b, a1 = b + 1, a2 = b + 2, a3 = b + 3, a4 = b + 4,
        a5 = b + 5, a6 = b + 6, a7 = b + 7, a8 = b + 8, a9 = b + 9,
        t0 = "s_${seed}_0", t1 = "s_${seed}_1", t2 = "s_${seed}_2", t3 = "s_${seed}_3", t4 = "s_${seed}_4",
        t5 = "s_${seed}_5", t6 = "s_${seed}_6", t7 = "s_${seed}_7", t8 = "s_${seed}_8", t9 = "s_${seed}_9",
    )
}

// var xx = DebugTenLevelSample.sample
