/* eslint-disable @typescript-eslint/no-non-null-assertion,@typescript-eslint/no-explicit-any */
/*
 * Copyright (C) 2018, 2019 StApps
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {ActivatedRoute, RouterModule} from '@angular/router';
import {IonRefresher} from '@ionic/angular';
import {
  TranslateLoader,
  TranslateModule,
  TranslateService,
} from '@ngx-translate/core';
import {sampleThingsMap} from '../../../_helpers/data/sample-things';
import {HebisRoutingModule} from '../hebis-routing.module';
import {HebisModule} from '../hebis.module';
import {HebisDataProvider} from '../hebis-data.provider';
import {HebisDetailComponent} from './hebis-detail.component';
import {Observable, of} from 'rxjs';
import {StorageProvider} from '../../storage/storage.provider';

const translations: any = {data: {detail: {TITLE: 'Foo'}}};

class TranslateFakeLoader implements TranslateLoader {
  getTranslation(_lang: string): Observable<any> {
    return of(translations);
  }
}

describe('HebisDetailComponent', () => {
  let comp: HebisDetailComponent;
  let fixture: ComponentFixture<HebisDetailComponent>;
  let dataProvider: HebisDataProvider;
  let refresher: IonRefresher;
  const sampleThing = sampleThingsMap.book[0];
  let translateService: TranslateService;

  // @Component({ selector: 'stapps-data-list-item', template: '' })
  // class DataListItemComponent {
  //   @Input() item;
  // }

  const fakeActivatedRoute = {
    snapshot: {
      paramMap: {
        get: () => {
          return sampleThing.uid;
        },
      },
    },
  };

  const storageProviderSpy = jasmine.createSpyObj('StorageProvider', [
    'init',
    'get',
    'has',
    'put',
    'search',
  ]);

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [
          RouterModule.forRoot([], {relativeLinkResolution: 'legacy'}),
          HebisRoutingModule,
          HebisModule,
          TranslateModule.forRoot({
            loader: {provide: TranslateLoader, useClass: TranslateFakeLoader},
          }),
        ],
        providers: [
          {
            provide: ActivatedRoute,
            useValue: fakeActivatedRoute,
          },
          {
            provide: StorageProvider,
            useValue: storageProviderSpy,
          },
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
      }).compileComponents();
    }),
  );

  beforeEach(async () => {
    dataProvider = TestBed.get(HebisDataProvider);
    translateService = TestBed.get(TranslateService);
    refresher = jasmine.createSpyObj('refresher', ['complete']);
    spyOn(dataProvider, 'get' as any).and.returnValue(
      Promise.resolve(sampleThing),
    );
    spyOn(HebisDetailComponent.prototype, 'getItem').and.callThrough();
    fixture = await TestBed.createComponent(HebisDetailComponent);
    comp = fixture.componentInstance;
    translateService.use('foo');
    fixture.detectChanges();
  });

  it('should create component', () => expect(comp).toBeDefined());

  it('should get a data item', () => {
    comp.getItem(sampleThing.uid);
    expect(HebisDetailComponent.prototype.getItem).toHaveBeenCalledWith(
      sampleThing.uid,
    );
  });

  it('should get a data item when the view is entered', () => {
    comp.ionViewWillEnter();
    expect(HebisDetailComponent.prototype.getItem).toHaveBeenCalledWith(
      sampleThing.uid,
    );
  });

  it('should update the data item when refresh is called', async () => {
    await comp.refresh(refresher);
    expect(HebisDetailComponent.prototype.getItem).toHaveBeenCalledWith(
      sampleThing.uid,
    );
    expect(refresher.complete).toHaveBeenCalled();
  });
});
