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

import { CourseData } from '../../../structures/course-data';
import { SchoolData } from '../../../structures/school-data';
import { CourseThemeData } from '../../../structures/course-theme-data';
import { CourseInstructorData } from '../../../structures/course-instructor-data';
import { LoggedUserData } from '../../../structures/logged-user-data';

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

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

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

  @Input() courses: CourseData[] = [];
  @Input() instructorId: number = 0;

  detailLink: string = Globals.pages.courseDetail.path;

  displayedColumns: string[] = ['date', 'schoolName', 'role', 'link'];

  schools: SchoolData[] | null = null;
  courseThemes: CourseThemeData[] | null = null;

  constructor(private schoolService: SchoolService, private courseThemeService: CourseThemeService) {

      // nacteni skol
      this.schoolService.allSchools$.subscribe((schools: SchoolData[] | null) => {
        this.schools = schools;
      });
      this.schoolService.actualizeAllSchools();
  
      // nacteni temat kurzu
      this.courseThemeService.courseThemes$.subscribe((themes: CourseThemeData[] | null) => {
        this.courseThemes = themes;
      });
      this.courseThemeService.actualizeCourseThemes();
  }

  ngOnInit(): void {
  }

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

  getInstructorRole(instructors: CourseInstructorData[]) {
    var found = instructors.find(instructor => { return instructor.instructorId == this.instructorId; });
    if(found != undefined) {
      return Globals.instructorCourseRoles[found.roleId];
    }
    return '';
  }
}
