package com.sybotan.android.demo.tree


@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class TreeNodeId

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class TreeNodePid

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class TreeNodeLabel

class Node<T>(val id: String, val pid: String, val label: String, val data: T) {
    /**
     * 节点层级
     */
    val level: Int
        get() = parent?.let { parent!!.level + 1 } ?: 0

    /**
     * 展开状态
     */
    var isExpand = false
        set(isExpand) {
            field = isExpand

            if (!isExpand) {
                for (node in children) {
                    node.isExpand = false
                }
            }
        }

    var parent: Node<T>? = null

    var children: MutableList<Node<T>> = mutableListOf()

    /**
     * is 根节点
     */
    val isRoot: Boolean
        get() = parent == null

    /**
     * 当前节点父节点是否收缩
     */
    val isParentExpand: Boolean
        get() = parent?.isExpand ?: false

    /**
     * 是否是叶子节点
     *
     * @return
     */
    val isLeaf: Boolean
        get() = children.isEmpty()

}