import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ApiRequestService } from '../api/api-request.service';
import baseUrl from '../api/helper';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class BulkimportService {
  private URL = environment.backendUrl;
  constructor(private http: HttpClient,private apiRequest: ApiRequestService) { }

  public saveData(data: any): Observable<any> {
    return this.http.post(`${baseUrl}/BulkUpload/BulkUpload`, data);
  }

  public getDetails(): Observable<any> {
    return this.http.get(`${baseUrl}/BulkUpload/BulkUpload`);
  }

  public getDetailsById(id: number): Observable<any> {
    return this.http.get(`${baseUrl}/BulkUpload/BulkUpload/${id}`);
  }

  public deleteById(id: number): Observable<any> {
    return this.http.delete(`${baseUrl}/BulkUpload/BulkUpload/${id}`);
  }

  public updateData(data: any, id: number): Observable<any> {
    return this.http.put(`${baseUrl}/BulkUpload/BulkUpload/${id}`, data);
  }


  //rule
  condition:string = '';
  setCondition(condition: string) {
    this.condition = condition;
  }
  
  getCondition(): string {
    return this.condition;
  }

  getSheetName(id: number): Observable<any>{
    return this.apiRequest.get(`api/BulkUpload/sheet/${id}`);
  }

  convertFileToJson(id: number): Observable<any> {
    return this.apiRequest.post(`api/BulkUpload/templatetojson/${id}`, {});
  }

  // getTemplateById(id: number): Observable<any> {
  //   const url = `api/template/gettemplatebyid/${id}`;
  //   return this.apiRequest.get(url);
  // }

  getHeader(id: number): Observable<any> {
    const url = `api/BulkUpload/getHeaders/${id}`;
    return this.apiRequest.get(url);
  }

  getColumnNames(tableName: string): Observable<any> {
    const url = `api/BulkUpload/columns/${tableName}`;
    return this.apiRequest.get(url);
  }
  
  changeKeysOfJson(updatedKeyJson: string, oldKeyDataJson:any): Observable<any> {
    const formData:FormData=new FormData();
    formData.append("updatedKeyJson",updatedKeyJson);
    formData.append("oldKeyDataJson",oldKeyDataJson);
    return this.apiRequest.postFormData(`api/BulkUpload/jsonkeychange`, formData);
  }
  
  downloadExcel(id: string, jsonData:any, ruleData:any): Observable<any> {
    // return this.apiRequest.post(`api/dynamic/DownloadExcel/${tableName}`, data);
  jsonData = JSON.stringify(jsonData);
  ruleData = JSON.stringify(ruleData);
    const formData:FormData=new FormData();
    formData.append("jsonData",jsonData);
    formData.append("ruleData",ruleData);
    const url = `${this.URL}/api/BulkUpload/DownloadExcel/${id}`;
    return this.apiRequest.postFormData(`api/BulkUpload/DownloadExcel/${id}`, formData);
    // return this.http.post(url, formData, { responseType: 'blob' }).pipe(
    //   map((response: Blob) => {
    //     // Process the response as needed
    //     return response;
    //   })
    // );
  }

  ////for download
  getSheet(id: number): Observable<any> {
    const url = `${this.URL}/api/BulkUpload/downloadImportStatement/${id}`;
    // return this.apiRequest.get(url);
        return this.http.get(url, { responseType: 'blob' }).pipe(
      map((response: Blob) => {
        // Process the response as needed
        return response;
      })
    );
  }

  ///////////////////////
  //mapping rules
  public saveMappingData(data: any): Observable<any> {
    return this.http.post(`${baseUrl}/MappingRule/MappingRule`, data);
  }

  public getMappingDetails(): Observable<any> {
    return this.http.get(`${baseUrl}/MappingRule/MappingRule`);
  }

  public getMappingDetailsById(id: number): Observable<any> {
    return this.http.get(`${baseUrl}/MappingRule/MappingRule/${id}`);
  }

  public deleteMappingById(id: number): Observable<any> {
    return this.http.delete(`${baseUrl}/MappingRule/MappingRule/${id}`);
  }

  public updateMappingData(data: any, id: number): Observable<any> {
    return this.http.put(`${baseUrl}/MappingRule/MappingRule/${id}`, data);
  }

  getDDforMappingRule(excelFile:any, sheetName:string){
    console.log(sheetName,excelFile)
      const formData:FormData=new FormData();
      formData.append("excelFile",excelFile);
      formData.append("sheetName",sheetName);
      return this.apiRequest.postFormData(`MappingRule/getHeaders`, formData);
  }


  /////////////////////
  // master

  mastergetexcelSheet(file_type: string): Observable<any> {
    const url = `masterimport/demo/download/${file_type}`;
    return this.apiRequest.get(url);
  }

  masterdownloadExcel(id: string, jsonData:any,): Observable<any> {
  jsonData = JSON.stringify(jsonData);
    const formData:FormData=new FormData();
    formData.append("jsonData",jsonData);
    return this.apiRequest.postFormData(`masterimport/DownloadExcelForMaster/${id}`, formData);
  }

  mastergetSheetName(id: number): Observable<any>{
    return this.apiRequest.get(`masterimport/sheet/${id}`);
  }

  mastergetColumnNames(tableName: string): Observable<any> {
    const url = `masterimport/columns/${tableName}`;
    return this.apiRequest.get(url);
  }
}
