import 'dart:io';
import 'package:school_management_system/components/default_values.dart';

// To parse this JSON data, do
//
//     final calendar = calendarFromJson(jsonString);

import 'dart:convert';

Calendar calendarFromJson(String str) => Calendar.fromJson(json.decode(str));

String calendarToJson(Calendar data) => json.encode(data.toJson());

class Calendar {
  final String? calenderid;
  final String? session;
  final String? semester;
  final List<Calenderrdetail>? calenderrdetails;

  Calendar({
    this.calenderid,
    this.session,
    this.semester,
    this.calenderrdetails,
  });

  factory Calendar.fromJson(Map<String, dynamic> json) => Calendar(
        calenderid: json["calenderid"],
        session: json["session"],
        semester: json["semester"],
        calenderrdetails: json["calenderrdetails"] == null
            ? []
            : List<Calenderrdetail>.from(json["calenderrdetails"]!
                .map((x) => Calenderrdetail.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "calenderid": calenderid,
        "session": session,
        "semester": semester,
        "calenderrdetails": calenderrdetails == null
            ? []
            : List<dynamic>.from(calenderrdetails!.map((x) => x.toJson())),
      };
}

class Calenderrdetail {
  final String? note;
  final String? event;
  final bool? allday;
  final DateTime? startdate;
  final DateTime? enddate;
  final String? endtime;
  final String? starttime;
  final String? frequency;
  final String? interval;
  final String? dayofweek;

  Calenderrdetail({
    this.note,
    this.event,
    this.allday,
    this.startdate,
    this.enddate,
    this.endtime,
    this.starttime,
    this.frequency,
    this.interval,
    this.dayofweek,
  });

  factory Calenderrdetail.fromJson(Map<String, dynamic> json) =>
      Calenderrdetail(
        note: json["note"],
        event: json["event"],
        allday: json["allday"],
        startdate: json["startdate"] == null
            ? null
            : DateTime.parse(json["startdate"]),
        enddate:
            json["enddate"] == null ? null : DateTime.parse(json["enddate"]),
        endtime: json["endtime"],
        starttime: json["starttime"],
        frequency: json["frequency"],
        interval: json["interval"],
        dayofweek: json["dayofweek"],
      );

  Map<String, dynamic> toJson() => {
        "note": note,
        "event": event,
        "allday": allday,
        "startdate":
            "${startdate!.year.toString().padLeft(4, '0')}-${startdate!.month.toString().padLeft(2, '0')}-${startdate!.day.toString().padLeft(2, '0')}",
        "enddate":
            "${enddate!.year.toString().padLeft(4, '0')}-${enddate!.month.toString().padLeft(2, '0')}-${enddate!.day.toString().padLeft(2, '0')}",
        "endtime": endtime,
        "starttime": starttime,
        "frequency": frequency,
        "interval": interval,
        "dayofweek": dayofweek,
      };
}

Future<Calendar> getStudentCalender(
    String calenderid, String session, String semester) async {
  try {
    final response = await client
        .get(
          Uri.parse(
              "$baseUrl/calender/getCalender/$calenderid?session=$session&semester=$semester"),
          headers: header,
        )
        .timeout(
          const Duration(seconds: timeout),
        );

    if (response.statusCode == 200) {
      return calendarFromJson(response.body);
    } else {
      return calendarFromJson("");
    }
  } on SocketException {
    return throw Exception();
  } on HttpException {
    return throw Exception();
  } on FormatException {
    return throw Exception();
  }
}

Future<Calendar> getSchoolCalender(
    String calenderid, String session, String semester) async {
  try {
    final response = await client
        .get(
          Uri.parse(
              "$baseUrl/calender/getCalender/$calenderid?session=$session&semester=$semester"),
          headers: header,
        )
        .timeout(
          const Duration(seconds: timeout),
        );

    if (response.statusCode == 200) {
      return calendarFromJson(response.body);
    } else {
      return calendarFromJson(response.body);
    }
  } on SocketException {
    return throw Exception();
  } on HttpException {
    return throw Exception();
  } on FormatException {
    return throw Exception();
  }
}
