import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  Resolve,
  Router,
  RouterStateSnapshot,
} from '@angular/router';
import { catchError, Observable, throwError } from 'rxjs';

import { Statistics } from '../models/statistics';
import { CasinoStatistics } from '../models/casino-statistics';
import { SlotStatistics } from '../models/slot-statistics';
import { PowerballStatistics } from '../models/powerball-statistics';
import { StatisticsPagination } from '../models/statistics-pagination';
import { StatisticsService } from '../services/statistics.service';

@Injectable({
  providedIn: 'root',
})
export class StatisticsResolver implements Resolve<any> {
  /**
   * Constructor
   */
  constructor(
    private _statisticsService: StatisticsService,
    private _router: Router
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<{
    pagination: StatisticsPagination;
    statistics: Statistics[];
  }> {
    return this._statisticsService.getStatistics();
  }
  // resolve(
  //   route: ActivatedRouteSnapshot,
  //   state: RouterStateSnapshot
  // ): Observable<Statistics[] | undefined> {
  //   return this._statisticsService
  //     .getStatisticsById(route.paramMap.get('id'))
  //     .pipe(
  //       // Error here means the requested product is not available
  //       catchError((error) => {
  //         // Log the error
  //         console.error(error);

  //         // Get the parent url
  //         const parentUrl = state.url.split('/').slice(0, -1).join('/');

  //         // Navigate to there
  //         this._router.navigateByUrl(parentUrl);

  //         // Throw an error
  //         return throwError(error);
  //       })
  //     );
  // }
}

@Injectable({
  providedIn: 'root',
})
export class CasinoStatisticsResolver implements Resolve<any> {
  /**
   * Constructor
   */
  constructor(
    private _statisticsService: StatisticsService,
    private _router: Router
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<CasinoStatistics | undefined> {
    return this._statisticsService
      .getCasinoStatisticsByType(route.paramMap.get('type'))
      .pipe(
        // Error here means the requested product is not available
        catchError((error) => {
          // Log the error
          console.error(error);

          // Get the parent url
          const parentUrl = state.url.split('/').slice(0, -1).join('/');

          // Navigate to there
          this._router.navigateByUrl(parentUrl);

          // Throw an error
          return throwError(error);
        })
      );
  }
}
@Injectable({
  providedIn: 'root',
})
export class SlotStatisticsResolver implements Resolve<any> {
  /**
   * Constructor
   */
  constructor(
    private _statisticsService: StatisticsService,
    private _router: Router
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<SlotStatistics | undefined> {
    return this._statisticsService
      .getSlotStatisticsByType(route.paramMap.get('type'))
      .pipe(
        // Error here means the requested product is not available
        catchError((error) => {
          // Log the error
          console.error(error);

          // Get the parent url
          const parentUrl = state.url.split('/').slice(0, -1).join('/');

          // Navigate to there
          this._router.navigateByUrl(parentUrl);

          // Throw an error
          return throwError(error);
        })
      );
  }
}
@Injectable({
  providedIn: 'root',
})
export class PowerballStatisticsResolver implements Resolve<any> {
  /**
   * Constructor
   */
  constructor(
    private _statisticsService: StatisticsService,
    private _router: Router
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<PowerballStatistics | undefined> {
    return this._statisticsService
      .getPowerballStatisticsByType(route.paramMap.get('type'))
      .pipe(
        // Error here means the requested product is not available
        catchError((error) => {
          // Log the error
          console.error(error);

          // Get the parent url
          const parentUrl = state.url.split('/').slice(0, -1).join('/');

          // Navigate to there
          this._router.navigateByUrl(parentUrl);

          // Throw an error
          return throwError(error);
        })
      );
  }
}
