import { Component, OnInit } from "@angular/core";
import { JumpTypeResp } from "../../models/jumpType";
import { AircraftResp } from "../../models/aircraft";
import { DropZoneResp } from "../../models/dropzone";
import { DateService } from "../../services/date.service";
import { GearResp } from "../../models/gear";
import { ServiceComm } from "../../services/service-comm.service";
import { DropzoneService } from "../../services/dropzone.service";
import { AircraftService } from "../../services/aircraft.service";
import { JumpService } from "../../services/jump.service";
import { JumpTypeService } from "../../services/jump-type.service";
import { GearService } from "../../services/gear.service";
import { isNumber } from "util";

@Component({
  selector: "app-new-jump",
  templateUrl: "./new-jump.component.html",
  styleUrls: ["./new-jump.component.css"]
})
export class NewJumpComponent implements OnInit {
  beginDate: Date;
  endDate: Date;
  exitAltitude: number;
  deployAltitude: number;
  countOfJumps: number;
  selectedDz: DropZoneResp;
  selectedGear: GearResp;
  selectedAircraft: AircraftResp;
  selectedJumpType: JumpTypeResp;
  withCutaway: boolean;
  listOfJumpType: Array<JumpTypeResp>;
  listOfAircraft: Array<AircraftResp>;
  private listOfDropZone: Array<DropZoneResp>;
  listOfFilteredDropZone: Array<DropZoneResp>;
  listOfGear: Array<GearResp>;
  private countDatasLoaded: number;

  constructor(
    private serviceComm: ServiceComm,
    private serviceJump: JumpService,
    private serviceJumpType: JumpTypeService,
    private serviceAircraft: AircraftService,
    private serviceDropzone: DropzoneService,
    private serviceGear: GearService,
    private dateService: DateService
  ) {}

  ngOnInit() {
    this.serviceComm.UpdatedComponentTitle("Add a new jump");

    this.endDate = new Date();
    this.beginDate = this.dateService.AddDays(new Date(), -1);

    this.exitAltitude = 4000;
    this.deployAltitude = 1500;
    this.countOfJumps = 1;

    this.getListOfJumpTypes();
  }

  onFormSubmit() {
    this.serviceJump.AddListOfJump(
      this.selectedJumpType.id,
      this.selectedAircraft.id,
      this.selectedDz.id,
      this.selectedGear.id,
      this.withCutaway,
      this.beginDate,
      this.endDate,
      this.exitAltitude,
      this.deployAltitude,
      this.countOfJumps
    );
  }

  public isValidatedForm(): boolean {
    return (
      this.selectedDz !== undefined &&
      this.selectedDz.id !== undefined &&
      this.selectedGear !== undefined &&
      this.selectedGear.id !== undefined &&
      this.selectedAircraft !== undefined &&
      this.selectedAircraft.id !== undefined &&
      this.selectedJumpType !== undefined &&
      this.selectedJumpType.id !== undefined &&
      this.exitAltitude !== undefined &&
      isNumber(this.exitAltitude) &&
      this.deployAltitude !== undefined &&
      isNumber(this.deployAltitude) &&
      this.countOfJumps !== undefined &&
      isNumber(this.countOfJumps)
    );
  }

  private getListOfJumpTypes() {
    this.serviceJumpType.getListOfJumpTypes().subscribe(data => {
      this.listOfJumpType = data;
      this.countDatasLoaded = 1;

      this.getListOfAircrafts();
      this.getListOfDropZones();
      this.getListOfGears();
    });
  }

  private getListOfAircrafts() {
    this.serviceAircraft.getListOfAircrafts().subscribe(data => {
      this.listOfAircraft = data;
      this.countDatasLoaded++;
    });
  }

  private getListOfDropZones() {
    this.serviceDropzone.getListOfDropZones().subscribe(data => {
      data.sort((a, b) => (b.isFavorite ? 1 : 0) - (a.isFavorite ? 1 : 0));
      this.listOfDropZone = data;
      this.listOfFilteredDropZone = data;
      this.countDatasLoaded++;
    });
  }

  private getListOfGears() {
    this.serviceGear.getListOfGears().subscribe(data => {
      this.listOfGear = data;
      this.countDatasLoaded++;
    });
  }

  public displayFn(data?: JumpTypeResp): string | undefined {
    return data ? data.name : undefined;
  }

  public onChangeDz(event: string) {
    const filterValue = event.toLowerCase();

    this.listOfFilteredDropZone = this.listOfDropZone;
    this.listOfFilteredDropZone = this.listOfFilteredDropZone.filter(option =>
      option.name.toLowerCase().includes(filterValue)
    );
  }

  public allDatasLoaded(): boolean {
    return this.countDatasLoaded === 4;
  }
}
