import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable, OnInit } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MesmprInterface } from 'src/app/interfaces/mes/mesmpr/mesmpr-interface';
import { ResponseData } from 'src/app/interfaces/response-data';
import { USERInterface } from 'src/app/interfaces/user-interface';
import { ENCService } from '../../enc/enc.service';
import { IAMService } from '../../iam/iam.service';
import { WebSocketService } from '../../socket/web-socket.service';

@Injectable({
  providedIn: 'root',
})
export class MESMPRService implements OnInit{
  private _url = 'https://qasirh.ittec.mx/v1/public/api/MESMPR';

  usuario_session:USERInterface = {} as USERInterface;

  jwt: string = localStorage.getItem('jwt')!;
  header: HttpHeaders = new HttpHeaders().set('Authorization', 'Bearer ' + this.jwt);

  constructor(
    private http: HttpClient,
    private _encService: ENCService,
    private _iamService: IAMService
  ) { }

  ngOnInit(){
    this.usuario_session = JSON.parse(localStorage.getItem("TIMUSERENC")!);
  }


  obtener() {
    this.ngOnInit();
    return this.http.get<ResponseData>(
      `${this._url}/consultar/${this._encService.desencriptar(this.usuario_session.IDUSUARIO)}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`,
      { headers: this.header }
    );
  }

  obtenerPeriodos(usuario:string) {
    this.ngOnInit();
    return this.http.get<MesmprInterface>(`${this._url}/consultar/${usuario}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`, { headers: this.header });
  }

  private formatoRFC(rfc: string){
    let rfc_decoded =  window.atob(rfc);
    let rfc_replace = rfc_decoded.replace("|", "/");
    return rfc_replace;
  } 
}
