// 定义节点选中状态类型
type CheckedState = boolean | null;

// 定义路由事件类型
type RoutedEvent = string;

// 定义事件处理函数类型
type EventHandler = (event: RoutedEvent) => void;

// 定义 TreeViewListItem 类
class TreeViewListItem {
    private _level = -1;
    private _children: TreeViewListItem[] = [];
    private _parent: TreeViewListItem | null = null;
    private _isChecked: CheckedState = false;
    private _checkBoxVisibility: boolean = false;
    private _nodeLevel: number = -1;
    private _parentLevel: number = -1;
    private _styleNumber: number = 0;
    private _sortIndex: string = '';
    private _descriptionName: string = '';
    private _multsource: TreeViewListItem[] = [];

    // 事件处理函数集合
    private eventHandlers: { [key: string]: EventHandler[] } = {};

    constructor() {
        // 初始化时可添加事件监听逻辑
    }

    // 注册事件处理函数
    public on(event: RoutedEvent, handler: EventHandler) {
        if (!this.eventHandlers[event]) {
            this.eventHandlers[event] = [];
        }
        this.eventHandlers[event].push(handler);
    }

    // 触发事件
    private raiseEvent(event: RoutedEvent) {
        const handlers = this.eventHandlers[event];
        if (handlers) {
            handlers.forEach(handler => handler(event));
        }
    }

    // 引发路由事件
    private raiseRoutedEvent(source: any, routedEvent: RoutedEvent) {
        // 由于无法直接判断 TemplatedParent 是否为 ToggleButton，简单跳过
        this.raiseEvent(routedEvent);
    }

    // 获取子项容器
    public getContainerForItemOverride() {
        const li = new TreeViewListItem();
        li.setParent(this);
        this._children.push(li);
        return li;
    }

    // 判断项是否为自身容器
    public isItemItsOwnContainerOverride(item: any) {
        return item instanceof TreeViewListItem;
    }

    // 设置父节点
    public setParent(parent: TreeViewListItem) {
        this._parent = parent;
    }

    // 获取层次等级
    public get level() {
        if (this._level === -1) {
            this._level = this._parent ? this._parent.level + 1 : 0;
        }
        return this._level;
    }

    // 获取父节点的子集合
    public get parentItemsSource() {
        return this._parent?._children || [];
    }

    // 获取和设置复选框是否选中
    public static getIsChecked(item: TreeViewListItem) {
        return item._isChecked;
    }

    public static setIsChecked(item: TreeViewListItem, value: CheckedState) {
        item._isChecked = value;
    }

    // 复选框选中状态改变时的处理
    private static onIsCheckedChanged(item: TreeViewListItem, value: CheckedState) {
        item.isUpdateChecked(item, value, true, true);
    }

    // 设置是否选中，并更新父节点和子节点
    private isUpdateChecked(d: TreeViewListItem, value: CheckedState, updateChildren: boolean, updateParent: boolean) {
        TreeViewListItem.setIsChecked(d, value);

        // 更新子节点状态
        if (updateChildren && value !== null) {
            this._children.forEach(child => {
                TreeViewListItem.setIsChecked(child, value);
                child.isUpdateChecked(child, value, true, false);
            });
        }

        // 更新父节点状态
        if (updateParent && this._parent) {
            this._parent.verifyCheckState();
        }

        // 引发属性改变事件
        this.raiseEvent('PropertyChanged_IsChecked');
    }

    // 根据子节点状态来设置父节点的状态
    private verifyCheckState() {
        let state: CheckedState = null;

        for (let i = 0; i < this._children.length; i++) {
            const current = TreeViewListItem.getIsChecked(this._children[i]);
            if (i === 0) {
                state = current;
            } else if (state!== current) {
                state = null;
                break;
            }
        }

        // 更新父节点
        this.isUpdateChecked(this, state, false, true);
    }

    // 鼠标左键单击事件处理
    public onPreviewMouseLeftButtonDown() {
        this.raiseRoutedEvent(this, 'MouseLeftClickItemEvent');
    }

    // 鼠标右键单击事件处理
    public onPreviewMouseRightButtonDown() {
        this.raiseRoutedEvent(this, 'MouseRightClickItemEvent');
    }

    // 鼠标双击事件处理
    public onPreviewMouseDoubleClick() {
        this.raiseRoutedEvent(this, 'MouseDoubleClickItemEvent');
    }

    // 重写 ToString 方法
    public toString() {
        return this._descriptionName || '';
    }

    // 其他属性的 get 和 set 方法
    public get nodeLevel() {
        return this._nodeLevel;
    }

    public set nodeLevel(value: number) {
        if (value < -1) {
            throw new Error('节点级别的值必须大于等于 -1！');
        }
        this._nodeLevel = value;
    }

    public get parentLevel() {
        return this._parentLevel;
    }

    public set parentLevel(value: number) {
        this._parentLevel = value;
    }

    public get styleNumber() {
        return this._styleNumber;
    }

    public set styleNumber(value: number) {
        this._styleNumber = value;
    }

    public get sortIndex() {
        return this._sortIndex;
    }

    public set sortIndex(value: string) {
        this._sortIndex = value;
    }

    public get descriptionName() {
        return this._descriptionName;
    }

    public set descriptionName(value: string) {
        this._descriptionName = value;
    }

    public get multsource() {
        return this._multsource;
    }

    public set multsource(value: TreeViewListItem[]) {
        this._multsource = value;
    }
}

// 定义 TreeViewListItemCategory 类
class TreeViewListItemCategory extends TreeViewListItem {
    constructor() {
        super();
    }
}

export { TreeViewListItem, TreeViewListItemCategory };