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

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

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

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

@Component({
  selector: 'app-school-courses-page',
  templateUrl: './school-courses-page.component.html',
  styleUrls: ['./school-courses-page.component.scss']
})
export class SchoolCoursesPageComponent implements OnInit {
  
  ourCourses: CourseData[] | null = [];

  allConfirmedCourses: CourseData[] | null = [];
  futureConfirmedCourses: CourseData[] | null = [];
  requestedCourses: CourseData[] | null = [];

  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.

  constructor(private courseService: CourseService, private loginService: LoginService) { 
    this.courseService.courses$.subscribe((courses: CourseData[] | null) => {
      if(courses != null) {
        var userData = this.loginService.getActualUserData();
        if(userData != null) {
          if(userData.school != null) {
            this.ourCourses = CourseFilters.filterCoursesBySchool(courses, userData.school.ID);
            this.allConfirmedCourses = CourseFilters.filterActiveCourses(this.ourCourses);
            this.futureConfirmedCourses = CourseFilters.filterFutureCourses(this.allConfirmedCourses);
            this.requestedCourses = CourseFilters.filterRequestedCourses(this.ourCourses);
          } else {
            this.ourCourses = [];
          }
        } else {
          this.ourCourses = [];
        }
      } else {
        this.ourCourses = null;
      }
      this.invalidation++;
    })
    this.courseService.actualizeSchoolCourses();
  }

  ngOnInit(): void {
  }

}
