package com.example.android.wearable.watchface;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.wearable.watchface.WatchFaceService;
import android.text.format.Time;
import android.util.Log;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;

import com.google.android.gms.tagmanager.PreviewActivity;

import org.apache.commons.math3.distribution.NormalDistribution;

/**
 * Created by steven on 1/9/15.
 */
public class FuzzyTime extends Time {
    public static final String TAG = "FuzzyTime";
    static final int MSG_NEW_LOC = 1912391;

    private int lastUpdateTime;
    private int currentOffset;
    Context context;

    public FuzzyTime(Context context) {
        super();
        this.context = context;
    }

    public FuzzyTime() {
        super();
    }

    @Override
    public void setToNow() {
        RandomOffsetUtil.setNormalDistribution(5, 2);
        set(System.currentTimeMillis() + getOffset());
    }

    /**
     * offset getter
     * @return current offset in ms
     */
    public int getOffset() {
        return 1000 * 60 * currentOffset;
    }

    public int getOffsetInMinutes() {
        return getOffset()/(1000 * 60);
    }

    /**
     * updates the offset with a positive value sampled from the distribution
     *
     * @param atMinute minute that we are updating offset, so we don't update the offset
     *                 multiple calls in calls from the same minute
     */
    public void updateOffset(int atMinute) {
        if (atMinute != lastUpdateTime)
        {
            lastUpdateTime = atMinute;
            currentOffset = Math.abs((int) RandomOffsetUtil.sampleNormalDistribution());
        }
    }


    private static class RandomOffsetUtil {

        private static NormalDistribution distribution;

        public static void setNormalDistribution(double mean, double stddev) {

            Log.d(TAG, "Mean is now " + mean + " and stdev is now " + stddev);
            distribution = new NormalDistribution(mean, stddev);

            SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                    if (sharedPreferences.contains("key_mean") && sharedPreferences.contains("key_stdev")) {
                        Log.d(TAG, "Shared preferences have been updated");
                        setNormalDistribution(sharedPreferences.getInt("key_mean", 5),
                                sharedPreferences.getInt("key_stdev", 2));
                    }
                }
            };
        }

        public static double sampleNormalDistribution() {
            return distribution.sample();
        }

    }


    /** Handler to update the time once a second in interactive mode. */
    final Handler mUpdateTimeHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case MSG_NEW_LOC:



                    //// need to register



                    Log.v(TAG, "new location");
                    if (shouldAsk()) {

                    }
                    break;
            }
        }
    };


    public boolean shouldAsk(){

        return true;
    }

}

@@ -46,42 +46,8 @@
 e;%0A%0A
-import android.app.PendingIntent;%0A
 impo
@@ -78,39 +78,8 @@
 xt;%0A
-import android.content.Intent;%0A
 impo
@@ -135,420 +135,49 @@
 oid.
-net.Uri;%0Aimport android.os.Handler;%0Aimport android.os.Message;%0Aimport android.support.wearable.watchface.WatchFaceService;%0Aimport android.text.format.Time;%0Aimport android.util.Log;%0Aimport android.support.v4.app.NotificationCompat;%0Aimport android.support.v4.app.NotificationManagerCompat;%0Aimport android.support.v4.app.NotificationCompat.WearableExtender;%0A%0Aimport com.google.android.gms.tagmanager.PreviewActivity
+text.format.Time;%0Aimport android.util.Log
 ;%0A%0Ai
@@ -683,24 +683,272 @@
 etToNow() %7B%0A
+        int mean = context.getSharedPreferences(%22FuzzyTime%22, Context.MODE_PRIVATE)%0A                .getInt(%22key_mean%22, 5);%0A        int stdev = context.getSharedPreferences(%22FuzzyTime%22, Context.MODE_PRIVATE)%0A                .getInt(%22key_stdev%22, 2);%0A
         Rand
@@ -986,12 +986,19 @@
 ion(
-5, 2
+mean, stdev
 );%0A 
@@ -2786,16 +2786,18 @@
      %7D;%0A
+%0A%0A
         
@@ -2869,23 +2869,20 @@
         
-return 
+if (
 distribu
@@ -2890,583 +2890,279 @@
 ion.
-sample();%0A        %7D%0A%0A    %7D%0A%0A%0A    /** Handler to update the time once a second in interactive mode. */%0A    final Handler mUpdateTimeHandler = new Handler() %7B%0A        @Override%0A        public void handleMessage(Message message) %7B%0A            switch (message.what) %7B%0A                case MSG_NEW_LOC:%0A%0A%0A%0A                    //// need to register%0A%0A%0A%0A                    Log.v(TAG, %22new location%22);%0A                    if (shouldAsk()) %7B%0A%0A                    %7D%0A                    break;%0A            %7D%0A        %7D%0A    %7D;%0A%0A%0A    public boolean shouldAsk()%7B%0A%0A        return true;
+getMean() == 0d) %7B%0A                return 0d;%0A            %7D%0A            else %7B%0A                double sample;%0A                while ((sample = distribution.sample()) %3E (2 * distribution.getMean())) ;%0A                return  Math.abs(sample);%0A            %7D%0A        %7D%0A
 %0A    %7D%0A
-%0A
 %7D%0A

Update distribution with shared prefs