import { Component, OnInit } from '@angular/core';

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

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

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

interface YearMonth {
  year: number;
  month: number;
}

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

  title: string = Globals.pages.payouts.text;

  allCourses: CourseData[] | null = null;
  thisMonthCourses: CourseData[] | null = null;
  allInstructors: InstructorData[] | null = null;

  rate1: number = Globals.defaultRates[1];
  rate2: number = Globals.defaultRates[2];
  rate3: number = Globals.defaultRates[3];

  possibleMonths: YearMonth[] = [{ year: 2021, month: 3 }, { year: 2021, month: 4 }, { year: 2021, month: 5 }];
  year: number = 2021;
  month: number = 1;
  selectedYearMonth: string = '2021-1';

  courseRoles = Globals.instructorCourseRoles;

  constructor(private courseService: CourseService, private instructorService: InstructorService) { 
    this.initializeYearMonths();

    this.courseService.courses$.subscribe((courses: CourseData[] | null) => {
      if(courses != null) {
        this.allCourses = courses;
        this.thisMonthCourses = CourseFilters.filterCoursesInMonth(courses, this.year, this.month);
      } else {
        this.thisMonthCourses = null;
      }
    })
    this.courseService.actualizeActualYearCourses();

    this.instructorService.allInstructors$.subscribe((data: InstructorData[] | null) => {
      this.allInstructors = data
    })
    this.instructorService.actualizeAllInstructors();  
  }

  ngOnInit(): void {
  }

  initializeYearMonths() {
    this.possibleMonths = [];
    var actualDate = new Date;
    var startDate = new Date(actualDate.getFullYear() - 1, actualDate.getMonth(), 1, 12, 0);
    var iterationDate = new Date();
    for (let i = 0; i <= 12; i++) {
      iterationDate = new Date(startDate.getFullYear(), startDate.getMonth() + i, 1, 0,0);    
      this.possibleMonths.push({ year: iterationDate.getFullYear(), month: iterationDate.getMonth() });
    }
    this.year = actualDate.getFullYear();
    this.month = actualDate.getMonth() - 1;
    this.selectedYearMonth = this.year + '-' + this.month;
  }

  onMonthChange() {
    var exploded = this.selectedYearMonth.split('-');
    if(exploded != undefined) {  
      this.year = Number(exploded[0]);
      this.month = Number(exploded[1]);
    }
    if(this.allCourses != null) {
      this.thisMonthCourses = CourseFilters.filterCoursesInMonth(this.allCourses, this.year, this.month);
    }
  }

  getMonthName(month: number) {
    if(Globals.monthNames[month] != undefined) {
      return Globals.monthNames[month];
    }
    return month;
  }

}
