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

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

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

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

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

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

  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 {
  }

  getEvaluationText(course: CourseData) {
    var text = '';
    var index = course.instructors?.findIndex( instructor => { return instructor.instructorId == this.instructorId; } );
    if(index != undefined && course.instructors != null) {
      var evaluation = course.instructors[index].evaluation;
      if(evaluation != null) {
        text = evaluation.description;
      }
    }
    return text;
  } 

  getEvaluatorName(course: CourseData) {
    var text = '';
    var index = course.instructors?.findIndex( instructor => { return instructor.instructorId == this.instructorId; } );
    if(index != undefined && course.instructors != null) {
      var evaluation = course.instructors[index].evaluation;
      if(evaluation != null) {
        var instructorIndex = this.allInstructors?.findIndex( instructor => { return instructor.ID == evaluation?.evaluatorId; } );
        if(instructorIndex != undefined && this.allInstructors != null) {
          text = this.allInstructors[instructorIndex].firstname + ' ' + this.allInstructors[instructorIndex].lastname;
        }
      }
    }
    return text;
  } 

}
