package de.fau.cs.mad.kwikshop.android.view;

import org.joda.time.DateTime;
import org.joda.time.Hours;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.Minutes;
import org.joda.time.Period;
import org.joda.time.ReadableDuration;

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

import de.fau.cs.mad.kwikshop.android.R;
import de.fau.cs.mad.kwikshop.android.viewmodel.common.ResourceProvider;

public class DateFormatter {

    private final ResourceProvider resourceProvider;


    public DateFormatter(ResourceProvider resourceProvider) {

        if(resourceProvider == null) {
            throw new IllegalArgumentException("'resourceProvider' must not be null");
        }

        this.resourceProvider = resourceProvider;
    }


    public String formatDate(Date date) {

        DateTime now= DateTime.now();
        DateTime toFormat = new DateTime(date);


        //same day
        if(now.getYear() == toFormat.getYear() && now.getDayOfYear() == toFormat.getDayOfYear()) {

            int passedHours = Hours.hoursBetween(toFormat, now).getHours();

            if(passedHours == 0) {
                int passedMinutes = Minutes.minutesBetween(toFormat, now).getMinutes();

                //within 5 minutes, just display "just now"
                if(passedMinutes <= 5) {
                    return resourceProvider.getString(R.string.dateDescription_just_now);
                //within same hour display something like "10 minutes ago"
                } else {
                    String formatString = resourceProvider.getString(R.string.dateDescription_minutes_ago);
                    return String.format(formatString, passedMinutes);
                }

            } else  {

                //larger periods: display time
                return SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT).format(date);
            }

        //other day
        } else {

            DateFormat format = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
            return format.format(date);
        }

    }

}

@@ -468,16 +468,357 @@
 vider;%0A%0A
+/**%0A * Date formatter for human-friendly date strings:%0A *  - displays %22just now%22 if event was only minutes ago%0A *  - displays the amount of minutes passed since the date if date was within an our of current time%0A *  - displays the time of day for events of the same day%0A *  - displays only the date without time if the date is not today%0A */%0A
 public c
@@ -1215,16 +1215,17 @@
 Time now
+ 
 = DateTi
@@ -2241,16 +2241,46 @@
 at.SHORT
+, resourceProvider.getLocale()
 ).format
@@ -2427,16 +2427,46 @@
 at.SHORT
+, resourceProvider.getLocale()
 );%0A     

Use current locale in DateFormatter