import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { AircraftResp, AircraftReq } from '../models/aircraft';

import { BaseService } from './base.service';
import { RefData } from '../models/ref-data.enum';

@Injectable()
export class AircraftService extends BaseService {
  constructor(private http: HttpClient) {
    super();
  }

  public getListOfAircrafts(simple: boolean = false): Observable<Array<AircraftResp>> {
    let url : string;
    if (simple === true) {
      url = `${this.apiUrl}/Aircraft/GetSimple`;
    } else {
      url = `${this.apiUrl}/Aircraft`;
    }

    let callToApi = this.http.get<Array<AircraftResp>>(url, { headers: this.headers });
    return this.serviceRefData.get<Array<AircraftResp>>(RefData.Aircraft, callToApi);
  }

  public AddAircraft(aircraftName: string, dataImg: string) {
    const bodyNewAircraft: AircraftReq = {
      id: 0,
      name: aircraftName,
      imageData: dataImg
    };

    return this.http.post(`${this.apiUrl}/Aircraft`, 
                          bodyNewAircraft, 
                          { headers: this.headers });
  }
}
