import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { MatStepper } from '@angular/material';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { NotificationService } from '@xdf/commons';
import { ValidatorUtils } from '@xdf/gallery';
import { AereopuertoService } from '../../services/aereopuerto.service';
import { DatePipe } from '@angular/common';
import { Escala } from '../../interfaces/leg';
import { EscalaService } from '../../services/escala.service';

@Component({
  selector: 'byte-escala',
  templateUrl: './escala.component.html',
  styleUrls: ['./escala.component.scss']
})
export class EscalaComponent implements OnInit {

  @Input()
  stepper: MatStepper;

  @Output()
  nextPage: EventEmitter<boolean> = new EventEmitter<boolean>();

  flightId: number;
  flag: boolean;
  detail: any;
  form: FormGroup;
  dataSource: Escala[] = [];
  listAirport = [];
  viewMode: boolean;
  mode = 'new';



  constructor(
    private formBuilder: FormBuilder,
    private activatedRoute: ActivatedRoute,
    private datePipe: DatePipe,
    private airportService: AereopuertoService,
    private legService: EscalaService,
    private notificationService: NotificationService,
    private translateService: TranslateService,
    private validatorUtils: ValidatorUtils
  ) {

    this.mode = this.activatedRoute.snapshot.data.mode;
    this.viewMode = this.mode === 'view';
    this.form = this.formBuilder.group({
      id: new FormControl({ value: 0, disabled: this.viewMode }),
      originId: new FormControl({ value: '', disabled: this.viewMode }),
      destinationId: new FormControl({ value: '', disabled: this.viewMode }),
      departureDate: new FormControl({ value: '', disabled: this.viewMode }),
      arrivalDate: new FormControl({ value: '', disabled: this.viewMode }),
      departureTime: new FormControl({ value: '', disabled: this.viewMode }),
      arrivalTime: new FormControl({ value: '', disabled: this.viewMode }),
    });
    this.airportService.getAllAirports().subscribe(data => {
      this.listAirport = data;
    });
  }

  ngOnInit() {
    if (this.mode != 'create') {
      this.flag = true;
    }
  }

  getForm() {
    return this.dataSource;
  }

  buildForm(detail: any) {
    this.detail = detail;
    if (detail) {
      this.form.patchValue({
        originId: detail.originId,
        destinationId: detail.destinationId,
        departureDate: detail.departureDate,
        arrivalDate: detail.departureDate,
        departureTime: detail.departureTime,
        arrivalTime: detail.arrivalTime
      });
    }
  }

  setId(id: number) {
    this.flightId = id;
    if (this.mode != 'create') {
      this.legService.getAllFlight(this.flightId).subscribe(data => {
        let temp = data;
        temp.forEach(info => {
          let leg = new Escala();
          leg.id = info.id;
          leg.originId = info.origin.id;
          leg.destinationId = info.destination.id;
          leg.departureDate = info.departureDate;
          leg.arrivalDate = info.arrivalDate;
          leg.departureTime = info.departureTime;
          leg.arrivalTime = info.arrivalTime;
          this.dataSource.push(leg);
        });
      });
    }
  }

  addTable() {
    this.form.controls.arrivalDate.setValue(this.datePipe.transform(this.form.controls.arrivalDate.value, 'yyyy-MM-dd'));
    this.form.controls.departureDate.setValue(this.datePipe.transform(this.form.controls.departureDate.value, 'yyyy-MM-dd'));
    let leg = new Escala();
    leg.id = this.form.controls.id.value;
    leg.originId = this.form.controls.originId.value;
    leg.destinationId = this.form.controls.destinationId.value;
    leg.departureDate = this.form.controls.departureDate.value;
    leg.arrivalDate = this.form.controls.arrivalDate.value;
    leg.departureTime = this.form.controls.departureTime.value;
    leg.arrivalTime = this.form.controls.arrivalTime.value;
    if (this.mode == 'create') {
      this.dataSource.push(leg);
    } else {
      if (this.dataSource.length != 0) {
        this.dataSource.filter(item => {
          if (item.id == leg.id) {
            item.originId = leg.originId;
            item.destinationId = leg.destinationId;
            item.departureDate = leg.departureDate;
            item.arrivalDate = leg.arrivalDate;
            item.departureTime = leg.departureTime;
            item.arrivalTime = leg.arrivalTime;
          }
        });
      }
    }
  }

  editItem(body: any) {
    this.form.patchValue({
      id: body.id,
      originId: body.originId,
      destinationId: body.destinationId,
      departureDate: body.departureDate,
      arrivalDate: body.departureDate,
      departureTime: body.departureTime,
      arrivalTime: body.arrivalTime
    });
  }

  deleteItem(body: any) {
    if (body.id == 0) {
      this.dataSource = this.dataSource.filter(item => item !== body);
    } else {
      this.dataSource = this.dataSource.filter(item => item.id !== body.id);
    }
  }

  /*saveGeneralInformation(validateForm: boolean): boolean {
    this.detail.originId = this.form.controls.originId.value;
    this.detail.destinationId = this.form.controls.destinationId.value;
    this.detail.departureDate = this.form.controls.departureDate.value;
    this.detail.arrivalDate = this.form.controls.arrivalDate.value;
    this.detail.departureTime = this.form.controls.departureTime.value;
    this.detail.arrivalTime = this.form.controls.arrivalTime.value;
    return true;
  }*/

  reset() {
    this.form.reset();
  }

  isDirty() {
    return this.form.dirty;
  }

  next() {
    if (this.viewMode) {
      this.stepper.next();
      return;
    }

    if (true) {
      this.nextPage.emit(this.isDirty());
      this.stepper.next();
    }
  }

  getErrors(name: string, placeholder: string) {
    return this.validatorUtils.getErrors(this.form.controls[name] as FormControl, name, placeholder);
  }
}
