import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import { AuthServiceService } from './services/auth-service.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'Parent App';
  isloggedIn: boolean;
  isNotloggedIn: boolean;

  constructor(

    public oidcSecurityService: OidcSecurityService, public authService: AuthServiceService,private router: Router) {

      if (this.oidcSecurityService.moduleSetup) {
        this.doCallbackLogicIfRequired();
      } else {
        this.oidcSecurityService.onModuleSetup.subscribe(() => {
          this.doCallbackLogicIfRequired();
        });
      }

      if(!authService.loggedIn()){
        this.router.navigate(['/login']);
      }

    }

  ngOnInit(): void {
    const token = this.oidcSecurityService.getToken();
    this.isloggedIn = this.authService.loggedIn();
    this.isNotloggedIn = !this.isloggedIn;
  }

  private doCallbackLogicIfRequired() {
    this.oidcSecurityService.authorizedCallbackWithCode(window.location.toString());
  }

}
