import { AfterViewChecked, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewContainerRef } from '@angular/core';
import { FormGroup, ValidationErrors, FormControl, Validators, FormBuilder } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { NotificationService, InitCommonsService, NotificationType } from '@xdf/commons';
import { IFormView, ColumnTemplate, GridTemplate, DynaDataService, AuditDialogService, ConfirmationDialogService, ValidatorUtils } from '@xdf/gallery';
import { NavigationService } from '@xdf/layouts';
import { AereopuertoService } from '../../services/aereopuerto.service';
import { MatStepper } from '@angular/material';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'byte-information',
  templateUrl: './information.component.html',
  styleUrls: ['./information.component.scss']
})

export class InformationComponent implements OnInit {

  @Input()
  stepper: MatStepper;

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

  detail: any;
  form: FormGroup;
  listAirport = [];
  viewMode: boolean;
  mode = 'new';

  airline = [{ name: "aereolinea1" }, { name: "aereolinea2" }, { name: "aereolinea3" }]
  airplane = [{ name: "avion1" }, { name: "avion2" }, { name: "avion3" }]

  constructor(
    private formBuilder: FormBuilder,
    private activatedRoute: ActivatedRoute,
    private airportService: AereopuertoService,
    private datePipe: DatePipe,
    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({
      airline: new FormControl({ value: '', disabled: this.viewMode }),
      airplane: new FormControl({ value: '', disabled: this.viewMode }),
      originAirportId: new FormControl({ value: '', disabled: this.viewMode }),
      destinationAirportId: 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() {
  }

  getForm() {
    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'));
    return this.form;
  }

  buildForm(detail: any) {
    this.detail = detail;
    if (detail) {
      this.form.patchValue({
        airline: detail.airline,
        airplane: detail.airplane,
        originAirportId: detail.originAirportId,
        destinationAirportId: detail.destinationAirportId,
        departureDate: detail.departureDate,
        arrivalDate: detail.departureDate,
        departureTime: detail.departureTime,
        arrivalTime: detail.arrivalTime
      });
    }
  }

  saveGeneralInformation(validateForm: boolean): boolean {
    this.detail.airline = this.form.controls.airline.value;
    this.detail.airplane = this.form.controls.airplane.value;
    this.detail.originAirportId = this.form.controls.originAirportId.value;
    this.detail.destinationAirportId = this.form.controls.destinationAirportId.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 (this.saveGeneralInformation(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);
  }
}
