import { Component, OnInit, Input, ViewChild } from '@angular/core';
import {MatTable} from '@angular/material/table'; 
import { Observable } from 'rxjs';

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

import { CourseThemeService } from '../../../services/course-theme.service';

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

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

  @Input() courses: CourseData[] = [];
  @Input() invalidation: number = 0; // promenna slouzici pro invalidaci dat ve vlozene komponente. Kurzy jsou predavany jako pole a u toho Angular nedetekuje zmeny uvnitr.
  @Input() showQuestionnaire: boolean = true;

  @ViewChild(MatTable) table!: MatTable<CourseData>;

  courseThemes: CourseThemeData[] | null = null;

  questionnaireLink: string = Globals.pages.schoolQuestionnaire.path;

  displayedColumns: string[] = ['date', 'class', 'themeName', 'information'];

  constructor(private courseThemeService: CourseThemeService) { 
    // nacteni temat kurzu
    this.courseThemeService.courseThemes$.subscribe((themes: CourseThemeData[] | null) => {
      this.courseThemes = themes;
    });
    this.courseThemeService.actualizeCourseThemes();
  }

  ngOnInit(): void {
    if(this.showQuestionnaire == true) {
      this.displayedColumns = ['date', 'class', 'themeName', 'information', 'questionnaire'];
    }
  }

  ngOnChanges() {
    if(this.table != undefined) {
     this.table.renderRows();
    }
  } 

  getThemeNameById(id: number | null): string {
    if(this.courseThemes != null && id != null) {
      var found = this.courseThemes.find(theme => { return theme.ID == id; });
      if(found != undefined) {
        return found.name;
      }
    }
    return '';
  } 

}
