import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { EncService } from '../../../../../services/enc.service';
import { ResourcesService } from '../../../../../services/resources.service';

@Component({
    selector: 'app-token-viewer',
    templateUrl: './token-viewer.component.html',
    styleUrl: './token-viewer.component.css',
    standalone: false
})
export class TokenViewerComponent implements OnInit {
  tokenStr: string;
  showToken: boolean;

  constructor(
    @Inject(MAT_DIALOG_DATA) public data: any,
    private _dialogRef: MatDialogRef<TokenViewerComponent>,
    private _encService: EncService,
    private _resourcesService: ResourcesService,
  ) {
    this.tokenStr = "";
    this.showToken = false;
  }

  ngOnInit(): void {
    if(this.data.token == undefined || this.data.token == null){
      this._resourcesService.openSnackBar('El token no se recibió correctamente.');
      this._dialogRef.close();
    }else{
      this.decodeToken(this.data.token);
    }
  }

  async decodeToken(tokenEnc: string){
    let token = await this._encService.decrypt(tokenEnc);
    this.tokenStr = token;
  }
}
