import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { RegexPattern } from '../regex-pattern.model'
import { MatchedResult } from '../matched-result.model'

@Injectable({
  providedIn: 'root'
})
export class RegexPatternService {
  private apiUrl = `${environment.apiUrl}/RegexReplaceApi`;

  constructor(private http: HttpClient) { }

  // Method to fetch all regex patterns
  getRegexPatterns(token : any): Observable<RegexPattern[]> {
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json; charset=utf-8')
      .set('Authorization', 'Bearer ' + token);
    return this.http.get<RegexPattern[]>(`${this.apiUrl}/GetPatterns`, { headers: headers });
  }

  previewChanges(directoryPath: string, searchTerm: string, replacementText: string, fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean, token : any): Observable<MatchedResult[]> {
    const payload = {
      DirectoryPath: directoryPath || '',
      SearchTerm: searchTerm || '',
      ReplacementText: replacementText || '',
      FileType: fileType || '',
      CaseInsensitive: caseInsensitive || false,
      SingleLine: singleLine || false,
      MultiLine: multiLine || false,
      IgnoreWhitespace: ignoreWhitespace || false,
      ExplicitCapture: explicitCapture || false,
  };
    
    const headers = new HttpHeaders()
    .set('Content-Type', 'application/json; charset=utf-8')
    .set('Authorization', 'Bearer ' + token);
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/Preview`, payload, { headers: headers });
  }

  search(directoryPath: string, searchTerm: string, replacementText: string, fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean, token : any): Observable<MatchedResult[]> {
    const payload = {
      DirectoryPath: directoryPath || '',
      SearchTerm: searchTerm || '',
      ReplacementText: replacementText || '',
      FileType: fileType || '',
      CaseInsensitive: caseInsensitive || false,
      SingleLine: singleLine || false,
      MultiLine: multiLine || false,
      IgnoreWhitespace: ignoreWhitespace || false,
      ExplicitCapture: explicitCapture || false,
  };
    
    const headers = new HttpHeaders()
    .set('Content-Type', 'application/json; charset=utf-8')
    .set('Authorization', 'Bearer ' + token);
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/Search`, payload, { headers: headers });
  }

  applyChanges(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}/Apply`, data, { headers: headers });
   }

   replaceAll(directoryPath: string, searchTerm: string, replacementText: string, fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean, token: any): Observable<any> {   
    const payload = {
      DirectoryPath: directoryPath || '',
      SearchTerm: searchTerm || '',
      ReplacementText: replacementText || '',
      FileType: fileType || '',
      CaseInsensitive: caseInsensitive || false,
      SingleLine: singleLine || false,
      MultiLine: multiLine || false,
      IgnoreWhitespace: ignoreWhitespace || false,
      ExplicitCapture: explicitCapture || false,
  };
    
    const headers = new HttpHeaders()
    .set('Content-Type', 'application/json; charset=utf-8')
    .set('Authorization', 'Bearer ' + token);  
    return this.http.post<any>(`${this.apiUrl}/ReplaceAll`, payload, { headers: headers } );
  }

  accept(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}/Accept`, data, { headers: headers });
     }

     reject(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}/Reject`, data, { headers: headers });
       }
  
   uploadFiles(files: File[], searchTerm: string, fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean): Observable<MatchedResult[]> {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('Files', file);
    });
    formData.append('SearchTerm', searchTerm);    
    formData.append('FileType', fileType);
    formData.append('CaseInsensitive', String(caseInsensitive));
    formData.append('SingleLine', String(singleLine));
    formData.append('MultiLine', String(multiLine));
    formData.append('IgnoreWhitespace', String(ignoreWhitespace));
    formData.append('explicitCapture', String(explicitCapture));
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/Upload`, formData);
  }

  readFilesFromServer(directoryPath: string, searchTerm: string, fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean): Observable<MatchedResult[]> {       

    const formData = new FormData();      
    formData.append('DirectoryPath', directoryPath);    
    formData.append('SearchTerm', searchTerm);
    formData.append('FileType', fileType);
    formData.append('CaseInsensitive', String(caseInsensitive));
    formData.append('SingleLine', String(singleLine));
    formData.append('MultiLine', String(multiLine));
    formData.append('IgnoreWhitespace', String(ignoreWhitespace));
    formData.append('explicitCapture', String(explicitCapture));   
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/PerformSearch`, formData);

  }

  
  
  uploadFiless(files: File[], searchTerms: string[], fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean): Observable<MatchedResult[]> {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('Files', file);
    });  
    searchTerms.forEach((term, index) => {
      formData.append(`searchTerms[${index}]`, term);  // Append search terms one by one
    });
    formData.append('FileType', fileType);
    formData.append('CaseInsensitive', String(caseInsensitive));
    formData.append('SingleLine', String(singleLine));
    formData.append('MultiLine', String(multiLine));
    formData.append('IgnoreWhitespace', String(ignoreWhitespace));
    formData.append('explicitCapture', String(explicitCapture));
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/Upload`, formData);
  }

  readFilesFromServers(directoryPath: string, searchTerms: string[], fileType: string, caseInsensitive: boolean, singleLine: boolean, multiLine: boolean, ignoreWhitespace: boolean, explicitCapture: boolean): Observable<MatchedResult[]> {       
    const formData = new FormData();      
    formData.append('DirectoryPath', directoryPath); 
      searchTerms.forEach((term, index) => {
        formData.append(`searchTerms[${index}]`, term);  // Append search terms one by one
      });
    formData.append('FileType', fileType);
    formData.append('CaseInsensitive', String(caseInsensitive));
    formData.append('SingleLine', String(singleLine));
    formData.append('MultiLine', String(multiLine));
    formData.append('IgnoreWhitespace', String(ignoreWhitespace));
    formData.append('explicitCapture', String(explicitCapture));   
    return this.http.post<MatchedResult[]>(`${this.apiUrl}/PerformSearch`, formData);
  } 

}
