import 'dart:convert';

import '../../../requestCache.dart';
import '../authenticate/authenticate.dart';
import 'getTimetable.dart';
import 'getTimetableParams.dart';
import 'getTimetableResponse.dart';

class GetTimetableCache extends RequestCache<GetTimetableResponse> {
  int startdate;
  int enddate;

  GetTimetableCache({required onUpdate, onError, required this.startdate, required this.enddate}) : super(RequestCache.cacheMinute, onUpdate, onError: onError) {
    start("MarianumMobile", "wu-timetable-$startdate-$enddate");
  }

  @override
  GetTimetableResponse onLocalData(String json) {
    return GetTimetableResponse.fromJson(jsonDecode(json));
  }

  @override
  Future<GetTimetableResponse> onLoad() async {
    return GetTimetable(
        GetTimetableParams(
            options: GetTimetableParamsOptions(
                element: GetTimetableParamsOptionsElement(
                    id: (await Authenticate.getSession()).personId,
                type: (await Authenticate.getSession()).personType,
                keyType: GetTimetableParamsOptionsElementKeyType.id,
            ),
            startDate: startdate,
            endDate: enddate,
            teacherFields: GetTimetableParamsOptionsFields.all,
            subjectFields: GetTimetableParamsOptionsFields.all,
            roomFields: GetTimetableParamsOptionsFields.all,
            klasseFields: GetTimetableParamsOptionsFields.all,
        )
    )
    ).run();
  }
}