import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
let header = new HttpHeaders();
header = header.set('Content-Type', 'application/json; charset=utf-8');
@Injectable({
  providedIn: 'root'
})
export class WorkareaService {
  baseUrl: string = environment.serviceUrl;
  serverBaseUrl: string = environment.serverserviceUrl;
  constructor(private httpClient: HttpClient) { }

  uploadChunk(formData: FormData,token: any) {
     const headers = new HttpHeaders()
     .set('Authorization', 'Bearer ' + token);
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/chunk', 
      formData, {headers:headers,observe: "response"});
  }

  getCheckout(data: any[]): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/getCheckout',
      data, { headers: header, observe: 'response' });
  }
getOcrStage(jobId: number): Observable<any> {
  const url = `${this.baseUrl}api/WorkAreaApi/getLotDetailsOCR`;
  const body = { jobId }; // ES6 shorthand
  //return this.httpClient.post<any>(url, body, { observe: 'response' });
   return this.httpClient.post(url, body, { responseType: 'text' });
}



  // usrstagemove(data: any[], token: any): Observable<any> {
  //   const headers = new HttpHeaders()
  //     .set('Content-Type', 'application/json; charset=utf-8')
  //     .set('Authorization', 'Bearer ' + token);
  //   return this.httpClient.post<any>(this.serverBaseUrl + 'api/DashboardApi/usrstagemove',
  //     data, { headers: headers, observe: 'response' });
  // }
  usrstagemove(data: any[], token: any): Observable<any> {
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/usrstagemove',
      data, { headers: headers, observe: 'response' });
  }
  getJobDetails(data: any[]): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/getJobDetails',
      data, { headers: header, observe: 'response' });
  }
  getLotDetailsComments(data: any[]): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/getLotDetailsComments',
      data, { headers: header, observe: 'response' });
  }
  ValidateOutFileExist(jobid: number, tranid: number,stagename_alias:string,projectdefinitionid:number,lottitle:string): Observable<any> {
    debugger;
    return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/ValidateOutFileExist?jobid='
    + jobid + '&tranid=' + tranid+'&stagename_alias=' + stagename_alias+ '&projectdefinitionid=' + projectdefinitionid+'&lottitle='+lottitle);
  }

  jobBackForward(data: any, token: any): Observable<any> {
    debugger;
    const headers = new HttpHeaders()
    .set('Authorization', 'Bearer ' + token);
    return this.httpClient.post<any>(this.serverBaseUrl + 'api/DashboardApi/jobBackForward',
        data, { headers: headers, observe: 'response' });
}

getdispatchwating(jobid: number, tranid: number ,lotid: number,project_subtitle1: string): Observable<any> {
  debugger;
  return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/getdispatchid?jobid='
  + jobid + '&tranid=' + tranid + '&lotid=' + lotid+'&projectsubtitle=' + project_subtitle1 ,{observe: 'response'} );
}

  PostUpdateLotTransaction1(data: FormData): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/PostUpdateLotTransaction',
      data, {  observe: 'response' });
  }
  PostUpdateLotTransaction(data: FormData, token: any): Observable<any> {
    debugger;
    const headers = new HttpHeaders()
    //  .set('Content-Type', 'multipart/form-data')
      .set('Authorization', 'Bearer ' + token);
    return this.httpClient.post<any>(this.serverBaseUrl + 'api/WorkAreaApi/PostUpdatelot_transaction_V1',
      data, { headers: headers, observe: 'response' });
  }
  DownloadFormFile(fileData: any): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/DownloadFormFile', fileData,
        { headers: header, observe: 'response' });
  }
  saveOutputFile(data: FormData, token: any): Observable<any> {
    const headers = new HttpHeaders()
     .set('Authorization', 'Bearer ' + token);
     return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/saveOutputFile',
       data, { headers: headers, observe: 'response' });
  }
  GetAllOutputFiles(jobid: number, tranid: number): Observable<any> {
    return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/GetAllOutputFiles?jobid='
    + jobid + '&tranid=' + tranid);
  }
  GetAllOutFilesForDownload(jobid: number, tranid: number): Observable<any> {
    return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/GetAllOutFilesForDownload?jobid='
    + jobid + '&tranid=' + tranid );
  }

getocrpdffile(jobid: number, lotid: number): Observable<any> {
    return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/Getocrpdffile?jobid='
    + jobid + '&tranid=' + lotid );
  }


  savefilechecker(data: FormData,token: any)
  {
      const headers = new HttpHeaders()
     .set('Authorization', 'Bearer ' + token);
     return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/savefilechecker',
       data, { headers: headers, observe: 'response' });
  }

  
  GetAllFilesForDownload(jobid: number, tranid: number): Observable<any> {
    return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/GetAllFilesForDownload?jobid='
    + jobid + '&tranid=' + tranid);
  }
  DownloadSingleFile(fileData: any): Observable<any> {     
     return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/DownloadSingleFile',
     fileData, { headers: header, observe: 'response' });
  }

  DownloadInOutFiles(fileData: any): Observable<Blob> {
  return this.httpClient.post(
    this.baseUrl + 'api/WorkAreaApi/DownloadInOutFiles',
    fileData,
    {
      headers: header,
      responseType: 'blob', // Expect binary data
    }
  );
}

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

RemoveIOFiles(fileData: any): Observable<any> {     
  return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/RemoveIOFiles',
  fileData, { headers: header, observe: 'response' });
}
getchecklisttransaction(tranid, token: any): Observable<any> {
  const header = new HttpHeaders()
  .set('Authorization', 'Bearer ' + token);
  return this.httpClient.get<any>(this.serverBaseUrl + 'api/WorkAreaApi/getchecklistdetails/' + tranid ,{headers: header});
}


PostInsertchecklist(data: any[], token: any): Observable<any> {
  const header = new HttpHeaders()
  .set('Authorization', 'Bearer ' + token);
    return this.httpClient.post<any>(this.serverBaseUrl + 'api/WorkAreaApi/InsertchecklistTransaction',
          data, { headers: header, observe: 'response'});
}
getPreviousStageTranid(tranid, token: any): Observable<any> {
  const header = new HttpHeaders()
  .set('Authorization', 'Bearer ' + token);
  return this.httpClient.get<any>(this.serverBaseUrl + 'api/WorkAreaApi/GetPreviousStageTranid/' + tranid , {headers: header} );
}

downloadFile(path: string, title: string, jobid: string): Observable<Blob> {
  debugger;
  const encodedPath = encodeURIComponent(path);
  const encodedTitle = encodeURIComponent(title);
  const encodedJobId = encodeURIComponent(jobid);

  return this.httpClient.get(
    `${this.baseUrl}api/WorkAreaApi/DownloadFile?path=${encodedPath}&title=${encodedTitle}&jobid=${encodedJobId}`,
    { responseType: 'blob' } // ensures response is treated as binary
  );
}




getOutfileExtension(tranid: number): Observable<any> {
  return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/GetOutfileExtension?tranid=' + tranid);
}
getredatOutfile(tranid: number): Observable<any> {
  return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/GetredatOutfile?tranid=' + tranid);
}
getOutfileupdated(tranid: number,option:string): Observable<any> {
  return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/getOutfileupdated?tranid=' + tranid+'&option='+option);
}
sendmailfordispatch(jobtitle: string): Observable<any> {
  debugger;
  return this.httpClient.get<any>(this.baseUrl + 'api/WorkAreaApi/sendmailfordispatch?jobtitle=' + jobtitle);
}
appgetloader(tranid: number, userid: number, enviroment: string): Observable<any>{
  return this.httpClient.get<any>('http://localhost:60024/?transactionId=' + tranid + '&userId=' + userid + '&env=' + enviroment);
}


forwardstage(data){
    debugger;
    return this.httpClient.post<any>(this.serverBaseUrl + 'api/DashboardApi/forwardstage',
        data, { headers: header, observe: 'response' });
  }
//hari
saveWorkflowDecision(data: any[]): Observable<any> {
    return this.httpClient.post<any>(this.baseUrl + 'api/WorkAreaApi/workflowdecision',
      data, { headers: header, observe: 'response' });
  }
pushBack(data: FormData, token: any) {

  const header = new HttpHeaders()
    .set('Authorization', 'Bearer ' + token);

  return this.httpClient.post<any>(
    this.baseUrl + 'api/WorkAreaApi/pushback',
    data,
    { headers: header, observe: 'response' }
  );
}


/* async appgetloader1(tranid: number, userid: number, enviroment: string): Promise<any> {
  try {
    return await this.httpClient.get<any>('http://localhost:60024/?transactionId=' + tranid + '&userId=' + userid + '&env=' + enviroment).toPromise();
  } catch (error) {
    console.error('Error fetching data', error);
    throw error;
  } 
}*/
}
