package com.hellosanket.sol;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.location.Location;

import com.luckycatlabs.sunrisesunset.SunriseSunsetCalculator;
import com.luckycatlabs.sunrisesunset.dto.MyLocation;

import java.util.Calendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

/**
 *
 */
public class SolarDataIntentService extends IntentService {
    private static final String ACTION_COMPUTE = "com.hellosanket.sol.action.FOO";
    private SunriseSunsetCalculator mCalculator;
    private static final String TAG = "SolarDataIntentService";

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

    /**
     * Starts this service to perform action
     *
     * @see IntentService
     */

    public static void startComputeService(final Context context, final Location location) {
        Intent intent = new Intent(context, SolarDataIntentService.class);
        intent.setAction(ACTION_COMPUTE);
        intent.putExtra(Constants.SOLAR_DATA_INTENT_LOC_EXTRA, location);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_COMPUTE.equals(action)) {
                Location location = intent.getParcelableExtra(Constants.SOLAR_DATA_INTENT_LOC_EXTRA);
                try {
                    handleActionCompute(location);
                } catch (NullPointerException e) {
                    L.e(TAG, "failed to get location");
                }
            }
        }
    }

    /**
     * Handle action Compute in the provided background thread with the provided
     * parameters.
     */
    private void handleActionCompute(final Location location) {
        MyLocation loc;
        try {
            loc = new MyLocation(new Double(location.getLatitude()).toString(),
                    new Double(location.getLongitude()).toString());
            String timeZone = SimpleTimeZone.getDefault().getID();
            L.d(TAG,"my timzezone is " + timeZone);
            SunriseSunsetCalculator calculator = new SunriseSunsetCalculator(loc, timeZone);
            String sunrise = calculator.getOfficialSunriseForDate(Calendar.getInstance());
            String sunset = calculator.getOfficialSunsetForDate(Calendar.getInstance());
            L.d(TAG, "Sunrise at " + sunrise);
            L.d(TAG, "Sunset at " + sunset);
            DataWrapper.saveString(getApplicationContext(),
                    Constants.SOL_DB, Constants.SUNRISE_TIME_TEXT_KEY, sunrise);
            DataWrapper.saveString(getApplicationContext(),
                    Constants.SOL_DB, Constants.SUNSET_TIME_TEXT_KEY, sunset);

        } catch (NullPointerException e) {
            L.e(TAG, "failed to get lat long");
        }


    }

}

@@ -80,21 +80,22 @@
 content.
-Inten
+Contex
 t;%0Aimpor
@@ -104,38 +104,37 @@
 android.content.
-Contex
+Inten
 t;%0Aimport androi
@@ -269,24 +269,59 @@
 yLocation;%0A%0A
+import java.text.SimpleDateFormat;%0A
 import java.
@@ -371,35 +371,8 @@
 one;
-%0Aimport java.util.TimeZone;
 %0A%0A/*
@@ -521,11 +521,15 @@
 ion.
-FOO
+compute
 %22;%0A 
@@ -2118,60 +2118,8 @@
 ();%0A
-            L.d(TAG,%22my timzezone is %22 + timeZone);%0A
     
@@ -2223,22 +2223,24 @@
     
-String
+Calendar
  sunrise
  = c
@@ -2235,16 +2235,19 @@
  sunrise
+Cal
  = calcu
@@ -2270,16 +2270,24 @@
 lSunrise
+Calendar
 ForDate(
@@ -2327,21 +2327,26 @@
     
-String
+Calendar
  sunset
+Cal
  = c
@@ -2372,16 +2372,24 @@
 alSunset
+Calendar
 ForDate(
@@ -2405,32 +2405,33 @@
 getInstance());%0A
+%0A
             L.d(
@@ -2430,40 +2430,49 @@
     
-L.d(TAG, %22Sunrise at %22 + 
+String sunrise = getPrettyTime(
 sunrise
+Cal
 );%0A 
@@ -2486,41 +2486,51 @@
     
-L.d(TAG, %22Sunset at %22 + 
+String sunset = getPrettyTime(
 sunset
+Cal
 );%0A
+%0A
     
@@ -2907,17 +2907,212 @@
       %7D%0A
-%0A
+    %7D%0A%0A    private String getPrettyTime(Calendar cal) %7B%0A        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(%22LLL dd hh:mm a z%22);%0A        return simpleDateFormat.format(cal.getTime());
 %0A    %7D%0A%0A

fix time format