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

import { JumpResp, JumpReq } from "../models/jump";

import { DateService } from "./date.service";
import { BaseService } from "./base.service";

@Injectable()
export class JumpService extends BaseService {
  constructor(private http: HttpClient, 
              private dateService: DateService,
              private datePipe: DatePipe) {
    super();
  }

  public GetListOfJumps(): Observable<Array<JumpResp>> {
    return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`, 
                                          { headers: this.headers })
                    .pipe(
                      map((response) => {
                        const details = response.map((data) => new JumpResp(data));
                        return details;
                      })
                    );
  }

  public AddListOfJump(selectedJumpType: number,
                       selectedAircraft: number,
                       selectedDz: number,
                       selectedRig: number,
                       withCutaway: boolean,
                       beginDate: Date,
                       endDate: Date,
                       defaultExitAltitude: number,
                       defaultDeployAltitude: number,
                       countOfJumps: number,
                       notes: string,
                       isSpecial: boolean)
  {
    const diffInDays = this.dateService.DiffBetweenDates(beginDate, endDate) + 1;
    const countOfJumpsPerDay = Math.trunc(countOfJumps / diffInDays);

    for (let i = 1; beginDate.getTime() < endDate.getTime(); i++) {
      this.AddJumps(selectedJumpType,
                    selectedAircraft,
                    selectedDz,
                    selectedRig,
                    withCutaway,
                    beginDate,
                    defaultExitAltitude,
                    defaultDeployAltitude,
                    countOfJumpsPerDay,
                    notes,
                    isSpecial);

      beginDate = this.dateService.AddDays(beginDate, 1);
    }

    const restfJumps = countOfJumps - countOfJumpsPerDay * (diffInDays - 1);

    this.AddJumps(selectedJumpType,
                  selectedAircraft,
                  selectedDz,
                  selectedRig,
                  withCutaway,
                  beginDate,
                  defaultExitAltitude,
                  defaultDeployAltitude,
                  restfJumps,
                  notes,
                  isSpecial);
  }

  public DeleteJump(item: JumpResp) {
    this.http.delete(`${this.apiUrl}/Jump/${item.id}`,
                     { headers: this.headers, })
              .subscribe((data) => console.log(data));
  }

  public UpdateJump(id: number,
                    isSpecial: boolean, 
                    withCutaway: boolean, 
                    notes: string) {
    const jumpData = {
      id: id,
      isSpecial: isSpecial,
      withCutaway: withCutaway,
      notes: notes
    };  
    const bodyUpdatedJump = new JumpReq(jumpData);
    
    return this.http.put(`${this.apiUrl}/Jump/${id}`,
                         bodyUpdatedJump,
                         { headers: this.headers, });
  }

  private AddJumps(selectedJumpType: number,
                   selectedAircraft: number,
                   selectedDz: number,
                   selectedRig: number,
                   withCutaway: boolean,
                   jumpDate: Date,
                   defaultExitAltitude: number,
                   defaultDeployAltitude: number,
                   countOfJumps: number,
                   notes: string,
                   isSpecial: boolean) 
  {
    for (let i = 0; i < countOfJumps; i++) {
      const bodyNewjump: JumpReq = {
        jumpTypeId: selectedJumpType,
        aircraftId: selectedAircraft,
        dropZoneId: selectedDz,
        withCutaway: withCutaway,
        exitAltitude: defaultExitAltitude,
        deployAltitude: defaultDeployAltitude,
        gearId: selectedRig,
        notes: notes,
        id: 0,
        jumpDate: this.datePipe.transform(jumpDate, "yyyy-MM-dd"),
        isSpecial: isSpecial
      };

      this.http.post(`${this.apiUrl}/Jump`,
                     bodyNewjump, 
                     { headers: this.headers, })
                .subscribe((data) => console.log(data));
    }
  }
}
