package com.ford.openxc.rain;

import java.io.IOException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;

import org.apache.http.impl.client.DefaultHttpClient;

import com.openxc.measurements.Latitude;
import com.openxc.measurements.Longitude;
import com.openxc.measurements.UnrecognizedMeasurementTypeException;
import com.openxc.measurements.WindshieldWiperStatus;

import com.openxc.remote.NoValueException;

import com.openxc.VehicleService;

import android.os.Handler;

import android.util.Log;

import android.widget.TextView;

public class CheckWipersTask implements Runnable {
    private final String TAG = "CheckWipersTask";
    private final String WUNDERGROUND_URL =
        "http://www.wunderground.com/weatherstation/VehicleWeatherUpdate.php";

    private VehicleService mVehicleService;
    private Handler mHandler;
    private TextView mWiperStatusView;

    public CheckWipersTask(VehicleService vehicleService, Handler handler,
            TextView wiperStatusView) {
        mVehicleService = vehicleService;
        mHandler = handler;
        mWiperStatusView = wiperStatusView;
    }

    public void run() {
        final Latitude latitude;
        final Longitude longitude;
        final WindshieldWiperStatus wiperStatus;
        try {
            latitude = (Latitude) mVehicleService.get(Latitude.class);
            longitude = (Longitude) mVehicleService.get(Longitude.class);
            wiperStatus = (WindshieldWiperStatus) mVehicleService.get(
                    WindshieldWiperStatus.class);
        } catch(UnrecognizedMeasurementTypeException e) {
            return;
        } catch(NoValueException e) {
            Log.w(TAG, "One or more of the required measurements" +
                    " didn't have a value", e);
            return;
        }

        final boolean wiperStatusValue;
        wiperStatusValue = wiperStatus.getValue().booleanValue();

        mHandler.post(new Runnable() {
            public void run() {
                String wiperText;
                if(wiperStatusValue) {
                    wiperText = "on";
                } else {
                    wiperText = "off";
                }
                mWiperStatusView.setText(wiperText);
            }
        });
        uploadWiperStatus(latitude, longitude, wiperStatus);
        // Repeat every 5 minutes or 300,000ms
        mHandler.postDelayed(this, 300000);
    }

    private void uploadWiperStatus(Latitude latitude, Longitude longitude,
            WindshieldWiperStatus wiperStatus) {
        double latitudeValue;
        double longitudeValue;
        boolean wiperStatusValue;
        latitudeValue = latitude.getValue().doubleValue();
        longitudeValue = longitude.getValue().doubleValue();
        wiperStatusValue = wiperStatus.getValue().booleanValue();

        if(!wiperStatusValue) {
            Log.d(TAG, "Wipers are off -- not uploading");
            return;
        }

        StringBuilder uriBuilder = new StringBuilder(WUNDERGROUND_URL);
        // TODO need to update API to accept boolean instead of speed
        int wiperSpeed = 0;
        if(wiperStatusValue) {
            wiperSpeed = 1;
        }
        uriBuilder.append("?wiperspd=" + wiperSpeed);
        uriBuilder.append("&lat=" + latitudeValue);
        uriBuilder.append("&lon=" + longitudeValue);

        final HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(uriBuilder.toString());
        try {
            HttpResponse response = client.execute(request);
            final int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode != HttpStatus.SC_OK) {
                Log.w(TAG, "Error " + statusCode +
                    " while uploading wiper status");
            } else {
                Log.d(TAG, "Wiper status (" + wiperStatus + ") uploaded " +
                    "successfully");
            }
        } catch(IOException e) {
            Log.w(TAG, "Error while uploading wiper status", e);
        }
    }
}

@@ -2592,32 +2592,38 @@
 de,%0A            
+final 
 WindshieldWiperS
@@ -3445,17 +3445,148 @@
 Value);%0A
-%0A
+        final String finalUri = uriBuilder.toString();%0A%0A        new Thread(new Runnable() %7B%0A            public void run() %7B%0A        
         
@@ -3632,24 +3632,32 @@
 tpClient();%0A
+        
         Http
@@ -3686,32 +3686,27 @@
 Get(
-uriBuilder.toString());%0A
+finalUri);%0A        
     
@@ -3723,24 +3723,32 @@
             
+        
 HttpResponse
@@ -3780,24 +3780,32 @@
 e(request);%0A
+        
             
@@ -3881,16 +3881,24 @@
         
+   
+     
 if(statu
@@ -3934,32 +3934,40 @@
                 
+     
+   
 Log.w(TAG, %22Erro
@@ -4005,16 +4005,24 @@
         
+      
+  
 %22 while 
@@ -4051,32 +4051,40 @@
 %22);%0A            
+        
 %7D else %7B%0A       
@@ -4068,32 +4068,40 @@
        %7D else %7B%0A
+        
                 
@@ -4180,16 +4180,24 @@
         
+        
 %22success
@@ -4217,26 +4217,37 @@
         
-%7D%0A
+        %7D%0A   
         
 %7D catch(
@@ -4230,32 +4230,37 @@
    %7D%0A           
+     
 %7D catch(IOExcept
@@ -4260,32 +4260,40 @@
 OException e) %7B%0A
+        
             Log.
@@ -4345,22 +4345,64 @@
 e);%0A        
+     
-%7D
+   %7D%0A            %7D%0A        %7D).start();
 %0A    %7D%0A%7D%0A

Move wiper status upload to a separate thread to avoid net on main thread.