import { Injectable } from "@angular/core";
import { DatePipe } from "@angular/common";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";

import { TunnelFlightReq, TunnelFlightResp, TunnelFlight, TunnelFlightByMonthResp, TunnelFlightByMonth } from "../models/tunnel-flight";
import { DropZoneResp } from '../models/dropzone';
import { JumpTypeResp } from '../models/jumpType';

import { BaseService } from "./base.service";
import { DropzoneService } from "./dropzone.service";
import { JumpTypeService } from "./jump-type.service";

@Injectable()
export class TunnelFlightService extends BaseService {
  constructor(private http: HttpClient,
              private datePipe: DatePipe,
              private jumpTypeService: JumpTypeService,
              private dropzoneService: DropzoneService) {
    super();
  }

  public addFlight(selectedTunnel: number,
                   selectedJumpType: number,
                   flightDate: Date,
                   nbMinutes: number,
                   comment: string) {

    const bodyNewFlight: TunnelFlightReq = {
      id: 0,
      tunnelId: selectedTunnel,
      jumpTypeId: selectedJumpType,
      flightDate: this.datePipe.transform(flightDate, "yyyy-MM-dd"),
      notes: comment,
      nbMinutes: nbMinutes
    };

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

  public getTunnelFlights(begin: Date, end: Date): Observable<Array<TunnelFlight>> {
    let beginDate = this.datePipe.transform(begin, "yyyy-MM-dd");
    let endDate = this.datePipe.transform(end, "yyyy-MM-dd");

    return this.http.get<Array<TunnelFlightResp>>(`${this.apiUrl}/TunnelFlight/${beginDate}/${endDate}`, { headers: this.headers })
                    .pipe(map(response => {
                        return this.mapWithDataInCache(response);
                      })
                    );
  }

  public deleteTunnelFlight(item: TunnelFlight) {
    this.http.delete(`${this.apiUrl}/TunnelFlight/${item.id}`, { headers: this.headers }).subscribe();
  }

  public getTunnelFlightsByMonth(begin: Date, end: Date): Observable<Array<TunnelFlightByMonth>> {
    let beginDate = this.datePipe.transform(begin, "yyyy-MM-dd");
    let endDate = this.datePipe.transform(end, "yyyy-MM-dd");

    return this.http.get<Array<TunnelFlightByMonthResp>>(`${this.apiUrl}/TunnelFlight/month/${beginDate}/${endDate}`, { headers: this.headers })
                    .pipe(
                      map(response => {
                        const stats = response.map(data => new TunnelFlightByMonth(data));
                        return stats;
                      })
                    );
  }

  private mapWithDataInCache(apiResp: Array<TunnelFlightResp>): Array<TunnelFlight> {
    let allDropzones: Array<DropZoneResp>;
    this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
    let allJumpType: Array<JumpTypeResp>;
    this.jumpTypeService.getFromCache().subscribe(data => { allJumpType = data; });

    return apiResp.map((data) => {
      let tmp = new TunnelFlight(data);
      tmp.tunnel = allDropzones.find(d => d.id == data.tunnelId);
      tmp.jumpType = allJumpType.find(d => d.id == data.jumpTypeId);

      return tmp;
    });
  }
}
