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-sso',
  templateUrl: './sso.component.html',
  styleUrls: ['./sso.component.scss']
})
export class SsoComponent implements OnInit {
  showLoader = false;

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

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(value => {
      if (value && value.euid && value.timestamp && value.hash_val) {
        this.showLoader = true;
        const hashValue = ''.concat(value.euid, ':', value.timestamp, ':', value.hash_val);
        this.authService.ssoLogin(hashValue).subscribe(response => {
          this.router.navigateByUrl(this.config.getPanelHomeUrl());
        }, error => {
          this.router.navigateByUrl(this.config.getPanelUrl('login'));
        }).add(() => {
          this.showLoader = false;
        });
      } else {
        this.router.navigateByUrl(this.config.getPanelUrl('login'));
      }
    });
  }

}
