import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs';
import { apiTemp } from '../../environments/environment.prod';

@Injectable({
  providedIn: 'root'
})
export class ControlPanelService {

  constructor(private _http: HttpClient) { }

  getPanels(idUser: string, line: number){
    return this.getQuery(`control-panel/get-panels/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getCountersList(idUser: string, line: number){
    return this.getQuery(`control-panel/get-counters-list/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getPanel(idPanel: string, idUser: string, line: number){
    return this.getQuery(`control-panel/get-panel/${idPanel}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getInfoToExcel(element: string, idModule: string, idUser: string, line: number){
    return this.getQuery(`control-panel/get-info-to-excel/${element}/${idModule}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getBroadcastLists(idUser: string, line: number){
    return this.getQuery(`control-panel/get-broadcast-lists/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getBroadcastList(idList: string, idUser: string, line: number){
    return this.getQuery(`control-panel/get-broadcast-list/${idList}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getProfileExportPermissions(idProfile: string, idUser: string, line: number){
    return this.getQuery(`control-panel/get-profile-export-permissions/${idProfile}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getExportableSpreadsheets(idUser: string, line: number){
    return this.getQuery(`control-panel/get-exportable-spreadsheets/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  registerPanel(body: any){
    return this.postQuery("control-panel/register-panel", body).pipe(map((data: any) => data));
  }

  updatePanel(body: any){
    return this.postQuery("control-panel/update-panel", body).pipe(map((data: any) => data));
  }

  updatePanelElements(body: any){
    return this.postQuery("control-panel/update-panel-elements", body).pipe(map((data: any) => data));
  }

  updatePanelAssociations(body: any){
    return this.postQuery("control-panel/update-panel-associations", body).pipe(map((data: any) => data));
  }

  deletePanel(body: any){
    return this.postQuery("control-panel/delete-panel", body).pipe(map((data: any) => data));
  }

  registerBroadcastList(body: any){
    return this.postQuery("control-panel/register-broadcast-list", body).pipe(map((data: any) => data));
  }

  updateBroadcastList(body: any){
    return this.postQuery("control-panel/update-broadcast-list", body).pipe(map((data: any) => data));
  }

  deleteBroadcastList(body: any){
    return this.postQuery("control-panel/delete-broadcast-list", body).pipe(map((data: any) => data));
  }

  saveProfileExportPermissions(body: any){
    return this.postQuery("control-panel/save-profile-export-permissions", body).pipe(map((data: any) => data));
  }

  getQuery(query: string){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`;
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: JWT
      })
    });
  }

  postQuery(query: string, body: any){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: JWT!,
      }),
    });
  }
}
