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

import { CourseReportDialogComponent } from '../../../courses/components/course-report-dialog/course-report-dialog.component';

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

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

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

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

  constructor(public dialog: MatDialog, private courseService: CourseService, private loginService: LoginService) { }

  ngOnInit(): void {
  }

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

  openCourseReportDialog() {
    var report = '';
    if(this.courseData?.report != undefined) {
      report = this.courseData?.report;
    }
    
    const dialogRef = this.dialog.open(CourseReportDialogComponent, {
      width: '480px',
      data: { report: report }
    });

    dialogRef.afterClosed().subscribe(savedReport => {
      if(savedReport != undefined && this.courseData != null) {
        this.courseService.saveCourseReport(this.courseData.ID, savedReport);
        this.courseData.report = savedReport;
      }
    });
  }

}
