import { Component, OnInit } from '@angular/core';

import { CourseService } from '../../../services/course.service';

import { CourseData } from '../../../structures/course-data';

import { LoginService } from '../../../services/login.service';

import * as Globals from '../../../globals';
import * as CourseFilters from '../../../courseFilters';

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

  courses: CourseData[] | null = [];
  futureCourses: CourseData[] | null = [];

  newCoursePage = Globals.pages.newCourse;

  showPast = false;

  invalidation = 0; // promenna slouzici pro invalidaci dat ve vlozene komponente. Kurzy jsou predavany jako pole a u toho Angular nedetekuje zmeny uvnitr pole.

  courseSavingStatus: number = Globals.savingStatus.none;

  constructor(private courseService: CourseService, private loginService: LoginService) { 

    this.courseService.saveStatusReset();
    this.courseService.courseSaveStatus$.subscribe( val => this.courseSavingStatus = val );

    this.courseService.courses$.subscribe((courses: CourseData[] | null) => {
      //this.courses = courses;
      if(courses != null) {
        this.courses = CourseFilters.filterSchoolyearCourses(courses, null);// null pro aktualni skolni rok
        this.courses = CourseFilters.filterActiveCourses(this.courses);
        this.futureCourses = CourseFilters.filterFutureCourses(this.courses);
      } else {
        this.courses = null;
      }
      this.invalidation++;
    })
    this.courseService.actualizeActualYearCourses();
  }

  ngOnInit(): void {
  }

  roleAtLeast(role: string): boolean {
    return this.loginService.instructorRoleAtLeast(role);
  }

}
