import { CourseData } from './structures/course-data';
import * as Globals from './globals';
import { dateInputsHaveChanged } from '@angular/material/datepicker/datepicker-input-base';

// schoolyear je rok, ve kterem skolni rok zacina. Pri zadani null se bere aktualni skolni rok
export function filterSchoolyearCourses(courses: CourseData[], schoolyear: number | null): CourseData[] {
    var timespan = Globals.getSchoolyear(schoolyear);
    var result = courses.filter( course => { 
        if(course.dateStart != null) {
            var date = new Date(course.dateStart);
            return date.getTime() >= timespan.from && date.getTime() <= timespan.to;
        }
        return false;
    });
    return result;
}

export function filterCoursesByInstructor(courses: CourseData[], instructorId: number): CourseData[] {
    var result = courses.filter( course => { 
        var match = false;
        if(course.instructors != null) {
            course.instructors.forEach(instructor => {
                if(instructor.instructorId == instructorId && instructor.state == 'aktivni') {
                    match = true;
                }
            })
        }
        return match;
    });
    return result;
}

export function filterCoursesBySchool(courses: CourseData[], schoolId: number): CourseData[] {
    var result = courses.filter( course => { 
        return course.schoolId == schoolId;
    });
    return result;
}

export function filterFutureCourses(courses: CourseData[]): CourseData[] {
    var actualDate = new Date();
    var result = courses.filter( course => { 
        if(course.dateStart != null) {
            var date = new Date(course.dateStart);
            return actualDate.getTime() <= date.getTime();
        }
        return false;
    });
    return result;
}

export function filterActiveCourses(courses: CourseData[]): CourseData[] {
    var result = courses.filter( course => { 
        return course.state == 'aktivni';
    });
    return result;
}

export function filterRequestedCourses(courses: CourseData[]): CourseData[] {
    var result = courses.filter( course => { 
        return course.state == 'zadost';
    });
    return result;
}

export function filterDeniedCourses(courses: CourseData[]): CourseData[] {
    var result = courses.filter( course => { 
        return course.state == 'zamitnuty';
    });
    return result;
}

export function filterCoursesInMonth(courses: CourseData[], year: number, month: number): CourseData[] {
    var dateFrom = new Date(year, month, 1, 0, 0);
    var dateTo = new Date(year, month+1, 1, 0, 0);
    var dateEnd = new Date();
    var result = courses.filter(course => {
        if(course.dateEnd != null) {
            dateEnd = new Date(course.dateEnd);
            return dateEnd >= dateFrom && dateEnd <= dateTo;
        } 
        return false;
    });
    return result;
}
