import 'package:intl/intl.dart';

String capitalize(String s) {
  return s[0].toUpperCase() + s.substring(1);
}

String dateFormater(String date) {
  final DateTime dateTime = DateTime.parse(date);
  final DateFormat formatter = DateFormat('dd MMM yyyy');
  return formatter.format(dateTime);
}

String dateFormatterShort(String date) {
  final DateTime dateTime = DateTime.parse(date);
  final DateFormat formatter = DateFormat('yyyy-MM-dd');
  return formatter.format(dateTime);
}

String setTwoDigitAfterComma(double number) {
  return number.toStringAsFixed(2);
}

String getGreeting(String time) {
  DateTime parsedTime = DateTime.parse(time);
  int hour = parsedTime.hour;

  if (hour >= 5 && hour < 12) {
    return 'Selamat Pagi';
  } else if (hour >= 12 && hour < 15) {
    return 'Selamat Siang';
  } else if (hour >= 15 && hour < 18) {
    return 'Selamat Sore';
  } else {
    return 'Selamat Malam';
  }
}

String timeAgo(String? dateString) {
  if (dateString == null) {
    return '';
  }
  DateTime date = DateTime.parse(dateString);
  DateTime now = DateTime.now();

  Duration difference = now.difference(date);

  if (difference.inMinutes < 1) {
    return "Baru saja";
  } else if (difference.inMinutes < 60) {
    return "${difference.inMinutes} menit yang lalu";
  } else if (difference.inHours < 24) {
    return "${difference.inHours} jam yang lalu";
  } else if (difference.inDays < 7) {
    return "${difference.inDays} hari yang lalu";
  } else if (difference.inDays < 30) {
    int weeks = (difference.inDays / 7).floor();
    return "$weeks minggu yang lalu";
  } else if (difference.inDays < 365) {
    int months = (difference.inDays / 30).floor();
    return "$months bulan yang lalu";
  } else {
    int years = (difference.inDays / 365).floor();
    return "$years tahun yang lalu";
  }
}
