import { inject, Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { environment } from '../../environments/environment';

@Injectable()
export class Api
{
  private readonly http = inject(HttpClient);

  public static TmpValue1: any;
  public static TmpValue2: any;
  public static TmpValue3: any;
  public static TmpValue4: any;
  public static TmpValue5: any;
  public static TmpValue6: any;
  public static currentUrl: string = '';
  public static previousUrl: string = '';
  public static firebaseToken: string = '';

  public static secretKey: string = '';
  public static cipherSecretKey: string = '';
  public static microServiceEnabled: boolean = false;

  private readonly url: string = '';

  constructor()
  {
    this.url = environment.apiUrl + 'api';
  }

  public get(endpoint: string, params?: any, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
      options = options.append('Accept', 'application/json');
    }

    if (params)
    {
      const p = [];
      for (const k in params)
      {
        p.push(k + '=' + params[k]);
      }
      endpoint = endpoint + '?' + (p.join('&'));
    }
    return this.http.get(`${this.url}/${endpoint}`, { headers: options, withCredentials: true });
  }

  public getAsync<T>(endpoint: string, params?: any, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
      options = options.append('Accept', 'application/json');
    }

    if (params)
    {
      const p = [];
      for (const k in params)
      {
        p.push(k + '=' + params[k]);
      }
      endpoint = endpoint + '?' + (p.join('&'));
    }
    return this.http.get<T>(`${this.url}/${endpoint}`, { headers: options, withCredentials: true });
  }

  public post(endpoint: string, body: any, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
      options = options.append('Accept', 'application/json');
    }
    return this.http.post(`${this.url}/${endpoint}`, body, { headers: options, withCredentials: true });
  }

  public postAsync<T>(endpoint: string, body: any, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
      options = options.append('Accept', 'application/json');
    }
    return this.http.post<T>(`${this.url}/${endpoint}`, body, { headers: options, withCredentials: true });
  }

  public put(endpoint: string, body: any, options: HttpHeaders)
  {
    return this.http.put(`${this.url}/${endpoint}`, body, { headers: options, withCredentials: true });
  }

  public delete(endpoint: string, options?: HttpHeaders)
  {
    return this.http.delete(`${this.url}/${endpoint}`, { headers: options, withCredentials: true });
  }

  public patch(endpoint: string, body: any, options?: HttpHeaders)
  {
    return this.http.put(`${this.url}/${endpoint}`, body, { headers: options, withCredentials: true });
  }

  public uplaodFormData(endpoint: string, formData: FormData, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders();
    }
    return this.http.post(`${this.url}/${endpoint}`, formData, { headers: options, withCredentials: true });
  }

  public downloadFile(endpoint: string, body: any, options?: HttpHeaders)
  {
    if (!options)
    {
      options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' });
      options = options.append('Accept', 'application/json');
    }
    return this.http.post(`${this.url}/${endpoint}`, body, { headers: options, withCredentials: true, observe: 'response', responseType: 'blob' });
  }

  public static clearTmpValue(): void
  {
    this.TmpValue1 = this.TmpValue2 = this.TmpValue3 = this.TmpValue4 = this.TmpValue5 = this.TmpValue6 = undefined;
  }
}
