import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { LocalStorageService } from '../local-storage/local-storage.service';

@Injectable({
  providedIn: 'root'
})
export class ImageService {
  private apiUrl = environment.apiURL;

  constructor(
    private http: HttpClient,
    private localStorageService: LocalStorageService
  ) { }

  getImage(imageId: string): Observable<Blob> {
    const token = this.localStorageService.getToken();
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`
    });

    return this.http.get(`${this.apiUrl}/image/${imageId}`, {
      headers,
      responseType: 'blob'
    });
  }

  postImage(image: File): Observable<any> {
    const token = this.localStorageService.getToken();
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`
    });

    const formData = new FormData();
    formData.append('image', image);

    return this.http.post(`${this.apiUrl}/image/pin/null/add`, formData, { headers });
  }
}
