/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { CatalogConf } from './catalog-conf';
import { UserSetting } from './userSetting';
import { WidgetContentComponent } from './widget-content-component';
import { Configuration } from './configuration';

/**
 * WidgetDefinitions are the base for all widgets
 * They store static data which won't be edited by the user
 */
export class WidgetDefinition<T extends WidgetContentComponent, C, S> {
  /**
   * Unique identifier
   */
  public id: string;
  /**
   * Hides the edit button in the widget burger menu
   */
  public canEdit = true;
  /**
   * Hides the remove button in the widget burger menu
   */
  public canRemove = true;
  /**
   * Gives the widget the ability to listen to dashboard filters
   */
  public canLinkToFilter = false;
  /**
   * Shows the link to dashboard filter button in the widget burger menu
   */
  public hasLinkToFilterToggle = false;
  /**
   * Holds data, how the definition will be shown in the widgetcatalog
   */
  public catalogConf: CatalogConf;
  /**
   * Configuration of the widget in widget configuration  (ex: width, height etc)
   * Configuration is defined by widget provide for
   * Width and height sizes give in a dropdown in the widget settings form
   * When width is unset, these defaults will be used: [4,5,6,7,8]
   * When height is unset, these defaults will be used: [3,4,5,6]
   */
  public configuration: Configuration = {
    availableWidths: [4, 5, 6, 7, 8],
    availableHeights: [3, 4, 5, 6],
  };
  /**
   * Setups the widgetSettings form
   */
  public userSettings: UserSetting[] = [];
  /**
   * Settings which can't be modified by the user through the widgetSettingsForm
   */
  public settings: S = {} as S;
  /**
   * When adding a new widget, this will be the title
   */
  public defaultTitle = '';
  /**
   * The different widgetTypes are defined in the WidgetDefinitions Array (definitions.ts)
   * There is also defined which contentComponent and Config belong to the Widget
   */
  public widgetType: string;

  constructor(definition: WidgetDefinition<T, C, S>);
  constructor(id: string, catalogConf: CatalogConf, widgetType: string);
  constructor(defOrId: string | WidgetDefinition<T, C, S>, catalogConf?: CatalogConf, widgetType?: string) {
    if (typeof defOrId === 'string') {
      this.id = defOrId;
      this.catalogConf = catalogConf;
      this.widgetType = widgetType;
    } else {
      this.id = defOrId.id;
      this.userSettings = defOrId.userSettings;
      this.catalogConf = defOrId.catalogConf;
      this.canRemove = defOrId.canRemove === undefined ? true : defOrId.canRemove;
      this.canEdit = defOrId.canEdit === undefined ? true : defOrId.canEdit;
      this.canLinkToFilter = defOrId.canLinkToFilter === undefined ? false : defOrId.canLinkToFilter;
      this.hasLinkToFilterToggle = defOrId.hasLinkToFilterToggle === undefined ? false : defOrId.hasLinkToFilterToggle;
      this.defaultTitle = defOrId.defaultTitle === undefined ? this.defaultTitle : defOrId.defaultTitle;
      this.configuration = defOrId.configuration === undefined ? this.configuration : defOrId.configuration;
      this.widgetType = defOrId.widgetType;
      this.settings = defOrId.settings;
    }
  }
}
