import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

String formatDateTime(DateTime? dateTime) {
  if (dateTime == null) {
    return '-';
  }
  final DateFormat dateFormatter = DateFormat('dd/MM/yyyy');
  final DateFormat timeFormatter = DateFormat('HH:mm');

  return '${dateFormatter.format(dateTime)} ${timeFormatter.format(dateTime)}';
}

String formatTimeOfDayToISO(TimeOfDay time, {DateTime? currentDate}) {
  final now = currentDate ?? DateTime.now();

  final dateTime = DateTime(
    now.year,
    now.month,
    now.day,
    time.hour,
    time.minute,
  );
  // Convert DateTime to Unix timestamp (in seconds)
  final unixTimestamp = dateTime.millisecondsSinceEpoch ~/ 1000;

  return unixTimestamp.toString();
}

String formatIsoStringToTime(String isoString, BuildContext context) {
  try {
    final parts = isoString.split(':');
    final hour = int.parse(parts[0]);
    final minute = int.parse(parts[1]);
    final timeOfDay = TimeOfDay(hour: hour, minute: minute);
    return timeOfDay.format(context);
  } catch (e) {
    return isoString;
  }
}
