import { EncService } from 'src/app/services/enc/enc.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Component, Inject, OnInit } from '@angular/core';

@Component({
  selector: 'app-token-wiewer',
  templateUrl: './token-wiewer.component.html',
  styleUrls: ['./token-wiewer.component.css']
})
export class TokenWiewerComponent implements OnInit {
  tokenStr: string;
  showToken: boolean;

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

  ngOnInit(): void {
    if(this.data.token == undefined || this.data.token == null){
      this.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.desencriptar(tokenEnc);
    this.tokenStr = token;
  }

  openSnackBar(msg: string){
    this._snackBar.open(msg, undefined, {
      duration: 2500,
    })
  }
}
