package tr.xip.wanikani.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.Date;

import tr.xip.wanikani.R;

/**
 * Created by xihsa_000 on 3/15/14.
 */
public class Utils {
    Context context;

    public Utils(Context context) {
        this.context = context;
    }

    public static String getTimeDifference(Context context, Date date1, Date date2) {

        long differenceInMilliseconds = date1.getTime() - date2.getTime();
        long differenceInMinutes = differenceInMilliseconds / 60000;
        long differenceInHours = differenceInMinutes / 60;
        long differenceInDays = differenceInHours / 24;
        double differenceInMonths = differenceInDays / 30.4368;

        if (differenceInMinutes == 0) {
            return context.getString(R.string.less_than_a_minute)
                    + getAgoIfPastTime(context, differenceInMilliseconds);
        } else if (differenceInMinutes >= 60 || differenceInMinutes <= -60) {
            if (differenceInHours >= 24 || differenceInHours <= -24) {
                if ((double) differenceInDays >= 30.4368 || (double) differenceInDays <= -30.4368) {
                    if (differenceInMonths == 1 || differenceInMonths == -1)
                        return context.getString(R.string.a_month)
                                + getAgoIfPastTime(context, differenceInMonths);
                    else
                        return (long) removeMinusFromLong(differenceInMonths) + " "
                                + context.getString(R.string.months)
                                + getAgoIfPastTime(context, differenceInMonths);
                } else {
                    if (differenceInDays == 1 || differenceInDays == -1)
                        return context.getString(R.string.a_day)
                                + getAgoIfPastTime(context, differenceInDays);
                    else
                        return (long) removeMinusFromLong(differenceInDays) + " "
                                + context.getString(R.string.days)
                                + getAgoIfPastTime(context, differenceInDays);
                }
            } else {
                if (differenceInHours == 1 || differenceInHours == -1)
                    return context.getString(R.string.an_hour)
                            + getAgoIfPastTime(context, differenceInDays);
                else
                    return (long) removeMinusFromLong(differenceInHours) + " "
                            + context.getString(R.string.hours)
                            + getAgoIfPastTime(context, differenceInDays);
            }
        } else {
            if (differenceInMinutes == 1 || differenceInMinutes == -1)
                return context.getString(R.string.a_minute)
                        + getAgoIfPastTime(context, differenceInMinutes);
            else
                return (long) removeMinusFromLong(differenceInMinutes) + " "
                        + context.getString(R.string.minutes)
                        + getAgoIfPastTime(context, differenceInMinutes);
        }
    }

    private static String getAgoIfPastTime(Context context, double time) {
        if (time < 0)
            return " " + context.getString(R.string.ago);
        else return "";
    }

    private static double removeMinusFromLong(double time) {
        if (time < 0) {
            String timeString = time + "";
            timeString = timeString.substring(1, timeString.length());
            return Double.parseDouble(timeString);
        } else return time;
    }

    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    public boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

    public float pxFromDp(float dp) {
        return dp * context.getResources().getDisplayMetrics().density;
    }
}

@@ -1104,25 +1104,16 @@
     if (
-(double) 
 differen
@@ -1130,25 +1130,11 @@
 = 30
-.4368 %7C%7C (double)
+ %7C%7C
  dif
@@ -1153,21 +1153,16 @@
 s %3C= -30
-.4368
 ) %7B%0A    
@@ -1172,32 +1172,33 @@
              if 
+(
 (differenceInMon
@@ -1198,23 +1198,77 @@
 ceIn
-Months == 1
+Days %3E= 30 && differenceInDays %3C 46)%0A                           
  %7C%7C 
+(
 diff
@@ -1279,20 +1279,46 @@
 ceIn
-Months == -1
+Days %3C= -30 && differenceInDays %3E -46)
 )%0A  
@@ -1337,32 +1337,39 @@
           return
+ %22~ %22 +
  context.getStri
@@ -1518,39 +1518,43 @@
          return 
-(long) 
+Math.round(
 removeMinusFromL
@@ -1576,16 +1576,17 @@
 nMonths)
+)
  + %22 %22%0A 

Improve time difference calculation