/* eslint-disable @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 {TestBed, waitForAsync} from '@angular/core/testing';

import {Platform} from '@ionic/angular';

import {TranslateService} from '@ngx-translate/core';
import {ThingTranslateService} from './translation/thing-translate.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {AppComponent} from './app.component';
import {AuthModule} from './modules/auth/auth.module';
import {ConfigProvider} from './modules/config/config.provider';
import {SettingsProvider} from './modules/settings/settings.provider';
import {NGXLogger} from 'ngx-logger';
import {RouterTestingModule} from '@angular/router/testing';
import {ScheduleSyncService} from './modules/background/schedule/schedule-sync.service';

describe('AppComponent', () => {
  let platformReadySpy: any;
  let platformSpy: jasmine.SpyObj<Platform>;
  let translateServiceSpy: jasmine.SpyObj<TranslateService>;
  let thingTranslateServiceSpy: jasmine.SpyObj<ThingTranslateService>;
  let settingsProvider: jasmine.SpyObj<SettingsProvider>;
  let configProvider: jasmine.SpyObj<ConfigProvider>;
  let ngxLogger: jasmine.SpyObj<NGXLogger>;
  let scheduleSyncServiceSpy: jasmine.SpyObj<ScheduleSyncService>;

  let platformIsSpy;

  beforeEach(
    waitForAsync(() => {
      platformReadySpy = Promise.resolve();
      platformIsSpy = Promise.resolve();
      platformSpy = jasmine.createSpyObj('Platform', {
        ready: platformReadySpy,
        is: platformIsSpy,
      });
      translateServiceSpy = jasmine.createSpyObj('TranslateService', [
        'setDefaultLang',
        'use',
      ]);
      thingTranslateServiceSpy = jasmine.createSpyObj('ThingTranslateService', [
        'init',
      ]);
      settingsProvider = jasmine.createSpyObj('SettingsProvider', [
        'getSettingValue',
        'provideSetting',
        'setCategoriesOrder',
      ]);
      scheduleSyncServiceSpy = jasmine.createSpyObj('ScheduleSyncService', [
        'getDifferences',
        'postDifferencesNotification',
      ]);
      configProvider = jasmine.createSpyObj('ConfigProvider', ['init']);
      ngxLogger = jasmine.createSpyObj('NGXLogger', ['log', 'error', 'warn']);

      TestBed.configureTestingModule({
        imports: [
          RouterTestingModule.withRoutes([]),
          HttpClientTestingModule,
          AuthModule,
        ],
        declarations: [AppComponent],
        providers: [
          {provide: Platform, useValue: platformSpy},
          {provide: TranslateService, useValue: translateServiceSpy},
          {provide: ThingTranslateService, useValue: thingTranslateServiceSpy},
          {provide: ScheduleSyncService, useValue: scheduleSyncServiceSpy},
          {provide: SettingsProvider, useValue: settingsProvider},
          {provide: ConfigProvider, useValue: configProvider},
          {provide: NGXLogger, useValue: ngxLogger},
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
      }).compileComponents();
    }),
  );

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });

  it('should initialize the app', async () => {
    TestBed.createComponent(AppComponent);
    expect(platformSpy.ready).toHaveBeenCalled();
    // await platformReadySpy;

    // TODO: https://capacitorjs.com/docs/guides/mocking-plugins
    // expect(splashScreenSpy.hide).toHaveBeenCalled();
  });

  // TODO: add more tests!
});
