import System from '@/Engine/ECS/System';
import { PAGE_KEY } from '@/Project/constants';
import { getDvaApp } from 'umi';

class SysRouter extends System {
  dispatch: any;
  constructor(name: string) {
    super(name);
    console.log('init SysPage');
    this.dispatch = getDvaApp()._store.dispatch;
  }

  /**
   * 新增界面
   * @param key 页面的唯一key
   * @param params 页面的参数
   */
  add(key: PAGE_KEY, params?: any) {
    this.dispatch({
      type: 'page/add',
      payload: { key, params },
    });
  }
  // 删除选中页面
  remove() {
    this.dispatch({
      type: 'page/remove',
      payload: {},
    });
  }
  // 根据pageId删除页面
  removeById(id?: number) {
    this.dispatch({
      type: 'page/remove',
      payload: { id },
    });
  }
  // 根据pageId删除页面
  removeByKey(key?: PAGE_KEY) {
    this.dispatch({
      type: 'page/removeByKey',
      payload: { key },
    });
  }

  /**
   * 跳转界面
   * @param key 新界面的唯一key
   * @param params 新界面的参数
   * @param id 待跳转的pageID
   */
  push(key: string, params?: any, id?: number) {
    this.dispatch({
      type: 'page/replace',
      payload: { key, params, id },
    });
  }
  /**
   * 返回界面
   * @param id 待返回的pageID
   */
  back(id?: number) {
    this.dispatch({
      type: 'page/back',
      payload: { id },
    });
  }
  /**
   * 界面最小化
   * @param index 界面的index
   * @param options 界面的配置信息
   */
  zoomOut(index: number, options: GT.IPageOptions) {
    this.dispatch({
      type: 'page/zoomOut',
      payload: { index, options },
    });
  }
  /**
   * 最大化
   * @param index 界面的index
   */
  zoomIn(index: number) {
    this.dispatch({
      type: 'page/zoomIn',
      payload: { index },
    });
  }
}

export default new SysRouter('SysRouter');
