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

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

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

interface WeeklyAccommondationSummary {
  dateFrom: Date | null;
  dateTo: Date | null;
  courses: CourseAccommondationSummary[];
}

interface CourseAccommondationSummary {
  text: string;
  dateStart: Date | null;
  dateEnd: Date | null;
  maleInstructorCount: number;
  femaleInstructorCount: number;
  maleTeacherCount: number;
  femaleTeacherCount: number;
  maleStudentCount: number;
  femaleStudentCount: number;
  arrivalTime: string | null;
  departureTime: string | null;
  filledQuestionnaire: boolean;
}

@Component({
  selector: 'app-accommondation-weekly-summary-list',
  templateUrl: './accommondation-weekly-summary-list.component.html',
  styleUrls: ['./accommondation-weekly-summary-list.component.scss']
})
export class AccommondationWeeklySummaryListComponent implements OnInit {

  @Input() courses: CourseData[] = [];
  @Input() allInstructors: InstructorData[] = [];
  @Input() allSchools: SchoolData[] = [];

  summary: WeeklyAccommondationSummary[] = [];

  constructor() { }

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

  ngOnChanges() {
    this.computeSummary();
  }

  computeSummary() {
    this.summary = [];
    if(this.allInstructors != null && this.courses != null) {
      var actualDate = new Date;
      var startDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() - actualDate.getDay() + 1, 0, 0);
      var weekStartDate = new Date();
      var weekEndDate = new Date();
      var newSummary = this.getEmptyWeeklySummary();
      var index = 0;
      for (let i = 0; i < 3; i++) {
        weekStartDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + (i * 7), 0,0);
        weekEndDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + ((i+1) * 7) - 1, 23,59);
        newSummary.dateFrom = new Date(weekStartDate);
        newSummary.dateTo = new Date(weekEndDate);

        var courseStartDate = new Date();
        var courseEndDate = new Date();
        var weeklyCourses = this.courses.filter(course => {
          if(course.dateStart != null && course.dateEnd != null) {
            courseStartDate = new Date(course.dateStart);
            courseEndDate = new Date(course.dateEnd);
            return courseStartDate <= weekEndDate && courseEndDate >= weekStartDate;
          }
          return false;
        });

        var newCourseSummary: CourseAccommondationSummary = this.getEmptyCourseSummary();
        weeklyCourses.forEach(course => {

          newCourseSummary.dateStart = course.dateStart;
          newCourseSummary.dateEnd = course.dateEnd;

          // nastaveni nazvu tridy a skoly
          if(course.schoolId != null) {
            newCourseSummary.text = course.class + ' (' + this.getSchoolName(course.schoolId) + ')';
          } else {
            newCourseSummary.text = course.class
          }

          // nastaveni dat z dotazniku
          if(course.questionnaire != null) {
            newCourseSummary.maleStudentCount = course.questionnaire.maleCount != null ? course.questionnaire.maleCount : 0;
            newCourseSummary.femaleStudentCount = course.questionnaire.femaleCount != null ? course.questionnaire.femaleCount : 0;
            newCourseSummary.maleTeacherCount = course.questionnaire.maleTeachers != null ? course.questionnaire.maleTeachers : 0;
            newCourseSummary.maleTeacherCount = course.questionnaire.femaleTeachers != null ? course.questionnaire.femaleTeachers : 0;
            newCourseSummary.arrivalTime = course.questionnaire.arrivalTime;
            newCourseSummary.departureTime = course.questionnaire.departureTime;
            newCourseSummary.filledQuestionnaire == true;
          } else {
            newCourseSummary.filledQuestionnaire == false;
          }

          // pocet instruktoru
          course.instructors?.forEach(instructor => {
            var instructorData = this.allInstructors.find(instructorData => { return instructorData.ID == instructor.instructorId; });
            if(instructorData != undefined) {
              if(instructorData.gender == 'muz') {
                newCourseSummary.maleInstructorCount++;
              }
              if(instructorData.gender == 'zena') {
                newCourseSummary.femaleInstructorCount++;
              }
            }
          });

          newSummary.courses.push(newCourseSummary);
          newCourseSummary = this.getEmptyCourseSummary();
        });

        this.summary.push(newSummary);
        newSummary = this.getEmptyWeeklySummary();
      }

    }
  }

  getEmptyWeeklySummary(): WeeklyAccommondationSummary {
    return {
      dateFrom: null,
      dateTo: null, 
      courses: []
    }
  }

  getEmptyCourseSummary(): CourseAccommondationSummary {
    return {
      text: '',
      dateStart: null,
      dateEnd: null,
      maleInstructorCount: 0,
      femaleInstructorCount: 0,
      maleTeacherCount: 0,
      femaleTeacherCount: 0,
      maleStudentCount: 0,
      femaleStudentCount: 0,
      arrivalTime: null,
      departureTime: null,
      filledQuestionnaire: false
    }
  }

  getSchoolName(id: number): string {
    var found = this.allSchools.find(school => { return school.ID == id; });
    if(found != undefined) {
      return found.name;
    }
    return '';
  }

  getDayName(date: Date) {
    var dateObject = new Date(date);
    var dayNumber = dateObject.getDay();
    if(Globals.dayNames[dayNumber] != undefined) {
      return Globals.dayNames[dayNumber];
    }
    return '';
  }

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

}
