import { Component, OnInit } from '@angular/core';
import {EventsAvailableResponse, EventService} from "../../services/event.service";
import { Observable} from "rxjs";
import {MonthCalendarEvent} from "../../components/month-calendar/month-calendar.component";
import {NavigationService} from "../../services/navigation.service";
import {TimeTableFilter} from "../../app.types";
import {ActivatedRoute} from "@angular/router";
import {Store} from "@ngxs/store";
import {FilterTimeTableAction} from "../../state/app.actions";

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

  availableEvents: Observable<EventsAvailableResponse>;
  timeTableFilter: TimeTableFilter;

  constructor(private eventService: EventService,
              private navigationService: NavigationService,
              private route: ActivatedRoute,
              private store: Store ) {
  }

  ngOnInit() {
    this.availableEvents = this.eventService.findEventsAvailable();
    this.route.params.subscribe(params => {
      this.timeTableFilter = {
        idTrainer: params.idTrainer || -1,
        idEventType: params.idEventType || -1
      }
      this.store.dispatch(new FilterTimeTableAction(this.timeTableFilter.idTrainer,this.timeTableFilter.idEventType))
    });
  }

  handleEvent($event: MonthCalendarEvent) {
    this.navigationService.navigateToEventDetails($event.event.id);
  }

  filterChanged($event: TimeTableFilter) {
    this.navigationService.navigateToHome($event.idTrainer,$event.idEventType);
  }

}
