import { Component, Inject, OnInit } from '@angular/core';
import { SubjectService } from "../subject.service";
import { Subject } from "../subject.model";
import { ActivatedRoute } from "@angular/router";
import { Student } from "../../students/student.model";
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from "@angular/material/dialog";
import { StudentService } from "../../students/student.service";
import { MatListOption } from "@angular/material/list";
import { MatSnackBar } from "@angular/material/snack-bar";
import { StudentAssoc } from "../studentAssoc";

@Component({
  selector: 'app-subject',
  templateUrl: './subject-details.component.html',
  styleUrls: ['./subject-details.component.css']
})
export class SubjectDetailsComponent implements OnInit {
  subject?: Subject;

  constructor(private subjectService: SubjectService,
              private studentService: StudentService,
              private route: ActivatedRoute,
              public dialog: MatDialog,
              private _snackBar: MatSnackBar) {
  }

  ngOnInit(): void {
    this.getSubject();
  }

  getSubject(): void {
    const id = Number(this.route.snapshot.paramMap.get('id'));
    this.subjectService.getSubject(id).subscribe(subject => this.subject = subject);
  }

  addStudent(): void {
    const dialogRef = this.dialog.open(StudentSelectionDialog, {
      width: '250px',
      data: {},
    });
    dialogRef.afterClosed().subscribe(result => {
      if (result) {
        const ids = result.map((student: MatListOption) => student.value);
        this.subjectService.addStudentToSubject(this.subject!, ids).subscribe();
      }
    });
  }

  deleteStudent(studentIdToDelete: number): void {
    let ids = this.subject!.studentsAssoc!.map((studentAssoc: StudentAssoc) => studentAssoc.student.id!);
    ids = ids.filter((id: number) => id !== studentIdToDelete);
    this.subjectService.addStudentToSubject(this.subject!, ids).subscribe();
  }

  addGrade(studentId: number, gradeEl: HTMLInputElement, descriptionEl: HTMLInputElement): void {
    const grade = Number(gradeEl.value);
    if (!Number.isInteger(grade) || grade < 2 || grade > 5) {
      this._snackBar.open('Grade must be integer in range 2-5', 'Dismiss');
      return;
    }
    this.studentService.addGrade(studentId, {
      mark: grade,
      description: descriptionEl.value,
      subject: this.subject!,
    }).subscribe(() => this.getSubject());
  }

  deleteGrade(studentId: number, gradeId: number): void {
    this.studentService.deleteGrade(studentId, gradeId).subscribe(() => this.getSubject());
  }

  confirmPayment(studentId: number, confirmed: boolean): void {
    this.studentService.confirmPayment(studentId, this.subject?.id!, confirmed).subscribe();
  }

}

@Component({
  selector: 'add-student-to-subject-dialog',
  templateUrl: 'add-student-to-subject-dialog.component.html',
})
export class StudentSelectionDialog implements OnInit {
  allStudents?: Student[];

  constructor(public dialogRef: MatDialogRef<StudentSelectionDialog>, @Inject(MAT_DIALOG_DATA) public subject: Subject,
              private studentService: StudentService) {
  }

  onNoClick(): void {
    this.dialogRef.close();
  }

  ngOnInit(): void {
    this.studentService.getStudents().subscribe(studentList => this.allStudents = studentList);
  }
}