import 'package:device_calendar/device_calendar.dart';
import 'package:intl/intl.dart';

class DateUtil {
  /// convert String To Date function
  /// [date] String we want to convert
  static DateTime convertStringToDate(String date) {
    if (date != null) {
      const start = "/Date(";
      const end = "+0300)";
      final startIndex = date.indexOf(start);
      final endIndex = date.indexOf(end, startIndex + start.length);
      return DateTime.fromMillisecondsSinceEpoch(
        int.parse(
          date.substring(startIndex + start.length, endIndex),
        ),
      );
    } else
      return DateTime.now();
  }

  static String convertDateToString(DateTime date) {
    const start = "/Date(";
    const end = "+0300)";
    int milliseconds = date.millisecondsSinceEpoch;

    return start + "$milliseconds" + end;
  }

  static String convertDateToStringLocation(DateTime date) {
    const start = "/Date(";
    const end = ")/";
    int milliseconds = date.millisecondsSinceEpoch;

    return start + "$milliseconds" + end;
  }

  static String yearMonthDay(DateTime dateTime) {
    String dateFormat = '${dateTime.year}-${dateTime.month}-${dateTime.day}';
    return dateFormat;
  }

  static String time(DateTime dateTime) {
    String dateFormat = '${dateTime.hour}:${dateTime.minute}:00';
    return dateFormat;
  }

  /// check Date
  /// [dateString] String we want to convert
  static String checkDate(DateTime checkedTime) {
    DateTime currentTime = DateTime.now();
    if ((currentTime.year == checkedTime.year) &&
        (currentTime.month == checkedTime.month) &&
        (currentTime.day == checkedTime.day)) {
      return "Today";
    } else if ((currentTime.year == checkedTime.year) &&
        (currentTime.month == checkedTime.month)) {
      if ((currentTime.day - checkedTime.day) == 1) {
        return "YESTERDAY";
      } else if ((currentTime.day - checkedTime.day) == -1) {
        return "Tomorrow";
      }

      if ((currentTime.day - checkedTime.day) <= -2) {
        return "Next Week";
      } else {
        return "Old Date";
      }
    }
    return "Old Date";
  }

  static String getDateFormatted(String date) {
    DateTime dateObj = DateUtil.convertStringToDate(date);
    return DateUtil.getWeekDay(dateObj.weekday) +
        ", " +
        dateObj.day.toString() +
        " " +
        DateUtil.getMonth(dateObj.month) +
        " " +
        dateObj.year.toString();
  }

  /// get month by
  /// [month] convert month number in to month name
  static getMonth(int month) {
    switch (month) {
      case 1:
        return "January";
      case 2:
        return "February";
      case 3:
        return "March";
      case 4:
        return "April";
      case 5:
        return "May";
      case 6:
        return "June";
      case 7:
        return "July";
      case 8:
        return "August";
      case 9:
        return "September";
      case 10:
        return "October";
      case 11:
        return "November";
      case 12:
        return "December";
    }
  }

  static getMonthByName(String month) {
    switch (month.toLowerCase()) {
      case 'january':
        return 1;
      case 'february':
        return 2;
      case 'march':
        return 3;
      case 'april':
        return 4;
      case 'may':
        return 5;
      case 'june':
        return 6;
      case 'july':
        return 7;
      case 'august':
        return 8;
      case 'september':
        return 9;
      case 'october':
        return 10;
      case 'november':
        return 11;
      case 'december':
        return 12;
    }
  }

  static getMonthDateTime(String month, yearName) {
    DateTime date;
    try {
      date = DateTime(int.parse(yearName), getMonthByName(month));
    } catch (e) {
      print(e);
    }
    return date ?? DateTime.now();
  }

  /// get month by
  /// [weekDay] convert week day in int to week day name
  static getWeekDay(int weekDay) {
    switch (weekDay) {
      case 1:
        return "Monday";
      case 2:
        return "Tuesday";
      case 3:
        return "Wednesday";
      case 4:
        return "Thursday";
      case 5:
        return "Friday";
      case 6:
        return "Saturday ";
      case 7:
        return "Sunday";
    }
  }

  /// get data formatted like Apr 26,2020
  /// [dateTime] convert DateTime to data formatted
  static String getMonthDayYearDateFormatted(DateTime dateTime) {
    if (dateTime != null)
      return getMonth(dateTime.month) +
          " " +
          dateTime.day.toString() +
          ", " +
          dateTime.year.toString();
    else
      return "";
  }

  /// get data  formatted like 26/4/2020
  /// [dateTime] convert DateTime to data formatted
  static String getDayMonthYearDateFormatted(DateTime dateTime) {
    if (dateTime != null)
      return dateTime.day.toString() +
          "/" +
          dateTime.month.toString() +
          "/" +
          dateTime.year.toString();
    else
      return "";
  }

  /// get data  formatted like 26/4/2020 10:30
  /// [dateTime] convert DateTime to data formatted
  static String getDayMonthYearHourMinuteDateFormatted(DateTime dateTime) {
    if (dateTime != null)
      return dateTime.day.toString() +
          "/" +
          dateTime.month.toString() +
          "/" +
          dateTime.year.toString() +
          " " +
          dateTime.hour.toString() +
          ":" +
          dateTime.minute.toString();
    else
      return "";
  }

  /// get data  formatted like 2020-8-13 09:43:00
  /// [dateTime] convert DateTime to data formatted
  static String getYearMonthDayHourMinSecDateFormatted(DateTime dateTime) {
    if (dateTime != null)
      return dateTime.year.toString() +
          "-" +
          dateTime.month.toString() +
          "-" +
          dateTime.day.toString() +
          " " +
          dateTime.hour.toString() +
          ":" +
          dateTime.minute.toString() +
          ":" +
          dateTime.second.toString();
    else
      return "";
  }

  static convertISODateToJsonDate(String isoDate) {
    return "/Date(" +
        DateFormat('mm-dd-yyy')
            .parse(isoDate)
            .millisecondsSinceEpoch
            .toString() +
        ")/";
  }

  static String getDay(DayOfWeek dayOfWeek) {
    switch (dayOfWeek) {
      case DayOfWeek.Monday:
        return "Monday";
        break;
      case DayOfWeek.Tuesday:
        return "Tuesday";
        break;
      case DayOfWeek.Wednesday:
        return "Wednesday";
        break;
      case DayOfWeek.Thursday:
        return "Thursday";
        break;
      case DayOfWeek.Friday:
        return "Friday";
        break;
      case DayOfWeek.Saturday:
        return "Saturday";
        break;
      case DayOfWeek.Sunday:
        return "Sunday";

        break;
    }
    return "";
  }
}
