// 定义 TreeViewRoot 和 TreeViewNode 类型
interface TreeViewRoot {
    TradeName: string;
    listNode: TreeViewNode[];
}

interface TreeViewNode {
    // 这里可根据实际情况补充 TreeViewNode 的属性
}

// 定义 NodeViewModel 类型
class NodeViewModel {
    IsChecked: boolean;
    constructor(node: TreeViewNode) {
        this.IsChecked = false;
    }
}

// 定义 ObservableObject 类
class ObservableObject {
    protected propertyChangedHandlers: { [key: string]: (() => void)[] } = {};

    RaisePropertyChanged(propertyName: string) {
        const handlers = this.propertyChangedHandlers[propertyName];
        if (handlers) {
            handlers.forEach(handler => handler());
        }
    }

    onPropertyChanged(propertyName: string, handler: () => void) {
        if (!this.propertyChangedHandlers[propertyName]) {
            this.propertyChangedHandlers[propertyName] = [];
        }
        this.propertyChangedHandlers[propertyName].push(handler);
    }
}

// 定义 RootViewModel 类
export class RootViewModel extends ObservableObject {
    Item: TreeViewRoot;
    private nodeCollection: NodeViewModel[];

    get NodeCollection(): NodeViewModel[] {
        return this.nodeCollection;
    }

    set NodeCollection(value: NodeViewModel[]) {
        this.nodeCollection = value;
        this.RaisePropertyChanged('NodeCollection');
    }

    get TradeName(): string {
        return this.Item.TradeName;
    }

    set TradeName(value: string) {
        this.Item.TradeName = value;
        this.RaisePropertyChanged('TradeName');
    }

    get NodeImage(): string {
        return 'Images/root.gif';
    }

    private isChecked: boolean;
    get IsChecked(): boolean {
        return this.isChecked;
    }

    set IsChecked(value: boolean) {
        this.isChecked = value;
        this.RaisePropertyChanged('IsChecked');
        this.OnCheckChanged();
    }

    OnCheckChanged() {
        this.NodeCollection.forEach(leafViewModel => {
            leafViewModel.IsChecked = this.IsChecked;
        });
    }

    constructor(item: TreeViewRoot) {
        super();
        this.Item = item;
        this.NodeCollection = [];

        item.listNode.forEach(node => {
            this.NodeCollection.push(new NodeViewModel(node));
        });
    }
}