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";
import { ThrowStmt } from "@angular/compiler";

@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;
  private pendingAddRequest: boolean;
  comments: string;

  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.pendingAddRequest = false;

    this.initForm();

    this.getListOfJumpTypes();
  }

  onFormSubmit() {
    this.pendingAddRequest = true;

    this.serviceJump.AddListOfJump(
      this.selectedJumpType.id,
      this.selectedAircraft.id,
      this.selectedDz.id,
      this.selectedGear.id,
      this.withCutaway === undefined ? false : this.withCutaway,
      this.beginDate,
      this.endDate,
      this.exitAltitude,
      this.deployAltitude,
      this.countOfJumps,
      this.comments
    );

    setTimeout(() => {
      this.initForm();
      this.pendingAddRequest = false;
    }, 1000);
  }

  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 &&
      typeof this.exitAltitude === "number" &&
      this.deployAltitude !== undefined &&
      typeof this.deployAltitude === "number" &&
      this.countOfJumps !== undefined &&
      typeof this.countOfJumps === "number"
    );
  }

  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++;
    });
  }

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

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

    this.selectedDz = undefined;
    this.selectedGear = undefined;
    this.selectedAircraft = undefined;
    this.selectedJumpType = undefined;

    this.listOfFilteredDropZone = this.listOfDropZone;
  }

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

  public onChangeDz(event: any) {
    let filterValue: string;

    if (event.id === undefined) {
      filterValue = event.toLowerCase();

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

  public onChangeBeginDate(event: any) {
    this.endDate = event;
  }

  public notLoadingToDisplay(): boolean {
    return !(this.pendingAddRequest || this.countDatasLoaded !== 4);
  }

  public resetDz() {
    this.selectedDz = undefined;
    this.onChangeDz('');
  }
}
