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

import { Injectable } from '@angular/core';
import { Category } from '../models/category';
import { PinboardI18nService } from './pinboard-i18n.service';

@Injectable({ providedIn: 'root' })
/**
 * Adds/Registers categories which will be available for widgetDefinitions and Settings
 */
export class CategoryService {
  /**
   * Current set of categories
   */
  private categories: Category[] = [];

  constructor(private pinboardI18nService: PinboardI18nService) {
    this.reset();
  }

  /**
   * init the defined categories and define the global category
   */
  public reset() {
    this.categories.splice(0, this.categories.length, {
      id: 'set_general',
      visible: true,
      title: this.pinboardI18nService.general,
      description: this.pinboardI18nService.generalDescription,
    });
  }

  /**
   * Add a new category
   */
  public addCategory(category: Category): void {
    this.categories.push(category);
  }

  /**
   * Get all added categories
   * @returns all categories
   */
  public getCategories(): Category[] {
    return this.categories;
  }

  /**
   * Search for category by id
   * @param id - to search for
   * @returns category
   */
  public getCategory(id: string): Category {
    return this.categories.find((x: Category) => x.id === id);
  }
}
