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

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

import { InstructorService } from '../../../services/instructor.service';

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

@Component({
  selector: 'app-course-instructor-evaluation-list',
  templateUrl: './course-instructor-evaluation-list.component.html',
  styleUrls: ['./course-instructor-evaluation-list.component.scss']
})
export class CourseInstructorEvaluationListComponent implements OnInit {

  @Input() courseData: CourseData | null = null;
  
  courseInstructorData: CourseInstructorData[] = [];

  allInstructors: InstructorData[] | null = null;

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

  constructor(private instructorService: InstructorService) { 
    this.instructorService.allInstructors$.subscribe((data: InstructorData[] | null) => {
      this.allInstructors = data
    })
    this.instructorService.actualizeAllInstructors();    
  }

  ngOnInit(): void {
    if(this.courseData != null) {
      if(this.courseData.instructors != null) {
        this.courseInstructorData = this.courseData.instructors;
      } else {
        this.courseInstructorData = [];
      }
    }
  }

  getEvaluatedName(courseInstructor: CourseInstructorData) {
    var text = '';
    if(courseInstructor != null) {
      var instructorIndex = this.allInstructors?.findIndex( instructor => { return instructor.ID == courseInstructor.instructorId; } );
      if(instructorIndex != undefined && this.allInstructors != null) {
        text = this.allInstructors[instructorIndex].firstname + ' ' + this.allInstructors[instructorIndex].lastname;
      }
    }
    return text;
  }

}
