import { Component, OnInit } from '@angular/core';
import { AuthService } from '@shared/auth/auth.service';
import { ActivatedRoute, Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';

@Component({
  selector: 'app-saml2sso',
  templateUrl: './saml2sso.component.html',
  styleUrls: ['./saml2sso.component.scss']
})
export class Saml2SSOComponent implements OnInit {
  showLoader = false;
  showError = false;

  constructor(
    private authService: AuthService,
    private activatedRoute: ActivatedRoute,
    public config: ConfigService,
    private router: Router,
  ) {
  }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(value => {
      if (value && value.token) {
        this.showLoader = true;
        this.authService.saml2ssoLogin(value.token).subscribe({
          next: () => {
            this.router.navigateByUrl(this.config.getPanelHomeUrl()).then(() => {
              this.authService.signalLoginCompleted();
            });
          }, error: () => {
            this.showError = true;
          }
        }).add(() => {
          this.showLoader = false;
        });
      } else {
        this.showError = true;
      }
    });
  }
}
