import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Router } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { OidcAuthService } from 'src/app/core/service/oidc/oidc.service';
import { AppTokenService } from 'src/app/core/service/security/app-token.service';

@Component({
  selector: 'app-auth-callback',
  templateUrl: './auth-callback.component.html',
  styleUrls: ['./auth-callback.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AuthCallbackComponent implements OnInit {
    constructor(
        private oidc: OidcAuthService,
        private apptokenSV: AppTokenService,
        private router: Router
    ) { }

    ngOnInit(): void {
        this.oidc.manager.signinRedirectCallback().then(res => {
            this.oidc.user = res
            this.apptokenSV.exchangeToken().pipe(
                tap(x => this.router.navigate(['/home'])),
                catchError(err => {
                    this.router.navigate(['/'])
                    return throwError(err)
                })
            ).subscribe()
        }).catch(err => {
            // this.router.navigate(['/'])
            // this.router.navigate(['/setting-exam'])
        })
    }
}
