import { IBizContext } from '@ibiz-template/core';
import { IPSAppView } from '@ibiz-template/model';
import {
  IModal,
  IModalData,
  IOpenViewUtil,
  ViewMode,
} from '@ibiz-template/runtime';
import { generateRoutePath } from '@ibiz-template/vue-util';
import router from '@/router';
import {
  getDrawerPlacement,
  openViewDrawer,
  openViewModal,
  openViewPopover,
} from '../overlay-view-util/overlay-view-util';

/**
 * 打开视图方式工具类
 *
 * @description 此实现类挂载在 ibiz.openViewUtil
 * @author chitanda
 * @date 2022-08-16 20:08:54
 * @export
 * @class OpenViewUtil
 * @implements {IOpenViewUtil}
 */
export class OpenViewUtil implements IOpenViewUtil {
  root(
    appView: IPSAppView,
    context?: IBizContext | undefined,
    params?: IParams | undefined,
  ): void {
    const path = generateRoutePath(
      appView,
      router.currentRoute,
      context,
      params,
    );

    router.push({ path });
  }

  /**
   * 模态打开视图
   *
   * @author lxm
   * @date 2022-09-12 01:09:06
   * @param {IPSAppView} appView
   * @param {(IBizContext | undefined)} [context]
   * @param {(IParams | undefined)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async modal(
    appView: IPSAppView,
    context?: IBizContext | undefined,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    // 获取视图path
    const modelPath = appView.modelPath!;

    // 设置默认的modal参数
    const opts = {
      width: appView.width || '80%',
      height: appView.height || '80%',
      footerHide: true,
    };

    const modal: IModal = { mode: ViewMode.MODAL };
    return openViewModal(
      {
        context,
        params,
        modelPath,
        modal,
      },
      opts,
    );
  }

  async popover(
    appView: IPSAppView,
    event: MouseEvent,
    context?: IBizContext | undefined,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    // 获取视图path
    const modelPath = appView.modelPath!;

    const modal: IModal = { mode: ViewMode.POPOVER };
    return openViewPopover(
      event,
      {
        context,
        params,
        modelPath,
        modal,
      },
      { autoClose: true, placement: 'bottom' },
    );
  }

  /**
   * 抽屉打开视图
   *
   * @author lxm
   * @date 2022-09-15 15:09:50
   * @param {IPSAppView} appView
   * @param {(IBizContext | undefined)} [context]
   * @param {(IParams | undefined)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async drawer(
    appView: IPSAppView,
    context?: IBizContext | undefined,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    // 获取视图path
    const modelPath = appView.modelPath!;
    const placement = getDrawerPlacement(appView.openMode);

    // 设置默认的modal参数
    const opts = {
      width: appView.width || '800',
      height: appView.height || '600',
      placement,
    };

    const modal: IModal = { mode: ViewMode.DRAWER };
    return openViewDrawer(
      {
        context,
        params,
        modelPath,
        modal,
      },
      opts,
    );
  }

  async custom(
    appView: IPSAppView,
    context?: IBizContext | undefined,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    ibiz.log.warn('openUserCustom', appView, context, params);
    throw new Error();
  }
}
