import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  Resolve,
  Router,
  RouterStateSnapshot,
} from '@angular/router';
import { MemberService } from 'app/modules/polyglot/member/services/member.service';
import { GetMemberResponse } from 'app/modules/protobuf/c2se/backend/member_pb';
import { catchError, Observable, throwError } from 'rxjs';

import { User } from '../models/user';
import { UserPagination } from '../models/user-pagination';
import { UserService } from '../services/user.service';

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

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

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<User | undefined> {
    return this._userService.getUserById(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 UsersResolver implements Resolve<any> {
  /**
   * Constructor
   */
  constructor(private _userService: UserService) {}

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

  /**
   * Resolver
   *
   * @param route
   * @param state
   */
  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<{
    pagination: UserPagination;
    users: User[];
  }> {
    return this._userService.getUsers();
  }
}

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

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

  /**
   * Resolver
   *
   * @param route
   * @param state
   */

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<GetMemberResponse.Result>
    | Promise<GetMemberResponse.Result>
    | GetMemberResponse.Result {
    let memberId = route.paramMap.get('id');
    if (!memberId) {
      console.error('user.resolver memberId is null');
    }
    return this._memberService.getMember(memberId!);
  }
}
