import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";



import {AppConfig} from "../../config/app.config";
import {ResourceType, Language, Region, Resource, Sector, Project, Keyword, Pest, Crop} from "../model/resource";

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

  private readonly _httpClient: HttpClient;

  public constructor(httpClient: HttpClient) {
    this._httpClient = httpClient;
  }
  public getAllResources() {
    return this._httpClient.get<Resource[]>(`${AppConfig.ApiPaths.getAllResources}`);
  }
  public getResource(id:string) {
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.getResource}`+"/"+id);
  }
  public downloadFile(name: string) {
    const httpOptions = {
      responseType: 'blob' as 'json',
    };
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.getFile}`+"/"+name, httpOptions);
  }
  public saveResource(resource: Resource) {
    return this._httpClient.post<Resource>(`${AppConfig.ApiPaths.saveResource}`, resource);
  }

  public addFiles(image:File,resourceContent:File[],idResource:any){

    const formData: FormData = new FormData();
    if(image != undefined) {
      formData.append("image", image);
    }
    resourceContent.forEach(r =>{
      formData.append("files",r);
    })
    return this._httpClient.post(`${AppConfig.ApiPaths.addFile}`+"/"+idResource,formData)
  }
  public getAllSectors() {
    return this._httpClient.get<Sector[]>(`${AppConfig.ApiPaths.getAllSectors}`);
  }
  public getAllRegions() {
    return this._httpClient.get<Region[]>(`${AppConfig.ApiPaths.getAllRegions}`);
  }
  public getAllResourceTypes() {
    return this._httpClient.get<ResourceType[]>(`${AppConfig.ApiPaths.getAllResourceTypes}`);
  }
  public getAllLanguages() {
    return this._httpClient.get<Language[]>(`${AppConfig.ApiPaths.getAllLanguages}`);
  }
  public getAllProjects() {
    return this._httpClient.get<Project[]>(`${AppConfig.ApiPaths.getAllProjects}`);
  }
  public getAllPests() {
    return this._httpClient.get<Pest[]>(`${AppConfig.ApiPaths.getAllPests}`);
  }
  public getAllCrops() {
    return this._httpClient.get<Crop[]>(`${AppConfig.ApiPaths.getAllCrops}`);
  }
  public getAllKeywords() {
    return this._httpClient.get<Keyword[]>(`${AppConfig.ApiPaths.getAllKeywords}`);

  }

}
