package com.example.petstoremobile.utils;

import android.util.Log;

import java.util.Calendar;
import java.util.Locale;

/**
 * Utility class for date and time operations.
 */
public class DateTimeUtils {

    private static final String TAG = "DateTimeUtils";

    /**
     * Formats status from backend format to UI format
     * (backend is using all caps so we lower case them with this function)
     */
    public static String formatStatusFromBackend(String status) {
        if (status == null || status.isEmpty()) return status;
        return status.substring(0, 1).toUpperCase() + status.substring(1).toLowerCase();
    }

    /**
     * Converts a date and time string to a Calendar object.
     * format: date = "YYYY-MM-DD", time = "HH:MM"
     */
    public static Calendar parseDateTimeToCalendar(String date, String time) throws Exception {
        Calendar calendar = Calendar.getInstance();
        String[] dateParts = date.split("-");
        String[] timeParts = time.split(":");

        calendar.set(
                Integer.parseInt(dateParts[0]),
                Integer.parseInt(dateParts[1]) - 1,
                Integer.parseInt(dateParts[2]),
                Integer.parseInt(timeParts[0]),
                Integer.parseInt(timeParts[1]),
                0
        );
        return calendar;
    }

    /**
     * Checks if a given date is in the past.
     * format: date = "YYYY-MM-DD"
     */
    public static boolean isDateInPast(String date) {
        if (date == null || date.isEmpty()) return false;
        try {
            Calendar selected = Calendar.getInstance();
            String[] dateParts = date.split("-");
            selected.set(
                    Integer.parseInt(dateParts[0]),
                    Integer.parseInt(dateParts[1]) - 1,
                    Integer.parseInt(dateParts[2]),
                    0, 0, 0
            );
            return selected.before(Calendar.getInstance());
        } catch (Exception e) {
            Log.e(TAG, "Error parsing date: " + e.getMessage());
            return false;
        }
    }

    /**
     * Checks if a given date is strictly before today (today and future return false).
     * format: date = "YYYY-MM-DD"
     */
    public static boolean isDateBeforeToday(String date) {
        if (date == null || date.isEmpty()) return false;
        try {
            String[] parts = date.split("-");
            Calendar today = Calendar.getInstance();
            today.set(Calendar.HOUR_OF_DAY, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);
            today.set(Calendar.MILLISECOND, 0);

            Calendar selected = Calendar.getInstance();
            selected.set(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]) - 1,
                    Integer.parseInt(parts[2]), 0, 0, 0);
            selected.set(Calendar.MILLISECOND, 0);
            return selected.before(today);
        } catch (Exception e) {
            Log.e(TAG, "Error parsing date: " + e.getMessage());
            return false;
        }
    }

    /**
     * Checks if a given date and time are in the past.
     * format: date = "YYYY-MM-DD", time = "HH:MM"
     */
    public static boolean isDateTimeInPast(String date, String time) {
        if (date == null || date.isEmpty() || time == null || time.isEmpty()) return false;
        try {
            Calendar selected = parseDateTimeToCalendar(date, time);
            return selected.before(Calendar.getInstance());
        } catch (Exception e) {
            Log.e(TAG, "Error parsing date/time: " + e.getMessage());
            return false;
        }
    }

    /**
     * Parses a time string and returns hour and minute values.
     */
    public static int[] parseTimeString(String time) {
        if (time == null || time.isEmpty()) return null;
        if (time.length() > 5) time = time.substring(0, 5);

        String[] parts = time.split(":");
        if (parts.length != 2) return null;

        try {
            int hour = Integer.parseInt(parts[0]);
            int min = Integer.parseInt(parts[1]);
            return new int[]{hour, min};
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * Formats an hour and minute into an HH:mm string.
     */
    public static String formatTime(int hour, int minute) {
        return String.format(Locale.getDefault(), "%02d:%02d", hour, minute);
    }

    /**
     * Extracts the date portion (YYYY-MM-DD) from timestamp string.
     * Returns null if the input is null or too short.
     */
    public static String extractDate(String timestamp) {
        if (timestamp == null || timestamp.length() < 10) return null;
        return timestamp.substring(0, 10);
    }

    /**
     * Formats an ID for display (e.g., "ID: 123").
     */
    public static String formatId(long id) {
        return String.format(Locale.getDefault(), "ID: %d", id);
    }
}
