package com.notipon;

import android.app.Service;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.content.Intent;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created by keith on 11/2/13.
 */
public class MainService extends Service  {
    public static final String TAG = "MainService";
    public static final String BROADCAST_ACTION = "com.notipon.MainService.broadcast";
    public static final String PACKAGE_NAME = "com.notipon";
    private static final String DEALS_EXTRA = "com.notipon.deals";
    private Handler handler = new Handler();
    private Intent intent;
    private static final int DELAY_MS = 5000;
    private Runnable updateRunnable;

    @Override
    public void onCreate() {
        super.onCreate();
        updateRunnable = new Runnable() {
            public void run() {
                // call the actual update function
                checkForResults();
                handler.postDelayed(this, DELAY_MS);
            }
        };
        intent = new Intent(BROADCAST_ACTION);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (updateRunnable == null) {
            handler.removeCallbacks(updateRunnable);
            handler.postDelayed(updateRunnable, DELAY_MS);
        }
        else {
            Log.e(TAG, "updateRunnable is null");
        }

        return START_STICKY;
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    private void checkForResults() {
        // get filter
        SharedPreferences settings = getSharedPreferences(PACKAGE_NAME, MODE_PRIVATE);
        Filter filter = Filter.getActiveFilter(settings);

        // run the search and filter
        ArrayList<Deal> deals = getDeals(filter);

        // call notification if match(es) found
        sendDeals(deals);
    }

    private void sendDeals(ArrayList<Deal> deals) {
        Intent intent = new Intent("whatever John says here");
        intent.putExtra(DEALS_EXTRA, deals);
        sendBroadcast(intent);
    }

    private ArrayList<Deal> getDeals() {
        ArrayList<Deal> examples = new ArrayList<Deal>();
        examples.add(new Deal("Caffe Vita", "http://www.caffevita.com/", "http://www.chefscollaborative.org/wp-content/uploads/2010/08/caffe-vita-logo-bw-650x1024.jpg", "Seattle"));
        examples.add(new Deal("Thai Curry Simple", "http://www.thaicurrysimple.com/", "http://www.thestranger.com/binary/c21d/Thai_Curry_Simple_Dominic_Holden.jpg", "Seattle"));
        return examples;
    }

    private ArrayList<Deal> getDeals(Filter filter) {
        return filter.apply(getDeals());
    }
}

@@ -1554,24 +1554,71 @@
 Results() %7B%0A
+        Log.d(TAG, %22Checking for results...%22);%0A
         // g
@@ -1857,24 +1857,368 @@
 s(filter);%0A%0A
+        // DEBUG%0A        for (Deal deal : deals) %7B%0A            Log.d(TAG, %22Found a deal!%22);%0A            Log.d(TAG, %22%5Ct%22 + deal.merchantName);%0A            Log.d(TAG, %22%5Ct%22 + deal.dealUrl);%0A            Log.d(TAG, %22%5Ct%22 + deal.imageUrl);%0A            for (String area : deal.areas) %7B%0A                Log.d(TAG, %22%5Ct%22 + area);%0A            %7D%0A        %7D%0A%0A
         // c

Debug code to make sure deals are being added properly.