import { AfterContentChecked, ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatStepper } from '@angular/material';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'byte-step-personal-information',
  templateUrl: './step-personal-information.component.html',
  styleUrls: ['./step-personal-information.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class StepPersonalInformationComponent implements OnInit, AfterContentChecked {

  @Input()
  public stepper: MatStepper;

  @Input()
  public passengers: any[] = [];

  @Input()
  public ticketTypes: any[] = [];

  @Input()
  public travelClasses: any[] = [];

  public mode: string = 'view';

  constructor(private activatedRouter: ActivatedRoute, private changeDetectorRef: ChangeDetectorRef) { }

  ngAfterContentChecked(): void {
    this.changeDetectorRef.detectChanges();
  }

  getDisableConditions() {
    return this.mode !== 'new';
  }

  @Input()
  public formGroup: FormGroup = new FormGroup({
    passenger: new FormControl(),
    ticketType: new FormControl(),
    travelClass: new FormControl()
  });

  ngOnInit() {
    this.activatedRouter.params
      .subscribe(params => {
        this.mode = params['mode'];
      });
  }

  onSave() {
    if (this.formGroup.valid || this.getDisableConditions()) {
      this.stepper.next();
    }
  }

}
