package com.lukekorth.httpebble.receivers;

import static com.getpebble.android.kit.Constants.MSG_DATA;
import static com.getpebble.android.kit.Constants.TRANSACTION_ID;

import org.json.JSONException;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;
import com.lukekorth.httpebble.Constants;

public class PebbleDataReceiver extends IntentService {

	public PebbleDataReceiver() {
		super("PebbleDataReceiver");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		int transactionId = intent.getIntExtra(TRANSACTION_ID, -1);
		PebbleKit.sendAckToPebble(this, transactionId);

		String data = intent.getStringExtra(MSG_DATA);
		if (data != null && data.length() != 0) {
			PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
			WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
					"PebbleDataReceiver");
			wakeLock.acquire();

			try {
				PebbleDictionary pebbleDictionary = PebbleDictionary.fromJson(data);

				// http request
				if (pebbleDictionary.getString(Constants.HTTP_URL_KEY) != null) {

				}
				// timezone infomation
				else if (pebbleDictionary
						.getUnsignedInteger(Constants.HTTP_TIME_KEY) != null) {

				}
				// location information
				else if (pebbleDictionary
						.getUnsignedInteger(Constants.HTTP_LOCATION_KEY) != null) {

				}
				// setting entries in key-value store
				else if (pebbleDictionary
						.getInteger(Constants.HTTP_COOKIE_STORE_KEY) != null) {

				}
				// removing entries from key-value store
				else if (pebbleDictionary
						.getInteger(Constants.HTTP_COOKIE_LOAD_KEY) != null) {

				}
				// deleting entries from key-value store
				else if (pebbleDictionary
						.getInteger(Constants.HTTP_COOKIE_DELETE_KEY) != null) {

				}
				// fsync
				else if (pebbleDictionary
						.getUnsignedInteger(Constants.HTTP_COOKIE_LOAD_KEY) != null) {

				}
			} catch (JSONException e) {
				// silently fail
			}

			wakeLock.release();
		}
	}

}

@@ -37,16 +37,76 @@
 ivers;%0A%0A
+import static com.getpebble.android.kit.Constants.APP_UUID;%0A
 import s
@@ -224,16 +224,90 @@
 ON_ID;%0A%0A
+import java.util.Date;%0Aimport java.util.TimeZone;%0Aimport java.util.UUID;%0A%0A
 import o
@@ -1314,16 +1314,82 @@
 n(data);
+%0A%09%09%09%09PebbleDictionary responseDictionary = new PebbleDictionary();
 %0A%0A%09%09%09%09//
@@ -1590,32 +1590,541 @@
 KEY) != null) %7B%0A
+%09%09%09%09%09responseDictionary.addInt32(Constants.HTTP_TIME_KEY,%0A%09%09%09%09%09%09%09(int) System.currentTimeMillis() / 1000);%0A%0A%09%09%09%09%09responseDictionary.addInt32(%0A%09%09%09%09%09%09%09Constants.HTTP_UTC_OFFSET_KEY,%0A%09%09%09%09%09%09%09TimeZone.getDefault().getOffset(%0A%09%09%09%09%09%09%09%09%09new Date().getTime()) / 1000);%0A%0A%09%09%09%09%09responseDictionary.addUint8(Constants.HTTP_IS_DST_KEY,%0A%09%09%09%09%09%09%09(byte) ((TimeZone.getDefault()%0A%09%09%09%09%09%09%09%09%09.inDaylightTime(new Date())) ? 1 : 0));%0A%0A%09%09%09%09%09responseDictionary.addString(Constants.HTTP_TZ_NAME_KEY,%0A%09%09%09%09%09%09%09TimeZone.getDefault().getID());
 %0A%09%09%09%09%7D%0A%09%09%09%09// lo
@@ -2788,24 +2788,183 @@
 ll) %7B%0A%0A%09%09%09%09%7D
+%0A%0A%09%09%09%09if (responseDictionary.size() %3E 0)%0A%09%09%09%09%09PebbleKit.sendDataToPebble(this,%0A%09%09%09%09%09%09%09(UUID) intent.getSerializableExtra(APP_UUID),%0A%09%09%09%09%09%09%09responseDictionary);
 %0A%09%09%09%7D catch 

Implement TimeZone response