// 定义 ObservableObject 基类
class ObservableObject {
  protected listeners: { [propertyName: string]: (() => void)[] } = {};

  public addPropertyChangedListener(propertyName: string, callback: () => void) {
    if (!this.listeners[propertyName]) {
      this.listeners[propertyName] = [];
    }
    this.listeners[propertyName].push(callback);
  }

  protected RaisePropertyChanged(propertyName: string) {
    const callbacks = this.listeners[propertyName];
    if (callbacks) {
      callbacks.forEach((callback) => callback());
    }
  }
}

// 定义 TreeViewLeaf 类
class TreeViewLeaf {
  public TradeName: string = "";
  public MenuCode: string = "";
  public Action: string = "";
}

// 定义 BaseCommand 类
class BaseCommand {
  constructor(private action: (obj: any) => void) {}

  public execute(obj: any) {
    this.action(obj);
  }
}

// 定义 LeafViewModel 类
export class LeafViewModel extends ObservableObject {
  public Leaf: TreeViewLeaf;
  public OpenTradeCommand: BaseCommand;

  private _isChecked: boolean = false;

  constructor(leaf: TreeViewLeaf) {
    super();
    this.Leaf = leaf;
    this.OpenTradeCommand = new BaseCommand(this.OpenTradeAction.bind(this));
  }

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

  set TradeName(value: string) {
    this.Leaf.TradeName = value;
    this.RaisePropertyChanged("TradeName");
  }

  get MenuCode(): string {
    return this.Leaf.MenuCode;
  }

  set MenuCode(value: string) {
    this.Leaf.MenuCode = value;
    this.RaisePropertyChanged("MenuCode");
  }

  get DiscribleText(): string {
    return `${this.Leaf.MenuCode}--${this.Leaf.TradeName}`;
  }

  get Action(): string {
    return this.Leaf.Action;
  }

  set Action(value: string) {
    this.Leaf.Action = value;
    this.RaisePropertyChanged("Action");
  }

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

  set IsChecked(value: boolean) {
    this._isChecked = value;
    this.RaisePropertyChanged("IsChecked");
  }

  public OpenTradeAction(obj: any) {
    const actionId = this.Action;
  }
}