package de.beyondsoft.ownchat.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by BoldijarPaul on 10/10/16.
 */
public abstract class DateUtils {

    static private DateFormat sHourDateFormat;
    static private DateFormat sDayFormat;
    static private DateFormat sDayOfYearFormat;

    private DateUtils() {

    }

    /**
     * Convert date object to string dd MMMM yyyy format.
     */
    public static String toDayOfYearString(Date date) {
        if (sDayOfYearFormat == null) {
            sDayOfYearFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
        }
        return sDayOfYearFormat.format(date);
    }

    /**
     * Convert long object to string HH:mm format.
     */
    public static String toHourFormatHour(long timestamp) {
        if (sHourDateFormat == null) {
            sHourDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
        }
        return sHourDateFormat.format(new Date(timestamp));
    }

    /**
     * Convert long object to string dd MMMM format.
     */
    public static String toHourOrDateFormatHour(long timestamp) {
        if (isToday(timestamp)) {
            return toHourFormatHour(timestamp);
        }
        if (sDayFormat == null) {
            sDayFormat = new SimpleDateFormat("dd MMMM", Locale.getDefault());
        }
        return sDayFormat.format(new Date(timestamp));
    }

    /**
     * Check if a date long object is today or not.
     */
    private static boolean isToday(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        Calendar today = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        return calendar.get(Calendar.DATE) == today.get(Calendar.DATE)
                && calendar.get(Calendar.MONTH) == today.get(Calendar.MONTH)
                && calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR);
    }

}
