import { Component, OnInit } from '@angular/core';
import {Student} from "./student.model";
import {StudentService} from "./student.service";

@Component({
  selector: 'app-students',
  templateUrl: './students.component.html',
  styleUrls: ['./students.component.css']
})
export class StudentsComponent implements OnInit {
  studentList?: Student[];
  student?: Student;

  constructor(private studentService: StudentService) { }

  ngOnInit() { this.getStudents();   }

  newStudent(firstname: string, lastname: string, email: string, telephone: string): Student {
    return new Student(firstname, lastname, email, telephone);
  }

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

  printStudent(ID: string) : void {
    this.studentService.getStudent(parseInt(ID)).subscribe((student) => {
      console.log(student);
    })
  }

  add(firstname: string, lastname: string, email: string, telephone: string): void {
    firstname = firstname.trim();
    lastname = lastname.trim();
    email = email.trim();
    telephone = telephone.trim();
    this.studentService.addStudent({ firstname, lastname, email, telephone } as Student)
      .subscribe({
        next: (student: Student) => { this.studentList?.push(student); },
        error: () => {},
        complete: () => {
          if (this.studentList != undefined) {
            this.studentService.totalItems.next(this.studentList.length);
            console.log(this.studentList.length);
          }
        }
  });
  }

  delete(student: Student): void {
    this.studentList = this.studentList?.filter(c => c !== student);
    this.studentService.deleteStudent(student).subscribe(() => {
        // for automatic update of number of students in parent component
      if(this.studentList != undefined) {
        this.studentService.totalItems.next(this.studentList.length);
        console.log(this.studentList.length);
      }
      }
    );
  }

  deleteAll(): void {
    this.studentList = [];
    this.studentService.deleteAllStudents().subscribe(() => {
        if(this.studentList != undefined) {
          this.studentService.totalItems.next(this.studentList.length);
          console.log(this.studentList.length);
        }
      }
    );
  }

  update(student: Student, firstname: string, lastname: string, email: string, telephone: string): void {
    student.firstname = firstname;
    student.lastname = lastname;
    student.email = email;
    student.telephone = telephone;
    this.studentService.updateStudent(student).subscribe(() => {
      console.log('student updated');
    });
  }

  updateAll(students: Student[]): void {
    this.studentList = students;
    this.studentService.updateAllStudents(students).subscribe(() => {
      console.log('updated all students');
    });
  }

  patch(student: Student, firstname: string, lastname: string, email: string, telephone: string): void {
    let changed: string = '';
    if (firstname !== '') {
      student.firstname = firstname;
      changed += 'firstname, ';
    }
    if (lastname !== '') {
      student.lastname = lastname;
      changed += 'lastname, ';
    }
    if (email !== '') {
      student.email = email;
      changed += 'email, ';
    }
    if (telephone !== '') {
      student.telephone = telephone;
      changed += 'telephone, ';
    }
    this.studentService.partialUpdateStudent(student).subscribe(() => {
      console.log(`Changed following data: ${changed}`);
    });
  }

}

