import { CurrencyPipe } from '@angular/common';
import { LOCALE_ID, NgModule } from '@angular/core';
import { MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';
import { DefaultUrlSerializer, Router, UrlSerializer, UrlTree } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { environment } from 'src/environments/environment';
import { EnvironmentService } from 'src/environments/environment.service';
import { HeaderMobileComponent } from '../components/header/mobile/header-mobile.component';
import { HomeMobileComponent } from '../components/home/mobile/home-mobile.component';
import { MainMenuComponent } from '../components/main-menu/main-menu.component';
import { SearchViewMobileComponent } from '../components/search-view/mobile/search-view-mobile.component';
import { SearchViewResultMobileComponent } from '../components/search-view/search-view-result/mobile/search-view-result-mobile.component';
import { SidebarMobileComponent } from '../components/sidebar/mobile/sidebar-mobile.component';
import { MobileAppComponent } from '../mobile-app.component';
import { FormatBytesPipe } from '../shared/pipes/format-bytes.pipe';
import { AuthService } from '../shared/server-services/auth.service';
import { SubscriptionService } from '../shared/server-services/subscription.service';
import { AudioService } from '../shared/services/audio.service';
import { CordovaService } from '../shared/services/cordova.service';
import { DialogService } from '../shared/services/dialog.service';
import { RouterHandler } from '../shared/services/router-handler.service';
import { ServerRestApiService } from '../shared/services/server-rest-api.service';
import { SwipeService } from '../shared/services/swipe.service';
import { TitleService } from '../shared/services/title.service';
import { VersionService } from '../shared/services/version.service';
import { DynamicLocaleId, SharedModule } from './shared.module';

class CustomUrlSerializer implements UrlSerializer {
  private defaultUrlSerializer = new DefaultUrlSerializer();

  parse(url: string): UrlTree {
    return this.defaultUrlSerializer.parse(url);
  }

  serialize(tree: UrlTree): string {
    let serialized = this.defaultUrlSerializer.serialize(tree);
    let parts = serialized.split('#');
    if (parts.length > 1) {
      serialized = parts[0] + '#' + tree.fragment;
    }
    return serialized;
  }
}
@NgModule({
  declarations: [
    HeaderMobileComponent,
    HomeMobileComponent,
    MainMenuComponent,
    MobileAppComponent,
    SearchViewMobileComponent,
    SearchViewResultMobileComponent,
    SidebarMobileComponent,
  ],
  imports: [SharedModule],
  providers: [
    { provide: UrlSerializer, useClass: CustomUrlSerializer },
    AudioService,
    AuthService,
    CordovaService,
    CurrencyPipe,
    DialogService,
    FormatBytesPipe,
    ServerRestApiService,
    SubscriptionService,
    SwipeService,
    TitleService,
    VersionService,
    {
      provide: RouterHandler,
      useClass: RouterHandler,
      deps: [Router],
    },
    { provide: LOCALE_ID, useClass: DynamicLocaleId, deps: [TranslateService] },
    { provide: EnvironmentService, useValue: environment },
    {
      provide: MAT_DIALOG_DEFAULT_OPTIONS,
      useValue: {
        disableClose: true,
        hasBackdrop: true,
        delayFocusTrap: true,
        autoFocus: 'first-tabbable',
      },
    },
  ],
  bootstrap: [MobileAppComponent],
})
export class MobileAppModule {
  constructor() {}
}
