package com.codepath.apps.simplecptweets.utils;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Months;
import org.joda.time.Years;

import java.util.Date;

/**
 * Created by ZkHaider on 6/29/15.
 */
public class TimeUtils {

    private static final String TAG = TimeUtils.class.getSimpleName();

    public static String timeAgo(Date date) {
        DateTime d = new DateTime(date.getTime());
        return getString(d, new DateTime());
    }

    private static String getString(DateTime date, DateTime now) {
        if (toYears(date, now) != null) {
            return toYears(date, now);
        }
        if (toMonths(date, now) != null) {
            return toMonths(date, now);
        }
        if (toDays(date, now) != null) {
            return toDays(date, now);
        }
        if (toHours(date, now) != null) {
            return toHours(date, now);
        }
        return toMinutes(date, now);
    }

    private static String toYears(DateTime date, DateTime now) {
        int result = Years.yearsBetween(date, now).getYears();
        if (Math.abs(result) >= 1) {
            return Math.abs(result) == 1 ? (result == 1 ? "Last" : "Next") + " year" :
                    toReference(Math.abs(result), result, " year ");
        }
        return null;
    }

    private static String toMonths(DateTime date, DateTime now) {
        int result = Months.monthsBetween(date, now).getMonths();
        if (Math.abs(result) >= 1) {
            return Math.abs(result) == 1 ? (result == 1 ? "Last" : "Next") + " month" :
                    toReference(Math.abs(result), result, " mon ");
        }
        return null;
    }

    private static String toDays(DateTime date, DateTime now) {
        int result = Days.daysBetween(date, now).getDays();
        if (Math.abs(result) >= 1) {
            return Math.abs(result) == 1 ? (result == 1 ? "Yesterday" : "Tomorrow") :
                    toReference(Math.abs(result), result, " d ");
        }
        return null;
    }

    private static String toHours(DateTime date, DateTime now) {
        int result = Hours.hoursBetween(date, now).getHours();
        if (Math.abs(result) >= 1) {
            return Math.abs(result) == 1 ? "1 hour " + (result == 1 ? "ago" : "from now") :
                    toReference(Math.abs(result), result, " h ");
        }
        return null;
    }

    private static String toMinutes(DateTime date, DateTime now) {
        int minutes = Minutes.minutesBetween(date, now).getMinutes();
        return toReference(Math.abs(minutes), minutes, " m ");
    }

    private static String toReference(long prefix, long suffixValue, String suffix) {
        return prefix + suffix + (suffixValue > 0 ? "" : "from now");
    }

}
@@ -1193,96 +1193,8 @@
 urn 
-Math.abs(result) == 1 ? (result == 1 ? %22Last%22 : %22Next%22) + %22 year%22 :%0A                    
 toRe
@@ -1228,17 +1228,16 @@
 esult, %22
- 
 year %22);
@@ -1467,97 +1467,8 @@
 urn 
-Math.abs(result) == 1 ? (result == 1 ? %22Last%22 : %22Next%22) + %22 month%22 :%0A                    
 toRe
@@ -1502,17 +1502,16 @@
 esult, %22
- 
 mon %22);%0A
@@ -1732,95 +1732,8 @@
 urn 
-Math.abs(result) == 1 ? (result == 1 ? %22Yesterday%22 : %22Tomorrow%22) :%0A                    
 toRe
@@ -1767,17 +1767,16 @@
 esult, %22
- 
 d %22);%0A  
@@ -1999,101 +1999,8 @@
 urn 
-Math.abs(result) == 1 ? %221 hour %22 + (result == 1 ? %22ago%22 : %22from now%22) :%0A                    
 toRe
@@ -2034,17 +2034,16 @@
 esult, %22
- 
 h %22);%0A  
@@ -2211,24 +2211,24 @@
 tMinutes();%0A
+
         retu
@@ -2271,17 +2271,16 @@
 nutes, %22
- 
 m %22);%0A  

Modify timeline format