import { MainStateManager } from '../../MainStateManager';
import { TabbingControl } from '../TabbingControl';
import { ValidatingControl } from '../ValidatingControl';
import { ChangingControl } from '../ChangingControl';
import { BasePageData, PropsOfPage } from '../BasePageData';
import { TouchingControl } from '../TouchingControl';
import { ModalPageData } from '../Modal/ModalData';
import { PopupPageData, MouseLocationProps, TLocationPopup } from '../Modal/ModalPopup';
import { IArmisaPageKey } from '../../ArmisaImportPage';
import { MainStacksFactory } from '../../ModalOfStack/Main';
import { StackFactory } from '../../ModalOfStack/Stack';
import { INaming } from '../../NamingCaption';

export class TabPageData extends BasePageData {
  public TouchingControl: TouchingControl = new TouchingControl(this);
  public TabbingControl: TabbingControl = new TabbingControl(this);
  public ValidatingControl: ValidatingControl = new ValidatingControl(this);
  public ChangingControl: ChangingControl = new ChangingControl(this);

  public get any(): any {
    return this;
  }

  protected _modal?: ModalPageData;
  public get modal(): ModalPageData | undefined {
    return this._modal;
  }
  public set modal(value: ModalPageData | undefined) {
    this._modal = value;
  }

  protected _popup?: PopupPageData;
  public get popup(): PopupPageData | undefined {
    return this._popup;
  }
  public set popup(value: PopupPageData | undefined) {
    this._popup = value;
  }


  public get hasChange(): boolean {
    return this.ChangingControl.controls.length > 0;
  }
  public updateHasChange = () => {
    this.Eventing.trigger('form.hasChangeOnTabs');
  };

  constructor(public mainStateManager: MainStateManager) {
    super(mainStateManager);
  }

  onClickCancelButton() { }
  onClickHelpButton() { }

  setHelpElementRef() { }
  selectThisPage = () => {
    // this.mainStateManager.Tabing.selectThisTab(this);
  };
  closeThisPage = () => {
    // this.mainStateManager.Tabing.closeThisTab(this);
  };

  showModal = (component: JSX.Element) => {
    this.modal = new ModalPageData(this.mainStateManager, this);
    this.modal.component = component;
    // this.mainStateManager.Modaling.addModal(this.modal);
  };
  showModalPage = (pageKey: IArmisaPageKey | undefined, props: PropsOfPage, isMainOfStacks?: boolean) => {
    if (pageKey) {
      this.modal = new ModalPageData(this.mainStateManager, this);
      this.modal.pageKey = pageKey;
      this.modal.props = props;
      this.modal.isMainOfStacks = isMainOfStacks;
      // this.mainStateManager.Modaling.addModalPage(this.modal);
    }
  };

  showPopup = (component: JSX.Element, mouseLocation?: MouseLocationProps | React.MouseEvent, location?: TLocationPopup, byArrow?: boolean) => {
    this.popup = new PopupPageData(this.mainStateManager, this, mouseLocation, location, byArrow);
    this.popup.component = component;
    // this.mainStateManager.Modaling.addPopup(this.popup);
  };
  showPopupPage = (pageKey: IArmisaPageKey | undefined, props: PropsOfPage) => {
    if (pageKey) {
      this.popup = new PopupPageData(this.mainStateManager, this);
      this.popup.pageKey = pageKey;
      this.popup.props = props;
      // this.mainStateManager.Modaling.addPopupPage(this.popup);
    }
  };

  closeModal = () => {
    if (this._modal) {
      // this.mainStateManager.Modaling.closeThisModal(this._modal);
      this._modal = undefined;
    }
  };

  closePopup = () => {
    if (this._popup) {
      // this.mainStateManager.Modaling.closeThisPopup(this._popup);
      this._popup = undefined;
    }
  };


  addNewStack(pageKey: IArmisaPageKey, props?: PropsOfPage, caption?: INaming) {
    if (this.mainStacksFactory instanceof MainStacksFactory) {
      const newTab = new TabPageData(this.mainStateManager);
      const stackFactory = new StackFactory(newTab, this.mainStacksFactory!);

      newTab.pageKey = pageKey;
      newTab.props = props;
      if (caption) {
        newTab.caption = this.mainStateManager.getCaptionNaming(caption);
      } else {
        // newTab.caption = this.mainStateManager.MenuIteming.getCaptionOfPage(newTab.pageKey);
      }
      this.mainStacksFactory.stacks.push(stackFactory);
      this.mainStacksFactory.currentStack = stackFactory;
      this.mainStacksFactory.trigger('stack.add.new', newTab);
    }
  }
}
