package org.sana.android.app;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import org.sana.android.content.Intents;
import org.sana.android.content.Uris;
import org.sana.android.util.Dates;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by winkler.em@gmail.com, on 03/28/2017.
 */
public class SynchronizationManager {
    public static final String TAG = SynchronizationManager.class.getSimpleName();

    public static void sync(Context context, Uri uri){
        long last = getLastSynch(context, uri);
        Uri synchUri = uri;
        if(last > 0){
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(last);
            synchUri = uri.buildUpon().appendQueryParameter("modified__gt",
                    Dates.toSQL(calendar.getTime())).build();
        }
        context.startService(new Intent(Intents.ACTION_READ, synchUri));
    }

    public static String getSynchKey(Uri uri){
        return String.valueOf(Uris.getDescriptor(uri));
    }

    public static long getLastSynch(Context context, Uri uri){
        long last = 0L;
        String key = getSynchKey(uri);
        SharedPreferences preferences =
                PreferenceManager.getDefaultSharedPreferences(context);
        last = preferences.getLong(key, 0L);
        return last;
    }

    public static void setLastSynch(Context context, Uri uri){
        if(Uris.isItemType(uri)){
            return;
        }
        long synch = 0;
        String synchValue = uri.getQueryParameter("modified__gt");
        if(TextUtils.isEmpty(synchValue)){
            Date now = new Date();
            synch = now.getTime();
        } else {
            try {
                synch = Dates.fromSQL(synchValue).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String key = getSynchKey(uri);
        SharedPreferences preferences =
                PreferenceManager.getDefaultSharedPreferences(context);
        preferences.edit().putLong(key, synch).commit();
    }
}

@@ -658,32 +658,145 @@
 text, Uri uri)%7B%0A
+        sync(context, uri, null);%0A    %7D%0A%0A    public static void sync(Context context, Uri uri, String keyExtra)%7B%0A
         long las
@@ -824,16 +824,26 @@
 ext, uri
+, keyExtra
 );%0A     
@@ -1274,53 +1274,689 @@
     
-return String.valueOf(Uris.getDescriptor(uri)
+String type = Uris.getType(uri);%0A        String%5B%5D resolver = type.split(%22%5C%5C.%22);%0A        return String.format(%22%25s_sync%22, resolver%5Bresolver.length - 1%5D.toLowerCase());%0A    %7D%0A%0A    public static String getSynchKey(Uri uri, String suffix)%7B%0A        String type = Uris.getType(uri);%0A        String%5B%5D resolver = type.split(%22%5C%5C.%22);%0A        if(TextUtils.isEmpty(suffix))%0A            return String.format(%22%25s_sync%22, resolver%5Bresolver.length - 1%5D.toLowerCase());%0A        else%0A            return String.format(%22%25s_%25s_sync%22, resolver%5Bresolver.length - 1%5D.toLowerCase(), suffix);%0A    %7D%0A%0A    public static long getLastSynch(Context context, Uri uri)%7B%0A        return getLastSynch(context, uri, null
 );%0A 
@@ -2013,32 +2013,49 @@
 context, Uri uri
+, String keyExtra
 )%7B%0A        long 
@@ -2093,32 +2093,42 @@
  getSynchKey(uri
+, keyExtra
 );%0A        Share
@@ -2351,32 +2351,49 @@
 context, Uri uri
+, String keyExtra
 )%7B%0A        if(Ur
@@ -2877,24 +2877,33 @@
 SynchKey(uri
+,keyExtra
 );%0A        S
@@ -3067,11 +3067,123 @@
 );%0A    %7D
+%0A%0A    public static void setLastSynch(Context context, Uri uri)%7B%0A        setLastSynch(context, uri, null);%0A    %7D
 %0A%7D%0A

Update Synchronization Manager methods