import 'dart:convert';

import '../../webuntisApi.dart';
import 'getHolidaysResponse.dart';

class GetHolidays extends WebuntisApi {
  GetHolidays() : super("getHolidays", null);

  @override
  Future<GetHolidaysResponse> run() async {
    String rawAnswer = await query(this);
    return finalize(GetHolidaysResponse.fromJson(jsonDecode(rawAnswer)));
  }

  static GetHolidaysResponseObject? find(GetHolidaysResponse holidaysResponse, {DateTime? time}) {
    time ??= DateTime.now();
    time = DateTime(time.year, time.month, time.day, 0, 0, 0, 0, 0);

    for (var element in holidaysResponse.result) {
      DateTime start = DateTime.parse(element.startDate.toString());
      DateTime end = DateTime.parse(element.endDate.toString());

      if(!start.isAfter(time) && !end.isBefore(time)) return element;
    }
    return null;
  }

}