import 'package:em2rp/models/event_model.dart';

class CalendarUtils {
  static String getDayName(int weekday) {
    switch (weekday) {
      case DateTime.monday:
        return 'Lundi';
      case DateTime.tuesday:
        return 'Mardi';
      case DateTime.wednesday:
        return 'Mercredi';
      case DateTime.thursday:
        return 'Jeudi';
      case DateTime.friday:
        return 'Vendredi';
      case DateTime.saturday:
        return 'Samedi';
      case DateTime.sunday:
        return 'Dimanche';
      default:
        return '';
    }
  }

  static String getShortDayName(int weekday) {
    switch (weekday) {
      case DateTime.monday:
        return 'Lun';
      case DateTime.tuesday:
        return 'Mar';
      case DateTime.wednesday:
        return 'Mer';
      case DateTime.thursday:
        return 'Jeu';
      case DateTime.friday:
        return 'Ven';
      case DateTime.saturday:
        return 'Sam';
      case DateTime.sunday:
        return 'Dim';
      default:
        return '';
    }
  }

  static String getMonthName(int month) {
    switch (month) {
      case 1:
        return 'Janvier';
      case 2:
        return 'Février';
      case 3:
        return 'Mars';
      case 4:
        return 'Avril';
      case 5:
        return 'Mai';
      case 6:
        return 'Juin';
      case 7:
        return 'Juillet';
      case 8:
        return 'Août';
      case 9:
        return 'Septembre';
      case 10:
        return 'Octobre';
      case 11:
        return 'Novembre';
      case 12:
        return 'Décembre';
      default:
        return '';
    }
  }

  static String getMonthYearString(DateTime weekStart, DateTime weekEnd) {
    if (weekStart.month == weekEnd.month) {
      return '${getMonthName(weekStart.month)} ${weekStart.year}';
    } else {
      return '${getMonthName(weekStart.month)} - ${getMonthName(weekEnd.month)} ${weekEnd.year}';
    }
  }

  static bool isMultiDayEvent(EventModel event) {
    return event.startDateTime.day != event.endDateTime.day ||
        event.startDateTime.month != event.endDateTime.month ||
        event.startDateTime.year != event.endDateTime.year;
  }

  static int calculateTotalDays(EventModel event) {
    final startDate = DateTime(event.startDateTime.year,
        event.startDateTime.month, event.startDateTime.day);
    final endDate = DateTime(
        event.endDateTime.year, event.endDateTime.month, event.endDateTime.day);
    return endDate.difference(startDate).inDays + 1;
  }

  static int calculateDayNumber(DateTime startDate, DateTime currentDay) {
    final start = DateTime(startDate.year, startDate.month, startDate.day);
    final current = DateTime(currentDay.year, currentDay.month, currentDay.day);
    return current.difference(start).inDays + 1;
  }

  static List<EventModel> getEventsForDay(
      DateTime day, List<EventModel> events) {
    final nextDay = day.add(const Duration(days: 1));
    final dayStart = DateTime(day.year, day.month, day.day, 2, 0);
    final dayEnd = DateTime(nextDay.year, nextDay.month, nextDay.day, 2, 59, 59);

    return events.where((event) {
      return !(event.endDateTime.isBefore(dayStart) ||
          event.startDateTime.isAfter(dayEnd));
    }).toList();
  }
}
