import { Component, OnInit, Input, OnDestroy, ChangeDetectorRef, Inject } from '@angular/core';
import { ScheduleFilterValidationService } from '@app/features/schedule/components/schedule-filter/services/schedule-filter-validation.service';
import { UserLocationService } from '@app/shared/services/user-location/user-location.service';
import { FlightsMapFiltersStateService, IFlightsMapFilterState } from '@shared/services/filters/flights-map-filters-state.service';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { AppSettings } from '@shared/models-legacy';
import { APP_SETTINGS } from '@shared/services';
import { OnlineBoardApiService } from "@app/features/online-board/services/api.service";
import { FlightsMapApiService } from '../../services/flights-map-api.service';

@Component({
  selector: 'flights-map-filter',
  templateUrl: './flights-map-filter.component.html',
  styleUrls: ['./flights-map-filter.component.scss'],
  providers: [ScheduleFilterValidationService],
})
export class FlightsMapFilterComponent implements OnInit, OnDestroy  {

  private currentFilterState: IFlightsMapFilterState;

  withReturn: boolean;

  private destroy$ = new Subject<void>();
  
  constructor(
    public validationService: ScheduleFilterValidationService,
    private apiService: FlightsMapApiService,
    @Inject(APP_SETTINGS) public settings: AppSettings,
    private filterStateService: FlightsMapFiltersStateService,
    private locationService: UserLocationService,
    private cdr  : ChangeDetectorRef
  ) { }


  ngOnInit() {

    this.filterStateService.state$
      .pipe(takeUntil(this.destroy$))
      .subscribe(state => {
        this.currentFilterState = state;

        this.cdr.markForCheck();
      });

      this.locationService.location.subscribe((location) => {
        // set default state only if user permitted geo position
        // search and filter isn't filled
        if (location && !this.departure && !this.arrival) {
            this.filterStateService.setDeparture(location);
        }
      });
  }

  ngOnDestroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }


  get departure() {
    return this.currentFilterState.departure;
  }

  set departure(departure: string) 
  {
    this.filterStateService.setDeparture(departure);
  }

  get arrival() 
  {
    return this.currentFilterState.arrival;
  }

  set arrival(arrival: string){
    this.filterStateService.setArrival(arrival);
  }

  get connections() 
  {
    return this.currentFilterState.connections;
  }

  set connections(showConenctions: boolean)
  {
    this.filterStateService.setConnections(showConenctions);
  }

  get domestic() 
  {
    return this.currentFilterState.domestic;
  }

  set domestic(showDomestic: boolean)
  {
    this.filterStateService.setDomestic(showDomestic);
  }

  get international() 
  {
    return this.currentFilterState.international;
  }

  set international(showInternational: boolean)
  {
    this.filterStateService.setInternational(showInternational);
  }

  get date()
  {
    return this.currentFilterState.date;
  }

  set date(date: Date)
  {
    this.filterStateService.setDate(date);
  }

  get departurePlaceholder() {
    return 'FLIGHTS-MAP.FILTER_DEPARTURE_PLACEHOLDER'
  }
  get arrivalPlaceholder() {
    return 'FLIGHTS-MAP.FILTER_ARRIVAL_PLACEHOLDER'
  }

  get minDate(){
    return this.currentFilterState.minDate;
  }

  get maxDate(){
    return this.currentFilterState.maxDate;
  }

  get disabledDates()
  {
    return this.currentFilterState.disabledDates;
  }

  set disabledDates(disabledDates: Date[]){
    this.filterStateService.setDisabledDates(disabledDates);
  }

  exchange() {
    [
        this.validationService.departureError,
        this.validationService.arrivalError,
    ] = [null, null];
    [this.departure, this.arrival] = [this.arrival, this.departure];
  }

  resetReturnDateRange() {
    throw new Error('Method not implemented.');
  }
}
