import { Injectable } from '@angular/core';
import { Resolve, Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

import { IToken, TokenService } from './token.service';

@Injectable()
export class ApiConsumerTokenResolve implements Resolve<IToken> {
  constructor(private tokenService: TokenService, private router: Router) { }

  resolve(): Promise<IToken> {
    return this.tokenService.apiConsumerToken().catch(r => {
      this.router.navigate(['/session', 'error']);
    });
  }
}

@Injectable()
export class ApiOperatorTokenResolve implements Resolve<IToken> {
  constructor(private tokenService: TokenService, private router: Router) { }
  resolve(): Promise<IToken | boolean> {
    return new Promise((resolve, reject) => {
      if (!this.tokenService.isLogin) {
        // reject();
        this.router.navigate(['/session', 'signin']);
      }
      resolve(true);
    });
  }
}
