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){
        sync(context, uri, null);
    }

    public static void sync(Context context, Uri uri, String keyExtra){
        long last = getLastSynch(context, uri, keyExtra);
        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){
        String type = Uris.getType(uri);
        String[] resolver = type.split("\\.");
        return String.format("%s_sync", resolver[resolver.length - 1].toLowerCase());
    }

    public static String getSynchKey(Uri uri, String suffix){
        String type = Uris.getType(uri);
        String[] resolver = type.split("\\.");
        if(TextUtils.isEmpty(suffix))
            return String.format("%s_sync", resolver[resolver.length - 1].toLowerCase());
        else
            return String.format("%s_%s_sync", resolver[resolver.length - 1].toLowerCase(), suffix);
    }

    public static long getLastSynch(Context context, Uri uri){
        return getLastSynch(context, uri, null);
    }

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

    public static void setLastSynch(Context context, Uri uri, String keyExtra){
        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,keyExtra);
        SharedPreferences preferences =
                PreferenceManager.getDefaultSharedPreferences(context);
        preferences.edit().putLong(key, synch).commit();
    }

    public static void setLastSynch(Context context, Uri uri){
        setLastSynch(context, uri, null);
    }
}

@@ -869,34 +869,8 @@
 ri;%0A
-        if(last %3E 0)%7B%0A    
     
@@ -913,32 +913,75 @@
 Instance();%0A    
+    long now = calendar.getTimeInMillis();%0A
         calendar
@@ -1000,28 +1000,24 @@
 llis(last);%0A
-    
         sync
@@ -1138,24 +1138,25 @@
 uild();%0A
+%0A
         
 %7D%0A      
@@ -1139,33 +1139,57 @@
 ild();%0A%0A        
-%7D
+// Build the synch Intent
 %0A        context
@@ -1185,29 +1185,24 @@
     
-context.startService(
+Intent intent = 
 new 
@@ -1238,16 +1238,209 @@
 ynchUri)
+;%0A        intent.putExtra(Intents.EXTRA_SYNCH, now);%0A        if(!TextUtils.isEmpty(keyExtra))%0A            intent.putExtra(Intents.EXTRA_SYNCH_KEY, keyExtra);%0A        context.startService(intent
 );%0A    %7D
@@ -3405,11 +3405,425 @@
 );%0A    %7D
+%0A%0A    public static void setSynchTime(Context context, Uri uri, String keyExtra,%0A                                    long time)%7B%0A        if(Uris.isItemType(uri))%7B%0A            return;%0A        %7D%0A        String key = getSynchKey(uri,keyExtra);%0A        SharedPreferences preferences =%0A                PreferenceManager.getDefaultSharedPreferences(context);%0A        preferences.edit().putLong(key, time).commit();%0A    %7D
 %0A%7D%0A

Update to synch methods