import { Widget } from '@lumino/widgets';
import { ISignal, Signal } from '@lumino/signaling';

interface ISwitchSignalData {
  type: string;
}

export default class DirectorySwitcher extends Widget {
  constructor(options: Widget.IOptions = {}) {
    super(options);
    this._createButtons();
    this._addClickEventListener();
  }

  public get switchButtonClick(): ISignal<
    DirectorySwitcher,
    ISwitchSignalData
  > {
    return this._clickSwitched;
  }

  private _createButtons() {
    const btn1 = document.createElement('button');
    btn1.textContent = '开发目录';
    btn1.classList.add('jldbq-btn', 'jldbq-btn-current');
    this.node.appendChild(btn1);
    const btn2 = document.createElement('button');
    btn2.textContent = '数据管理';
    btn2.classList.add('jldbq-btn');
    this.node.appendChild(btn2);
  }

  private _addClickEventListener() {
    this.node.childNodes.forEach((childNode: Element) => {
      childNode.addEventListener('click', () => {
        this.node.childNodes.forEach((item: Element) => {
          item.setAttribute('class', 'jldbq-btn');
        });
        childNode.classList.add('jldbq-btn-current');
        this._clickSwitched.emit({ type: childNode.innerHTML });
      });
    });
  }

  private _clickSwitched = new Signal<DirectorySwitcher, ISwitchSignalData>(
    this
  );
}
