import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MCOMBAInterface } from 'src/app/interfaces/mco/mcomba/mcomba-interface';
import { USERInterface } from 'src/app/interfaces/user-interface';

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

  jwt = localStorage.getItem('jwt');
  header = new HttpHeaders().set('Authorization', 'Bearer ' + this.jwt)
  usuario_session:USERInterface = {} as USERInterface;
  
  constructor(private http: HttpClient, private _snackBar: MatSnackBar) {
    this.header.set('timeout', '30000');
  }

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

  isOnline() {
    return navigator.connection;
  }

  obtenerNumeroRegistro() {
    this.ngOnInit();
    return this.http.get<any>(`${this._url}/consultar/numeroRegistros/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`, {
      headers: this.header,
    });
  }

  obtenerPorPagina(pagina1:number, pagina2:number){
    this.ngOnInit();
    return this.http.get<any>(`${this._url}/consultar/${pagina1}/${pagina2}/${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;
  } 
}
