import { ModelData } from '../model-data';

/**
 * 粘贴板控制器
 *
 * @export
 * @class ModelClipboardController
 */
export class ModelClipboardController {
  /**
   * 唯一实例
   *
   * @private
   * @static
   * @type {ModelClipboardController}
   * @memberof ModelClipboardController
   */
  private static readonly instance: ModelClipboardController = new ModelClipboardController();

  /**
   * 拖拽配置
   *
   * @type {*}
   * @memberof ModelClipboardController
   */
  draggableOption: any = {
    noPutGroup: {
      name: 'StudioDesign',
      pull: 'clone',
      put: false,
    },
    group: {
      name: 'StudioDesign',
    },
  };

  /**
   * 缓存识别码
   *
   * @private
   * @type {string}
   * @memberof ModelClipboardController
   */
  private localStorageKey: string = 'ModelClipboardCache';

  /**
   * 是否显示粘贴板
   *
   * @type {boolean}
   * @memberof ModelClipboardController
   */
  isShowModelClipboard: boolean = false;

  /**
   * 当前放置到剪贴板数据
   *
   * @type {ModelData[]}
   * @memberof ModelClipboardController
   */
  items: ModelData[] = [];

  /**
   * Creates an instance of ModelClipboardController.
   * @memberof ModelClipboardController
   */
  constructor() {
    if (ModelClipboardController.instance) {
      return ModelClipboardController.instance;
    }
    this.init();
  }

  /**
   * 初始化
   *
   * @private
   * @memberof ModelClipboardController
   */
  private init(): void {
    const items = window.localStorage.getItem(this.localStorageKey);
    try {
      if (items) {
        this.items = JSON.parse(items);
      }
    } catch (err) {
      console.error(err);
    }
  }

  /**
   * 新建模型至剪贴板
   *
   * @param {{ title: string, srfDeName: string, srfParentData?: any, items: any[] }} data
   * @memberof ModelClipboardController
   */
  createdItem(data: { title: string; srfDeName: string; srfParentData?: any; items: any[] }): void {
    this.setItem(new ModelData(data));
    this.showChange(true);
  }

  /**
   *
   *
   * @param {ModelData} data
   * @memberof ModelClipboardController
   */
  setItem(data: ModelData): void {
    this.items.splice(0, 0, data);
    this.saveToStore();
  }

  /**
   * 改变粘贴板显示状态
   *
   * @param {boolean} [bool]
   * @memberof ModelClipboard
   */
  showChange(bool?: boolean): void {
    if (bool !== undefined) {
      this.isShowModelClipboard = bool;
    } else {
      this.isShowModelClipboard = !this.isShowModelClipboard;
    }
  }

  /**
   * 存储数据至缓存
   *
   * @memberof ModelClipboardController
   */
  saveToStore(): void {
    try {
      localStorage.setItem(this.localStorageKey, JSON.stringify(this.items));
    } catch (err) {
      console.error(err);
    }
  }

  /**
   * 根据下标删除数据
   *
   * @param {number} i
   * @memberof ModelClipboardController
   */
  removeByIndex(i: number): void {
    this.items.splice(i, 1);
    this.saveToStore();
  }

  /**
   * 清除所有
   *
   * @memberof ModelClipboardController
   */
  clearAll(): void {
    this.items = [];
    localStorage.removeItem(this.localStorageKey);
  }

  /**
   * 获取实例
   *
   * @static
   * @returns {ModelClipboardController}
   * @memberof ModelClipboardController
   */
  static getInstance(): ModelClipboardController {
    return this.instance;
  }
}
