import 'package:flutter/foundation.dart';
import 'package:intl/intl.dart';
import 'package:shamsi_date/shamsi_date.dart';

class DateTimeUtils {
  static DateTime getNow() {
    try {
      DateTime nowUtc = DateTime.now().toUtc();
      return nowUtc.add(const Duration(hours: 3, minutes: 30));
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in getNow: $e');
      }
      return DateTime.now();
    }
  }

  static Jalali getNowJalali() {
    return Jalali.now();
  }

  static String getTimeFromDuration(int seconds) {
    try {
      final time = Duration(seconds: seconds);
      final min = time.inMinutes.toString().padLeft(2, '0');
      final sec = (time.inSeconds % 60).toString().padLeft(2, '0');
      return '$min:$sec';
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in getTimeFromDuration: $e');
      }
      return '00:00'; // Return a default value
    }
  }

  static String getFormattedTimeFromSeconds(int seconds) {
    try {
      if (seconds >= 3600) {
        final hours = (seconds ~/ 3600).toString();
        final minutes = ((seconds % 3600) ~/ 60).toString().padLeft(2, '0');
        return '$hours:$minutes ساعت';
      } else if (seconds >= 60) {
        final minutes = (seconds ~/ 60).toString();
        return '$minutes دقیقه';
      } else {
        return '$seconds ثانیه';
      }
    } catch (e) {
      if (kDebugMode) {
        print('Error in getFormattedTimeFromSeconds: $e');
      }
      return '0 ثانیه'; // Default fallback
    }
  }

  static DateTime convertStringIsoToDate(String isoDate) {
    try {
      DateTime dateTime = DateTime.parse(isoDate);
      DateTime iranTime = dateTime.add(const Duration(hours: 3, minutes: 30));
      return iranTime;
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in convertStringIsoToDate: $e');
      }
      return DateTime.now(); // Return current time as a fallback
    }
  }

  static String convertDateToStringInFormatted(DateTime date,
      {final String? formatted}) {
    try {
      String formattedDate =
          DateFormat(formatted ?? 'yyyy-MM-dd – kk:mm').format(date);
      return formattedDate;
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in convertDateToStringInFormatted: $e');
      }
      return ''; // Return a default value
    }
  }

  static String convertStringIsoToStringInFormatted(
    String isoDate,
  ) {
    try {
      DateTime dateTime = convertStringIsoToDate(isoDate).toUtc()
        ..add(const Duration(hours: 3, minutes: 30));
      final persianDate = Jalali.fromDateTime(dateTime);
      return '${persianDate.year.toString().padLeft(2, '0')}-${persianDate.month.toString().padLeft(2, '0')}-${persianDate.day.toString().padLeft(2, '0')} - ${persianDate.hour.toString().padLeft(2, '0')}:${persianDate.minute.toString().padLeft(2, '0')} ';
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in convertStringIsoToStringInFormatted: $e');
      }
      return ''; // Return a default value
    }
  }

  static String convertToSentTime(String isoDate) {
    try {
      final date = convertStringIsoToDate(isoDate);
      final min = date.minute.toString().padLeft(2, '0');
      final hour = (date.hour % 60).toString().padLeft(2, '0');
      return '$hour:$min';
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in convertToSentTime: $e');
      }
      return '00:00'; // Return a default value
    }
  }

  static int getDaysBetweenNowAnd(String targetDateIso) {
    try {
      DateTime now = getNow();
      final targetDate = convertStringIsoToDate(targetDateIso);
      Duration difference = now.difference(targetDate);
      return difference.inDays;
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in getDaysBetweenNowAnd: $e');
      }
      return 0; // Return a default value
    }
  }

  static int getDaysBetweenTwoDate(String firstDateIso, String targetDateIso) {
    try {
      DateTime now = convertStringIsoToDate(firstDateIso);
      final targetDate = convertStringIsoToDate(targetDateIso);
      Duration difference = now.difference(targetDate);
      return difference.inDays;
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in getDaysBetweenNowAnd: $e');
      }
      return 0; // Return a default value
    }
  }

  static Jalali getDateFromString(final bool isTime, final String date) {
    try {
      if (isTime) {
        final d = getNow();
        return Jalali.fromDateTime(d).copy(hour: int.tryParse(date));
      } else {
        final d = date.split('-');
        final dateInMiladi = DateTime(int.tryParse(d[0])!)
            .copyWith(month: int.tryParse(d[1]), day: int.tryParse(d[2]));
        final persianDate = Jalali.fromDateTime(dateInMiladi);

        return persianDate;
      }
    } catch (e) {
      // Handle the exception
      if (kDebugMode) {
        print('Error in getDaysBetweenNowAnd: $e');
      }
      final d = getNow();
      return Jalali.fromDateTime(d);
    }
  }

  static String normalizeTimeDuration(Duration input) {
    String minute;
    String second;

    if (input.inMinutes < 10) {
      minute = '0${input.inMinutes}';
    } else {
      minute = input.inMinutes.toString();
    }
    int realSeconds = input.inSeconds % 60;
    if (realSeconds < 10) {
      second = '0$realSeconds';
    } else {
      second = (realSeconds).toString();
    }
    return '$minute:$second';
  }
}
