import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Application } from '../entities/Application';
import { ApplicationStatus } from '../entities/ApplicationStatus';

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

  constructor(
    private _httpClient: HttpClient
  ) {}

  getAllStatus(): Promise<ApplicationStatus[]> {
    return this._httpClient.get<ApplicationStatus[]>('/api/applications/status').toPromise();
  }

  add(application: Application): Promise<Application> {
    const headers = new HttpHeaders()
        .append('Content-Type', 'application/json');
    return this._httpClient.post<Application>('/api/applications', application).toPromise();
  }

  update(application: Application): Promise<void> {
    const headers = new HttpHeaders()
        .append('Content-Type', 'application/json');
    return this._httpClient.put<void>(`/api/applications/${application.id}`, application, { headers }).toPromise();
  }
}
