import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ResponseData } from 'src/app/interfaces/response-data';
import { USERInterface } from 'src/app/interfaces/user-interface';
import { IAMService } from '../../iam/iam.service';
import { MCOMAURegistrarInterface } from 'src/app/interfaces/mco/mcomau/mcomau-registrar-interface';
import { ENCService } from '../../enc/enc.service';

@Injectable({
  providedIn: 'root',
})
export class MCOMAUService {
  private _url = 'https://qasirh.ittec.mx/v1/public/api/MCOMAU';
  usuario_session: USERInterface = {} as USERInterface;

  constructor(
    private http: HttpClient,
    private _snackBar: MatSnackBar,
    private _iamService: IAMService,
    private _encService: ENCService
  ) {
    this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!);
  }

  jwt = localStorage.getItem('jwt');

  header = new HttpHeaders().set('Authorization', 'Bearer ' + this.jwt);

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

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

  crear(data: MCOMAURegistrarInterface) {
    return this.http.post<ResponseData>(`${this._url}/registrar`, data, {
      headers: this.header,
    });
  }

  modificar(data: any) {
    return this.http.post<ResponseData>(`${this._url}/modificar`, data, {
      headers: this.header,
    });
  }

  eliminar(datos: any) {
    return this.http
      .post<ResponseData>(`${this._url}/eliminar`, datos, {
        headers: this.header,
      })
      
  }

  cambiarContrasena(datos: any) {
    return this.http
      .post<ResponseData>(`${this._url}/cambiarPwd`, datos, {
        headers: this.header,
      })
      
  }

  bloquear(datos: any) {
    return this.http.post<ResponseData>(`${this._url}/bloquear`, datos, {headers: this.header,}) 
  }

  desbloquear(datos: any) {
    return this.http.post<ResponseData>(`${this._url}/desbloquear`, datos, {
      headers: this.header,
    });
  }

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

  obtenerMenos(idmen: string) {
    this.ngOnInit();
    return this.http.get<ResponseData>(
      `https://qasirh.ittec.mx/v1/public/api/MCOMAU/consultar/menos/${this._encService.desencriptar(
        this.usuario_session.IDUSUARIO
      )}/${idmen}/${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;
  } 

  private snackAlert(mensaje: string) {
    this._snackBar.open(mensaje, 'Cerrar', {
      duration: 4000,
    });
  }
}
