/*
 * Copyright (C) 2022 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/>.
 */

/* eslint-disable @typescript-eslint/no-explicit-any */
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {TestBed} from '@angular/core/testing';

import {ModalController, Platform} from '@ionic/angular/standalone';

import {TranslateService} from '@ngx-translate/core';
import {ThingTranslateService} from './translation/thing-translate.service';
import {provideHttpClientTesting} 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';
import {sampleAuthConfiguration} from './_helpers/data/sample-configuration';
import {StorageProvider} from './modules/storage/storage.provider';
import {SimpleBrowser} from './util/browser.factory';
import {provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
import {InAppReviewProvider} from './modules/settings/in-app-review/in-app-review.provider';

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 inAppReviewProvider: jasmine.SpyObj<InAppReviewProvider>;
  let configProvider: jasmine.SpyObj<ConfigProvider>;
  let ngxLogger: jasmine.SpyObj<NGXLogger>;
  let scheduleSyncServiceSpy: jasmine.SpyObj<ScheduleSyncService>;
  let platformIsSpy;
  let storageProvider: jasmine.SpyObj<StorageProvider>;
  let simpleBrowser: jasmine.SpyObj<SimpleBrowser>;
  let modalController: jasmine.SpyObj<ModalController>;

  beforeEach(() => {
    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', 'getAnyValue']);
    configProvider.getAnyValue = jasmine.createSpy().and.callFake(function () {
      return sampleAuthConfiguration;
    });
    ngxLogger = jasmine.createSpyObj('NGXLogger', ['log', 'error', 'warn']);
    storageProvider = jasmine.createSpyObj('StorageProvider', ['init', 'get', 'has', 'put']);
    simpleBrowser = jasmine.createSpyObj('SimpleBrowser', ['open']);
    modalController = jasmine.createSpyObj('ModalController', ['create', 'dismiss', 'getTop']);

    TestBed.configureTestingModule({
      declarations: [AppComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      imports: [RouterTestingModule.withRoutes([]), AuthModule],
      providers: [
        {provide: Platform, useValue: platformSpy},
        {provide: TranslateService, useValue: translateServiceSpy},
        {provide: ThingTranslateService, useValue: thingTranslateServiceSpy},
        {provide: ScheduleSyncService, useValue: scheduleSyncServiceSpy},
        {provide: SettingsProvider, useValue: settingsProvider},
        {provide: InAppReviewProvider, useValue: inAppReviewProvider},
        {provide: ConfigProvider, useValue: configProvider},
        {provide: NGXLogger, useValue: ngxLogger},
        {provide: StorageProvider, useValue: storageProvider},
        {provide: SimpleBrowser, useValue: simpleBrowser},
        {provide: ModalController, useValue: modalController},
        provideHttpClient(withInterceptorsFromDi()),
        provideHttpClientTesting(),
      ],
    }).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!
});
