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

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

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

import { TranslateService } from '@ngx-translate/core';

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

    resolve(): Observable<IToken> {
        return this.tokenService.apiConsumerToken();
    }
}

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

    resolve(): boolean {
        if (this.tokenService.isLogin) {
            // console.log('resolve', this.route);
            this.router.navigate(['']);
            // this.router.navigate([this.translateService.getDefaultLang(), 'dashboard']);
            return false;
        }
        return true;
    }
}
@Injectable()
export class ApiUserTokenResolve implements Resolve<IToken | boolean> {
    constructor(private tokenService: TokenService,
        private router: Router) { }

    resolve(route: ActivatedRouteSnapshot): IToken | boolean {
        if (!this.tokenService.isLogin) {
            const langParam: string = 'lang';
            this.router.navigate([route.params[langParam], 'login']);
            return false;
        }
        return this.tokenService.authorizationToken;
    }
}
