package com.andela.motustracker.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.andela.motustracker.R;
import com.andela.motustracker.helper.AppContext;
import com.andela.motustracker.model.LocationData;

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

/**
 * Created by Spykins on 06/04/16.
 */
public class SharedPreferenceManager {
    private Context context;
    private static SharedPreferenceManager sharedPreferenceManager;
    private SharedPreferences sharedPreferences;
    private SharedPreferenceManager(){
        context = AppContext.get();
        sharedPreferences = context.getSharedPreferences(
                context.getString(R.string.details_for_db), Context.MODE_PRIVATE);;

    }

    public static SharedPreferenceManager getInstance() {
        if(sharedPreferenceManager == null) {
            sharedPreferenceManager = new SharedPreferenceManager();
            return sharedPreferenceManager;
        }
        return sharedPreferenceManager;
    }

    public void setTimeSpent(long timeSpent) {

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(context.getString(R.string.time), timeSpent);
        editor.apply();

    }

    public long getTimeSpent() {
        long time = sharedPreferences.getLong(context.getString(R.string.time), 0);
        Log.d("waleola", "called SharedPreferenceManager.. time .... = " + time);
        return time;
    }

    public void setDate() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(context.getString(R.string.date), new Date().getTime());
        editor.apply();

    }

    public String getDate() {

        Date date = new Date (sharedPreferences.getLong(context.getString(R.string.date), 0));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public void setAddress(String address) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(context.getString(R.string.address), address);
        editor.apply();

    }

    public String getAddress() {
        return sharedPreferences.getString(context.getString(R.string.address), "");
    }

    public void  setLatitude(double latitude) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(context.getString(R.string.latitude), Double.doubleToLongBits(latitude));
        editor.apply();
    }

    public double getLatitude() {
        return Double.longBitsToDouble(sharedPreferences.getLong(context.getString(R.string.latitude), 0));
    }

    public void  setLongitude(double longitude) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(context.getString(R.string.longitude), Double.doubleToLongBits(longitude));
        editor.apply();
    }

    public double getLongitude() {
        return Double.longBitsToDouble(sharedPreferences.getLong(context.getString(R.string.longitude), 0));
    }

    public LocationData getLocationData() {
        return new LocationData(getAddress(),getDate(),getLatitude(), getLongitude(),
                getTimeSpent());
    }


}

@@ -1525,24 +1525,204 @@
 setDate() %7B%0A
+        Date date = new Date();%0A        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(%22dd/MM/yyyy HH:mm:ss%22);%0A        String dateFormat = simpleDateFormat.format(date);%0A
         Shar
@@ -1787,34 +1787,36 @@
       editor.put
-Lo
+Stri
 ng(context.getSt
@@ -1840,28 +1840,32 @@
 e), 
-new Date().getTime()
+dateFormat.split(%22 %22)%5B0%5D
 );%0A 
@@ -1925,25 +1925,24 @@
 ate() %7B%0A
-%0A
         
 Date dat
@@ -1937,30 +1937,15 @@
     
-Date date = new Date (
+return 
 shar
@@ -1953,34 +1953,36 @@
 dPreferences.get
-Lo
+Stri
 ng(context.getSt
@@ -2006,145 +2006,11 @@
 e), 
-0));%0A        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss%22);%0A        return simpleDateFormat.format(date
+%22 %22
 );%0A 
@@ -3319,11 +3319,10 @@
 %0A    %7D%0A%0A
-%0A
 %7D%0A

Extract the time in the date string out, only the date string from the date class is sent into the database