import { Injectable } from '@angular/core';
import { Attachment, EventAttachment } from '../models/attachment.model';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { AuthService } from '../services/auth.service';
import { LoginUserRespose } from '../models/user.model';

@Injectable({
  providedIn: 'root'
})
export class AttachmentsService {

  loggeduser: LoginUserRespose;
  headers: HttpHeaders;

  constructor(private http: HttpClient, user: AuthService) {
    this.loggeduser = user.ValidatedUser;
    this.headers = new HttpHeaders();
    this.headers = this.headers.set('Authorization', this.loggeduser.BasicAuthKey);
  }

  getAttachmentsBySerial(serialNumber: string): Observable<Attachment[]>{
    let geturl = environment.apiURL + 'attachments/GetAttachments';
    let params = new HttpParams();

    params = params.set("SerialNumber", serialNumber);

    let options = {
      headers: this.headers,
      params: params
    };
    return this.http.get<Attachment[]>(`${geturl}`, options);
  }

  getAttachments(source: number, sourceid: string): Observable<Attachment[]> {
    let geturl = environment.apiURL + 'attachments/GetSourceName';
    let params = new HttpParams();

    params = params.set("Source", source.toString());
    /* params = params.set("SourceId", sourceid); */

    let options = {
      headers: this.headers,
      params: params
    };

    return this.http.get<Attachment[]>(`${geturl}`, options);
  }

  getAttachmentsById(eventId: string): Observable<Attachment[]> {
    let geturl = environment.apiURL + 'attachments/GetAttachmentsByEventId';
    let params = new HttpParams();

    params = params.set("ParentId", eventId);
    /* params = params.set("SourceId", sourceid); */

    let options = {
      headers: this.headers,
      params: params
    };

    return this.http.get<Attachment[]>(`${geturl}`, options);
  }

  setEventAttachmentById(body: any) {

    let geturl = environment.apiURL + 'Attachments/Create';
    let options = {
      headers: this.headers,
    }
    return this.http.post(`${geturl}`, body,  options);
  }

  deleteEventAttachmentById(attachmentId) {

    let geturl = environment.apiURL + 'Attachments/Delete';

    let params = new HttpParams();

    params = params.set("attachmentId", attachmentId);

    let options = {
      headers: this.headers,
      params: params
    }

    return this.http.delete(`${geturl}`,  options);
  }

  AddAttachment(body: any) {

    let geturl = environment.apiURL + 'Attachments/CreateAttachmentProcess';
    let options = {
      headers: this.headers,
    }
    return this.http.post(`${geturl}`, body,  options);
  }

}
