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

// To parse this JSON data, do
//
//     final session = sessionFromJson(jsonString);

import 'dart:convert';

Session sessionFromJson(String str) => Session.fromJson(json.decode(str));

String sessionToJson(Session data) => json.encode(data.toJson());

class Session {
  final String? session;
  final String? semester;
  final DateTime? startdate;
  final DateTime? enddate;
  final String? status;

  Session({
    this.session,
    this.semester,
    this.startdate,
    this.enddate,
    this.status,
  });

  factory Session.fromJson(Map<String, dynamic> json) => Session(
        session: json["session"],
        semester: json["semester"],
        startdate: json["startdate"] == null
            ? null
            : DateTime.parse(json["startdate"]),
        enddate:
            json["enddate"] == null ? null : DateTime.parse(json["enddate"]),
        status: json["status"],
      );

  Map<String, dynamic> toJson() => {
        "session": session,
        "semester": semester,
        "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')}",
        "status": status,
      };
}

Future getCurrentSession() async {
  try {
    final response = await client
        .get(
          Uri.parse("$baseUrl/session/getCurrentSession"),
          headers: header,
        )
        .timeout(
          const Duration(seconds: timeout),
        );

    if (response.statusCode == 200) {
      session = sessionFromJson(response.body).session!;
      semester = sessionFromJson(response.body).semester!;
      //return sessionFromJson(response.body);
    } else {
     return sessionFromJson("");
    }
  } on SocketException {
    throw Exception();
  } on HttpException {
    throw Exception();
  } on FormatException {
    throw Exception();
  }
}

// Future<List<String>> getSession() async {
//   try {
//     final response = await client.get(
//       Uri.parse("$baseUrl/session/getSessionsList"),
//       headers: header,
//     );

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