import { Component, OnInit, Input } from '@angular/core';
import { count, findIndex } from 'rxjs/operators';

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

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

interface CourseCounts {
  instructorId: number;
  count1: number;
  count2: number;
  count3: number;
  bonus: number;
}

@Component({
  selector: 'app-instructor-payouts-table',
  templateUrl: './instructor-payouts-table.component.html',
  styleUrls: ['./instructor-payouts-table.component.scss']
})
export class InstructorPayoutsTableComponent implements OnInit {

  @Input() thisMonthCourses: CourseData[] = [];
  @Input() allInstructors: InstructorData[] = [];

  @Input() rate1: number = Globals.defaultRates[1];
  @Input() rate2: number = Globals.defaultRates[2];
  @Input() rate3: number = Globals.defaultRates[3];

  @Input() yearMonth: string = '';

  courseCounts: CourseCounts[] = [];
  computedForYearMonth: string = '';

  courseRoleNames = Globals.instructorCourseRoles;

  profileLink: string = Globals.pages.instructorProfile.path;

  displayedColumns: string[] = ['name', 'role1', 'role2', 'role3', 'bonus', 'sum'];

  constructor() { }

  ngOnInit(): void {
    this.computedForYearMonth = this.yearMonth;
    this.computeCounts();
  }

  ngOnChanges() {
    if(this.yearMonth != this.computedForYearMonth) {
      this.courseCounts = [];
      this.computedForYearMonth = this.yearMonth;
    }
    this.computeCounts();
  }

  computeCounts() {
    this.clearCounts(this.courseCounts);
    this.thisMonthCourses.forEach(course => {
      course.instructors?.forEach(instructor => {
        if(instructor.state == 'aktivni') {
          var index = this.courseCounts.findIndex(count => { return count.instructorId == instructor.instructorId; });
          if(index == undefined || index == -1) {
            this.courseCounts.push({ instructorId: instructor.instructorId, count1: 0, count2: 0, count3: 0, bonus: 0 });
          }
          index = this.courseCounts.findIndex(count => { return count.instructorId == instructor.instructorId; });
          if(index != undefined && index != -1) {
            switch (instructor.roleId) {
              case 1: this.courseCounts[index].count1++; break;
              case 2: this.courseCounts[index].count2++; break;
              case 3: this.courseCounts[index].count3++; break;       
              default: break;
            }
          }
        }
      });
    });
  }

  clearCounts(courseCounts: CourseCounts[]) {
    courseCounts.forEach(counts => {
      counts.count1 = 0;
      counts.count2 = 0;
      counts.count3 = 0;
    });
  }

  getNameById(id: number | null): string {
    if(this.allInstructors != null && id != null) {
      var found = this.allInstructors.find(instructor => { return instructor.ID == id; });
      if(found != undefined) {
        return found.firstname + ' ' + found.lastname;
      }
    }
    return '';
  } 

  wordShape(count: number, word: string): string {
    return Globals.wordShape(count, word);
  }

  isValidNumber(value: number): boolean {
    if(isNaN(value) == false) {
      return true;
    }
    return false;
  }

  allInputsValid(element: CourseCounts): boolean {
    var bonus = this.numberValue(element.bonus);
    if(this.isValidNumber(bonus) == true && this.isValidNumber(this.rate1) == true && this.isValidNumber(this.rate2) == true && this.isValidNumber(this.rate3) == true) {
      return true;
    }
    return false;
  }

  numberValue(value: any): number {
    return Number(value);
  }

}
