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

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { CatalogConf } from '../models/catalog-conf';
import { Category } from '../models/category';
import { WidgetDefinition } from '../models/widget-definition';
import { PinboardModule } from '../pinboard.module';
import { CategoryService } from '../services/category.service';
import { WidgetDefinitionService } from '../services/widget-definition.service';
import { TestTextContentComponent, TestTextWidgetUserSetting } from '../widget/test-text-widget-component';
import { WidgetCatalogComponent } from './widget-catalog.component';

describe('WidgetCatalogComponent', () => {
  let component: WidgetCatalogComponent;
  let fixture: ComponentFixture<WidgetCatalogComponent>;
  let widgetDefService: WidgetDefinitionService;
  let catService: CategoryService;
  let def1: WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>;
  let def2: WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>;

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [NoopAnimationsModule, PinboardModule],
        providers: [
          WidgetDefinitionService,
          CategoryService,
          { provide: MAT_DIALOG_DATA, useValue: {} },
          { provide: MatDialogRef, useValue: {} },
        ],
        // declarations: [WidgetCatalogComponent]
      }).compileComponents();

      widgetDefService = TestBed.inject(WidgetDefinitionService);
      catService = TestBed.inject(CategoryService);
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(WidgetCatalogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    const cat1 = {} as Category;
    const cat2 = {} as Category;
    cat1.id = '1';
    cat1.title = 'one';
    cat2.id = '2';
    cat2.title = 'two';

    catService.addCategory(cat1);
    catService.addCategory(cat2);

    const catConf1 = {
      id: '50',
      title: 'title one',
      description: 'description one',
      categoryIds: ['1', '2'],
    } as CatalogConf;
    const catConf2 = {
      id: '51',
      title: 'title two',
      description: 'description two',
      categoryIds: ['2'],
    } as CatalogConf;

    def1 = new WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>('10', catConf1, 'TextWidget');
    def2 = new WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>('11', catConf2, 'TextWidget');

    widgetDefService.defineWidget(def1);
    widgetDefService.defineWidget(def2);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('#getCategories should return all categories of defined widgets', () => {
    const result = component.getCategories(false).map((value) => value.id);
    let expectation = [];
    expectation = expectation.concat(def1.catalogConf.categoryIds);
    expectation = expectation.concat(def2.catalogConf.categoryIds);
    for (const res of result) {
      if (expectation.indexOf(res) < 0) {
        fail();
      }
    }
  });

  it('#getWidgetsOfCategory should return widgetDefs which belong to a category', () => {
    const categoryToTest = catService.getCategory('1');
    const results = component.getWidgetsOfCategory(categoryToTest);
    const expectation = [def1];
    expect(results).toEqual(expectation);
  });
});
