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

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

import { emptyEvaluationDataFactory } from '../../../empty-objects/empty-evaluation-data';

import { EditInstructorEvaluationDialogComponent } from '../../../courses/components/edit-instructor-evaluation-dialog/edit-instructor-evaluation-dialog.component';

import { CourseInstructorsDialogComponent } from '../../../courses/components/course-instructors-dialog/course-instructors-dialog.component';

import { LoginService } from '../../../services/login.service';
import { CourseService } from '../../../services/course.service';

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

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

  @Input() courseData: CourseData | null = null;

  showList: boolean = false;

  userData: LoggedUserData | null = null;

  constructor(private courseService: CourseService, private loginService: LoginService, public dialog: MatDialog) { 
    // nacteni udaju o uzivateli
    this.loginService.userData$.subscribe((data: LoggedUserData | null) => {
      this.userData = data;
    })
  }

  ngOnInit(): void {
  }

  instructorEvaluationFilled(): number {
    var result = 0;
    this.courseData?.instructors?.forEach(instructor => {
      if(instructor.evaluation != null) {
        result++;
      }
    })
    return result;
  }

  roleAtLeast(role: string): boolean {
    return this.loginService.instructorRoleAtLeast(role);
  }

  openAddInstructorEvaluationDialog(id: number) {
    var evaluation = emptyEvaluationDataFactory();
    evaluation.evaluatedId = id;
    if(this.courseData != null) {
      evaluation.courseId = this.courseData.ID;
    }
    if(this.userData?.instructor?.ID != undefined) {
      evaluation.evaluatorId = this.userData?.instructor?.ID;
    }

    var name = '';
    var found = this.courseData?.instructors?.find(instructor => { return instructor.instructorId == id; });
    if(found != undefined && found.instructorName) {
      name = found.instructorName;
    }
    
    const dialogRef = this.dialog.open(EditInstructorEvaluationDialogComponent, {
      width: '480px',
      data: { evaluation: evaluation, name: name }
    });

    dialogRef.afterClosed().subscribe(savedEvaluation => {
      if(savedEvaluation != undefined) {
        this.courseService.saveInstructorEvaluation(savedEvaluation);
        // ulozeni pro aktualni zobrazeni
        var found = this.courseData?.instructors?.find(instructor => {return instructor.instructorId == id; });
        if(found != undefined){
          found.evaluation = savedEvaluation;
        }
      }
    });
  }
}
