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

@Injectable({
  providedIn: 'root'
})
export class DatamanagementService {
  private URL = environment.backendUrl;
  private updateUrl = "api/template/dynamic/dynamic_temp"
  constructor(private apiRequest: ApiRequestService, private _http: HttpClient) { }
  getall() {
    return this.apiRequest.get(`api/template/getalltemplate`);
  }

  // getallentity(){
  //   return this.apiRequest.get(`api/template/databasequery/Table_list_all`);
  // }

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

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

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

  // create(data:any,files:any): Observable<any> {
  //   const s = JSON.stringify(data);
  //   const formData:FormData = new FormData();
  //   formData.append("data",s);
  //   formData.append("file",files);

  //   return this.apiRequest.postFormData(`sureserve/template/dynamic/dynamic_temp`,formData);
  //   }

  create(file: any, fileType: string): Observable<any> {
    const formData: FormData = new FormData();
    formData.append('file', file);
    //   let params: HttpParams = new HttpParams();
    // params =params.append('fileType', fileType);
    return this.apiRequest.postFormData(`api/template/save/${fileType}`, formData);
  }

  update(id: number, data: any, files: any): Observable<any> {
    const s = JSON.stringify(data);
    const formData: FormData = new FormData();
    formData.append("data", s);
    formData.append("file", files);
    return this.apiRequest.postFormData(`${this.updateUrl}/${id}`, formData);
  }

  delete(id: any) {
    return this.apiRequest.delete(`api/template/deletetemplate/${id}`);
  }

  // getjson(){
  //   return this.apiRequest.get(`api/dynamic/jsonkeychange`);
  // }

  changeKeysOfJson(newjson: string, body: any): Observable<any> {
    // const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    // let params: HttpParams = new HttpParams();
    // params =params.append('newjson', newJson);
    // params =params.append('body', body);
    const formData: FormData = new FormData();
    formData.append("newjson", newjson);
    formData.append("body", body);
    return this.apiRequest.postFormData(`api/dynamic/jsonkeychange`, formData);
  }


  saveTemplateInJson(id: any): Observable<any> {
    // const formData: FormData = new FormData();
    // formData.append('file', file);
    // console.log(formData, file);

    // let params: HttpParams = new HttpParams();
    // params =params.append("file",file);
    return this.apiRequest.get(`api/dynamic/templatetojson/${id}`);
  }

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


  downloadExcel(tableName: string, data: any): Observable<any> {
    // return this.apiRequest.post(`api/dynamic/DownloadExcel/${tableName}`, data);
    const url = `${this.URL}/api/dynamic/DownloadExcel/${tableName}`;
    return this._http.post(url, data, { responseType: 'blob' }).pipe(
      map((response: Blob) => {
        // Process the response as needed
        return response;
      })
    );
  }



}
