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 DailyDietSummary {
  date: Date | null;
  classes: ClassDietSummary[];
}

interface ClassDietSummary {
  text: string;
  maleInstructorCount: number;
  femaleInstructorCount: number;
  maleTeacherCount: number;
  femaleTeacherCount: number;
  maleStudentCount: number;
  femaleStudentCount: number;
  specificDiet: string;
  arrivalTime: string | null;
  departureTime: string | null;
  snacks: boolean;
  filledQuestionnaire: boolean;
}

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

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

  summary: DailyDietSummary[] = [];

  displayedColumns: string[] = [ 'text', 'instructors', 'teachers', 'students', 'arrival' ];
  displayedFooterColumns: string[] = [ 'footer-text', 'footer-instructors', 'footer-teachers', 'footer-students' ];

  constructor() { }

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

  ngOnChanges() {
    this.computeSummary();
  }

  computeSummary() {
    this.summary = [];
    if(this.allInstructors != null && this.courses != null) {
      var actualDate = new Date;
      var endDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() - actualDate.getDay() + 13, 0, 0);
      var iterationDate = new Date();
      var newSummary = this.getEmptyDailySummary();
      var index = 0;
      while(iterationDate <= endDate && index < 25) {
        iterationDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + index, 0,0);
        newSummary.date = new Date(iterationDate);

        var courseStartDate = new Date();
        var courseEndDate = new Date();

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

        dailyCourses.forEach(course => {
          var classDietSummary: ClassDietSummary = this.getEmptyClassDietSummary();
          if(course.dateStart != null && course.dateEnd != null) {
            courseStartDate = new Date(course.dateStart);
            courseEndDate = new Date(course.dateEnd);
            // nastaveni nazvu tridy a skoly
            if(course.schoolId != null) {
              classDietSummary.text = course.class + ' (' + this.getSchoolName(course.schoolId) + ')';
            } else {
              classDietSummary.text = course.class
            }
            // nastaveni casu prijezdu pokud se jedna o prvni den
            if(Globals.areSameDay(courseStartDate, iterationDate)) {
              classDietSummary.arrivalTime = '';
              if(course.questionnaire?.arrivalTime != null && course.questionnaire?.arrivalTime != undefined) {
                classDietSummary.arrivalTime = course.questionnaire.arrivalTime;
              }
            }
            // nastaveni casu odjezdu pokud se jedna o posledni den
            if(Globals.areSameDay(courseEndDate, iterationDate)) {
              classDietSummary.departureTime = '';
              if(course.questionnaire?.departureTime != null && course.questionnaire?.departureTime != undefined) {
                classDietSummary.departureTime = course.questionnaire.departureTime;
              }
            }
            // nastaveni diet a svacin
            if(course.questionnaire != null) {
              classDietSummary.specificDiet = course.questionnaire.specificDiet;
              classDietSummary.snacks = course.questionnaire.snacks;
              classDietSummary.maleStudentCount = course.questionnaire.maleCount != null ? course.questionnaire.maleCount : 0;
              classDietSummary.femaleStudentCount = course.questionnaire.femaleCount != null ? course.questionnaire.femaleCount : 0;
              classDietSummary.maleTeacherCount = course.questionnaire.maleTeachers != null ? course.questionnaire.maleTeachers : 0;
              classDietSummary.maleTeacherCount = course.questionnaire.femaleTeachers != null ? course.questionnaire.femaleTeachers : 0;
              classDietSummary.filledQuestionnaire = true;
            } else {
              classDietSummary.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') {
                  classDietSummary.maleInstructorCount++;
                }
                if(instructorData.gender == 'zena') {
                  classDietSummary.femaleInstructorCount++;
                }
              }
            });
          }
          newSummary.classes.push(classDietSummary);
        });

        this.summary.push(newSummary);
        newSummary = this.getEmptyDailySummary();
        index++;
      }
    }
  }

  getEmptyDailySummary(): DailyDietSummary {
    return {
      date: null, 
      classes: []
    }
  }

  getEmptyClassDietSummary(): ClassDietSummary {
    return {
      text: '',
      maleInstructorCount: 0,
      femaleInstructorCount: 0,
      maleTeacherCount: 0,
      femaleTeacherCount: 0,
      maleStudentCount: 0,
      femaleStudentCount: 0,
      specificDiet: '',
      arrivalTime: null,
      departureTime: null,
      snacks: false,
      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 dayNumber = date.getDay();
    if(Globals.dayNames[dayNumber] != undefined) {
      return Globals.dayNames[dayNumber];
    }
    return '';
  }

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

  instructorSum(dailySummary: DailyDietSummary): number {
    var result = 0;
    dailySummary.classes.forEach(classSummary => {
      result += classSummary.maleInstructorCount + classSummary.femaleInstructorCount;
    });
    return result;
  }

  teacherSum(dailySummary: DailyDietSummary): number {
    var result = 0;
    dailySummary.classes.forEach(classSummary => {
      result += classSummary.maleTeacherCount + classSummary.femaleTeacherCount;
    });
    return result;
  }

  studentSum(dailySummary: DailyDietSummary): number {
    var result = 0;
    dailySummary.classes.forEach(classSummary => {
      result += classSummary.maleStudentCount + classSummary.femaleStudentCount;
    });
    return result;
  }

  getArrivalDeparture(classSummary: ClassDietSummary): string {
    var result = '';
    if(classSummary.arrivalTime != null) {
      result += 'příjezd: ' + (classSummary.arrivalTime != '' ? classSummary.arrivalTime : '?');
    }
    if(classSummary.arrivalTime != null && classSummary.departureTime != null) {
      result += ' | ';
    }
    if(classSummary.departureTime != null) {
      result += 'odjezd: ' + (classSummary.departureTime != '' ? classSummary.departureTime : '?');
    }
    return result;
  }
}
