import { Component } from '@angular/core';
import { Router, RouterModule, RouterOutlet, ActivatedRoute } from '@angular/router';
import { Authentication } from './services/Authentication';
import { CommonModule, Location } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { isDevMode } from '@angular/core';
import { TranslationService } from './services/translations';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, CommonModule, RouterModule],
  templateUrl: './app.html',
  styleUrl: './app.css'
})
export class App {

  devMode: boolean = false;
  loginToken: string | null = null;

  constructor( private http: HttpClient, public auth: Authentication, private router: Router, private route: ActivatedRoute, private location: Location, public strings: TranslationService){
    strings.setLanguage("en");
    this.devMode = isDevMode();
    this.route.queryParams.subscribe(params => {
      this.loginToken = params['LoginToken'];
      if (this.loginToken){
        this.http.post( "api/account/loginticket", JSON.stringify(this.loginToken), { headers: {'Content-Type': 'application/json'} } ).subscribe({
          next: async() => {
            await this.auth.loadLoginState();
            const pathWithoutQuery = this.location.path().split('?')[0];
            this.location.replaceState(pathWithoutQuery);
          }
        })
      }
    });
  }
}
