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,
  PageableResource, FileDesc
} from "../model/resource";
import {SearchModel} from "../model/search-model";

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

  private readonly _httpClient: HttpClient;

  public constructor(httpClient: HttpClient) {
    this._httpClient = httpClient;
  }
  public getAllResources(pageNo: number = 0,pageSize: number = 10) {
    return this._httpClient.get<PageableResource>(`${AppConfig.ApiPaths.getAllResources}?pageNumber=${pageNo}&pageSize=${pageSize}`);
  }
  public getResourcesForApproval() {
    return this._httpClient.get<Resource []>(`${AppConfig.ApiPaths.getResourcesForApproval}`);
  }
  public getResource(id:string, lang:string) {
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.getResource}`+"/"+id+"/"+lang);
  }
  public hasImage(id:string,type:string) {
    return this._httpClient.get<boolean>(`${AppConfig.ApiPaths.hasImage}`+"/"+type+"/"+id);
  }
  public approveResource(id:string) {
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.approveResource}`+"/"+id);
  }
  public deleteResource(id:string) {
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.deleteResource}`+"/"+id);
  }
  public deleteFile(id:string) {
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.deleteFile}`+"/"+id);
  }

  public downloadFile(name: string) {
    const httpOptions = {
      responseType: 'blob' as 'json',
    };
    return this._httpClient.get<Resource>(`${AppConfig.ApiPaths.getFile}`+"/"+name, httpOptions);
  }
  public downloadFileById(id: string) {
    const httpOptions = {
      responseType: 'blob' as 'json',
    };
    return this._httpClient.get<any>(`${AppConfig.ApiPaths.getFileById}`+"/"+id, httpOptions);
  }
  public saveResource(resource: Resource,edit:boolean) {
    resource.creationDate = new Date();
    if(!edit){
      resource.approved = false;
    }
    return this._httpClient.post<Resource>(`${AppConfig.ApiPaths.saveResource}`, resource);
  }

  public searchResource(search: SearchModel) {
    return this._httpClient.post<Resource[]>(`${AppConfig.ApiPaths.searchResource}`, search);
  }

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

    const formData: FormData = new FormData();
    if(image != undefined) {
      formData.append("image", image);
    }
    if(logo != undefined) {
      formData.append("logo", logo);
    }
    resourceContent.forEach(r =>{
      formData.append("files",r);
    })
    return this._httpClient.post(`${AppConfig.ApiPaths.addFile}`+"/"+idResource,formData)
  }
  public addFile(file:File,description:string, type:string){

    const formData: FormData = new FormData();
    if(file != undefined) {
      formData.append("file", file);
    }
    if(description != undefined) {
      formData.append("description", description);
    }
    if(type != undefined) {
      formData.append("type", type);
    }

    return this._httpClient.post(`${AppConfig.ApiPaths.addHelpFile}`,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}`);
  }

  public getAllHelpFilesDesc() {
    return this._httpClient.get<FileDesc[]>(`${AppConfig.ApiPaths.getAllHelpFilesDesc}`);
  }
  public download(resource:Resource){
    return this._httpClient.post(`${AppConfig.ApiPaths.download}`,resource,{responseType: 'blob' as 'blob', observe: 'response'})
  }

}
