// workarea.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { MatchedResult } from '../matched-result.model'
let header = new HttpHeaders();
header = header.set('Content-Type', 'application/json; charset=utf-8');

@Injectable({
  providedIn: 'root'
})
export class WorkareaService {
  private apiUrl = `${environment.apiUrl}/WorkAreaApi`; // Update based on your API URL
  private serverServiceUrl = `${environment.serverserviceUrl}/api/WorkAreaApi`; 
private header = new HttpHeaders({ 'Content-Type': 'application/json' });

  constructor(private http: HttpClient) {}
  
  ServerPath(tranid: string, token: any) : Observable<string> {
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.http.get(this.apiUrl + '/GetServerPath?tranid=' + tranid, { headers: headers, responseType: 'text' });
  }

  getanalysisfile(tranid: string,lotid:string,loginuserid:string, token: any) : Observable<string> {
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.http.get(this.apiUrl + '/getxmlfile?tranid=' + tranid+ '&lotid=' + lotid+'&userid=' + loginuserid, { headers: headers, responseType: 'text' });
  }

  XmlReviewQC(tranid: string,jobid : string, lotid : string,islot :boolean, token: any ) : Observable<MatchedResult[]> { 
    debugger;
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token); 
    return this.http.get<MatchedResult[]>(this.apiUrl + '/XmlReviewQC?tranid=' + tranid + '&lotid=' + lotid+'&jobid=' + jobid+'&islot=' + islot, { headers: headers });
  }

  get(tranid: string, token: any) : Observable<string> {
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.http.get(this.apiUrl + '/GetServerPath?tranid=' + tranid, { headers: headers, responseType: 'text' });
  }
  
  InsertlotPageDetails(data: any, token: string): Observable<any> {
    debugger;
    const headers = new HttpHeaders({
      'Authorization': 'Bearer ' + token
    });
  
    return this.http.post<any>(`${this.apiUrl}/InsertPageDetails`, data, { headers });
  }
  
    

  GetLotTitles(jobid: string): Observable<any> {
    debugger;
    return this.http.get<any>(this.apiUrl + '/GetLotTitles?jobid='
    + jobid );
  }
  usrstagemove(data: any[], token: any): Observable<any> {
    debugger;
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.http.post<any>(`${this.apiUrl}/usrstagemove`, data, { headers: headers, observe: 'response' });
  }

  getJobDetails(data: any[], token: any): Observable<any> {   
    const headers = new HttpHeaders()
    .set('Content-Type', 'application/json; charset=utf-8')
    .set('Authorization', 'Bearer ' + token); 
    return this.http.post<any>(this.apiUrl + '/getJobDetails',
      data, { headers: headers, observe: 'response' });
  }

  PostUpdateLotTransaction(data: FormData, token: any): Observable<any> {
    const headers = new HttpHeaders()   
      .set('Authorization', 'Bearer ' + token);
    return this.http.post<any>(this.serverServiceUrl + '/PostUpdatelot_transaction_V1',
      data, { headers: headers, observe: 'response' });
  }

  GetAllFilesForDownload(jobid: number, tranid: number): Observable<any> {
    return this.http.get<any>(this.apiUrl + '/GetAllFilesForDownload?jobid='
    + jobid + '&tranid=' + tranid);
  }

  GetAllOutputFiles(jobid: number, tranid: number): Observable<any> {
    return this.http.get<any>(this.apiUrl + '/GetAllOutputFiles?jobid='
    + jobid + '&tranid=' + tranid);
  }

DownloadSingleFile(fileData: any): Observable<HttpResponse<Blob>> {
  return this.http.post(`${this.apiUrl}/DownloadSingleFile`, fileData, {
    headers: this.header,
    observe: 'response',
    responseType: 'blob'
  }) as Observable<HttpResponse<Blob>>;
}

 DownloadInOutFiles(fileData: any): Observable<any> {     
   return this.http.post<any>(this.apiUrl + '/DownloadInOutFiles',
   fileData, { headers: header, observe: 'response' });
}

DownloadSingleOutFile(fileData: any): Observable<any> {     
 return this.http.post<any>(this.apiUrl + '/DownloadSingleOutFile',
 fileData, { headers: header, observe: 'response' });
}

RemoveIOFiles(fileData: any): Observable<any> {     
 return this.http.post<any>(this.apiUrl + '/RemoveIOFiles',
 fileData, { headers: header, observe: 'response' });
}



GetAllOutFilesForDownload(jobid: number, tranid: number): Observable<any> {
  return this.http.get<any>(this.apiUrl + '/GetAllOutFilesForDownload?jobid='
  + jobid + '&tranid=' + tranid );
}

saveLots(lots: any[]): Observable<any> {
  debugger;
  return this.http.post<any>(this.apiUrl + '/SaveLots', lots);
}



getreporterkey(jobtitle:string,jobId: number,  token: string): Observable<any> {
  debugger;
  const headers = { Authorization: `Bearer ${token}` };

  return this.http.get<any>(
    `${this.apiUrl}/getreporterkey?jobid=${jobId}&jobtitle=${jobtitle}`,
    { headers }
  );
}



}
