import { Injectable } from '@angular/core';
import { Observable } from "rxjs";
import { ApiRequestService } from "../services/api/api-request.service";
import { HttpParams } from "@angular/common/http";
import { saveAs } from 'file-saver';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import baseUrl from './api/helper';
@Injectable({
  providedIn: 'root'
})
export class CsvService {
  private getallheadersURL = "api/csv/getallheaders";
    private SaveURL = "api/template/save";
    private downloadURL = "api/csv/download";
 private purl="api/template/demo/download";
  constructor(private apiRequest: ApiRequestService,
    private http: HttpClient) { }


  importCsv(file: File,data:any):Observable<any>{
    const formData = new FormData();
    formData.append('file', file);
    return this.apiRequest.postFormData(`${this.SaveURL}/${data}`,formData);
    //return this.http.post('http://localhost:9191/api/csv/save', formData);
  }

  getAllCsv(): Observable<any>{
    let me = this;
    let params: HttpParams = new HttpParams();
    //params = params.set('file', file);
    return this.apiRequest.get(this.getallheadersURL, params);
  }

  downloadCsv(): Observable<any>{
    let me = this;
    let params: HttpParams = new HttpParams();
    //params= params.set('file', file);
      return this.apiRequest.get(this.downloadURL, params);
  }

  // working
  // downloadCsvsprio(data:any): void {
  //   this.http.get(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_priority2_t');
  //   });
  // }

  // downloadCsvsprio(data:any): void {
  //   this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_priority2_t');
  //   });
  // }

  // downloadCsvsimpa(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_impact2_t');
  //   });
  // }
  // downloadCsvsurge(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_urgency_t');
  //   });
  // }
  // downloadCsvscat(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_category2_t');
  //   });
  // }
  // downloadCsvsstate(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_State_t');
  //   });
  // }
  // downloadCsvscontact(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_Contact_type_t');
  //   });
  // }
  // downloadCsvscust(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_customer_t');
  //   });
  // }
  // downloadCsvshan(data:any): void {
  //   this.apiRequest.getFile(`${baseUrl}/api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
  //     saveAs(data, 'Sr_handler_t');
  //   });
  // }

  downloadCsvs(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, data+'_t');
    });
  }

  downloadCsvsprio(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_priority2_t');
    });
  }
  downloadCsvsimpa(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_impact2_t');
    });
  }
  downloadCsvsurge(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_urgency_t');
    });
  }
  downloadCsvscat(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_category2_t');
    });
  }
  downloadCsvsstate(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_State_t');
    });
  }
  downloadCsvscontact(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_Contact_type_t');
    });
  }
  downloadCsvscust(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_customer_t');
    });
  }
  downloadCsvshan(data:any): void {
    this.apiRequest.getFile(`api/template/demo/download/${data}`, { responseType: 'blob' }).subscribe(data => {
      saveAs(data, 'Sr_handler_t');
    });
  }
}
