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 { LoginService } from '../../../services/login.service';

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

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

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

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

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

  courseRoles = Globals.instructorCourseRoles;

  courseInstructorsSavingStatus: number = Globals.savingStatus.none;

  constructor(public dialog: MatDialog, private courseService: CourseService, private loginService: LoginService) { 
    this.courseService.saveStatusReset();
    this.courseService.courseInstructorsSaveStatus$.subscribe( val => this.courseInstructorsSavingStatus = val );
  }

  ngOnInit(): void {
  }

  openEditCourseInstructorsDialog(role: number): void {
    if(this.courseData != null) {
      const dialogRef = this.dialog.open(CourseInstructorsDialogComponent, {
        width: '480px',
        data: { instructors: this.courseData.instructors, role: role }
      });
  
      dialogRef.afterClosed().subscribe(newInstructors => {
        if(this.courseData != null && newInstructors != undefined) {
          // ulozeni nove hodnoty do kurzu v pameti
          var iterable: CourseInstructorData[] = newInstructors;
          if(this.courseData.instructors != null){
            var tmp = this.courseData.instructors?.filter(courseInstructor => { 
              var result = courseInstructor.roleId != role;
              iterable.forEach(instructor => {
                if(instructor.instructorId == courseInstructor.instructorId) {
                  result = false;
                }
              });
              return result; 
            });
            var tmp = tmp?.concat(newInstructors);
            if(tmp != undefined) {
              this.courseData.instructors = tmp;
            }
          }

          if(this.courseData.ID != null) {
            this.courseService.updateCourseInstructors(newInstructors, this.courseData.ID, role);
          }
          // this.rolePretty = Globals.instructorRolesPretty[Globals.instructorRoles.indexOf(newRole)];
        }
      });
    }
  }

  instructorsVisible(instructors: CourseInstructorData[] | null, role: number, state: string | null): boolean {
    if(instructors != null)
    {
      if(state == null) {
        var printable = instructors.filter(instructor => { return instructor.roleId == role;} );
        if(printable.length > 0) {
          return true;
        } else {
          return false;
        }
      }
      var printable = instructors.filter(instructor => { return instructor.roleId == role && instructor.state == state; } );
      if(printable.length > 0) {
        return true;
      }
    }
    return false;
  }

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

}
