/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatTableModule } from '@angular/material/table';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { firstValueFrom, of } from 'rxjs';
import { filter, first, map } from 'rxjs/operators';
import { NotificationItem, NotificationTypes } from '../notification/notification.models';
import { NotificationService } from '../notification/notification.service';
import { BciMessagesComponent } from './bci-messages.component';
import { BCI_MESSAGES_INTL_PROVIDER } from './messages-intl';

const notifications: NotificationItem[] = [
  new NotificationItem('Test1', 'Action1', NotificationTypes.Error, undefined, undefined, undefined),
  new NotificationItem('Test2', 'Action2', NotificationTypes.Error, undefined, undefined, undefined),
  new NotificationItem('Tea1', 'Action3', NotificationTypes.Information, undefined, undefined, undefined),
];

describe('MessagesComponent', () => {
  let component: BciMessagesComponent;
  let fixture: ComponentFixture<BciMessagesComponent>;

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        schemas: [NO_ERRORS_SCHEMA],
        imports: [MatDialogModule, MatTableModule, HttpClientTestingModule, RouterTestingModule, NoopAnimationsModule],
        declarations: [BciMessagesComponent],
        providers: [
          BCI_MESSAGES_INTL_PROVIDER,
          {
            provide: MatDialogRef,
            useValue: {
              _containerInstance: {
                _config: {},
              },
              // eslint-disable-next-line no-empty-function
              close: () => {},
              backdropClick: () => of(''),
            },
          },
          {
            provide: NotificationService,
            useValue: {
              getSessionNotifications: () => of(notifications),
              /* eslint-disable-next-line no-empty-function, @typescript-eslint/no-unused-vars */
              deleteSessionNotifications: (selection: NotificationItem) => {},
            },
          },
        ],
      }).compileComponents();
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(BciMessagesComponent);
    component = fixture.componentInstance;
    component.ngOnInit();
    fixture.detectChanges();
  });

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

  it('should filter for "Test" in notification messages', fakeAsync(async () => {
    component.search('Test');
    tick(1);
    await expect(firstValueFrom(component.dataSource.pipe(filter(ntf => ntf.data.length > 0), map(ntf => ntf.data.length), first()))).resolves.toEqual(2)
  }));

  it('should filter for error types in notification messages', fakeAsync(async () => {
    component.filters.next([NotificationTypes.Error]);
    await expect(firstValueFrom(component.dataSource.pipe(filter(ntf => ntf.data.length > 0), map(ntf => ntf.data.length), first()))).resolves.toEqual(2)
  }));

  it('should filter for for "Test1" and error types in notification messages', fakeAsync(async () => {
    component.search('Test1');
    component.filters.next([NotificationTypes.Error]);

    await expect(firstValueFrom(component.dataSource.pipe(filter(ntf => ntf.data.length > 0), map(ntf => ntf.data.length), first()))).resolves.toEqual(1)

  }));
});
