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

import { Question } from 'src/app/core/models';
import { QCMService } from 'src/app/core/services';
import { catchError } from 'rxjs/operators';

@Injectable()
export class QCMResolver implements Resolve<Question> {
    constructor(
        private _qcmService: QCMService,
        private router: Router
    ) { }

    resolve(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
    ): Observable<any> {

        return this._qcmService.get(route.params['type'])
            .pipe(catchError(() => this.router.navigateByUrl('/dashboard')));
    }
}
