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

import { CourseData } from '../../../structures/course-data';
import { emptyCourseDataFactory } from '../../../empty-objects/empty-course-data';
import { SchoolData } from '../../../structures/school-data';
import { CourseThemeData } from '../../../structures/course-theme-data';

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

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

  @Input() courseData: CourseData = emptyCourseDataFactory();
  
  courseThemes: CourseThemeData[] | null = null;
  schools: SchoolData[] | 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 '';
  } 

  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 '';
  } 

}
