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

import { Injectable } from '@angular/core';
import { WidgetContentComponent } from '../models/widget-content-component';
import { WidgetDefinition } from '../models/widget-definition';

@Injectable({ providedIn: 'root' })
/**
 * Manages all known WidgetDefinitions
 */
export class WidgetDefinitionService {
  /**
   * Holds defined widgetdefinitions
   */
  private definedWidgets: WidgetDefinition<WidgetContentComponent, any, any>[] = [];

  /**
   * reset all widget definitions
   */
  public reset() {
    this.definedWidgets.splice(0, this.definedWidgets.length);
  }

  /**
   * Adds a widgetdefinition to the definedWidget array
   * @param widgetDef - widgetDef to add
   */
  public defineWidget(widgetDef: WidgetDefinition<WidgetContentComponent, any, any>): void {
    this.definedWidgets.push(widgetDef);
  }

  /**
   * Returns the currently defined widgetdefinitions
   * @param id - optional: filter by id
   * @returns WidgetDefinitions
   */
  public getWidgetDefinitions(id?: string): WidgetDefinition<WidgetContentComponent, any, any>[] {
    if (id) {
      return this.definedWidgets.filter((x) => x.id === id);
    }
    return this.definedWidgets;
  }
}
