package org.jush.helsinkilivetransport;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import org.jush.helsinkilivetransport.api.RealTimeVehicles;
import org.jush.helsinkilivetransport.api.RealTimeVehiclesApi;
import org.jush.helsinkilivetransport.api.VehicleMonitoringDelivery;

import retrofit.RestAdapter;

public class RealTimeActivity extends FragmentActivity {

    private final AsyncTask<Void, Void, VehicleMonitoringDelivery> fetchRealTimeVehiclesTask =
            new FetchRealTimeVehiclesTask();
    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_time);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        fetchRealTimeVehicles();
    }

    private void fetchRealTimeVehicles() {
        fetchRealTimeVehiclesTask.execute();
    }

    private class FetchRealTimeVehiclesTask extends AsyncTask<Void, Void, VehicleMonitoringDelivery> {
        @Override
        protected VehicleMonitoringDelivery doInBackground(Void... params) {
            RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://dev.hsl.fi/")
                    .build();
            RealTimeVehiclesApi service = restAdapter.create(RealTimeVehiclesApi.class);
            RealTimeVehicles realTimeVehicles = service.fetchRealTimeVehicles();
            Log.d(RealTimeActivity.class.getSimpleName(), "Result: " + realTimeVehicles);
            return realTimeVehicles.getSiri()
                    .getServiceDelivery()
                    .getVehicleMonitoringDeliveries()
                    .get(0);
        }

        @Override
        protected void onPostExecute(VehicleMonitoringDelivery vehicleMonitoringDelivery) {
            super.onPostExecute(vehicleMonitoringDelivery);
            // Clear the map
            mMap.clear();
            for (VehicleMonitoringDelivery.VehicleActivity vehicleActivity : vehicleMonitoringDelivery.getVehicleActivities()) {
                VehicleMonitoringDelivery.VehicleActivity.MonitoredVehicleJourney.VehicleLocation
                        vehicleLocation = vehicleActivity
                        .getMonitoredVehicleJourney()
                        .getVehicleLocation();
                mMap.addMarker(new MarkerOptions().position(new LatLng(vehicleLocation
                        .getLatitude(), vehicleLocation
                        .getLongitude())));
            }
        }
    }
}

@@ -38,37 +38,8 @@
 t;%0A%0A
-import android.os.AsyncTask;%0A
 impo
@@ -534,16 +534,69 @@
 ivery;%0A%0A
+import java.util.Timer;%0Aimport java.util.TimerTask;%0A%0A
 import r
@@ -691,134 +691,19 @@
 ate 
-final AsyncTask%3CVoid, Void, VehicleMonitoringDelivery%3E fetchRealTimeVehiclesTask =%0A            new FetchRealTimeVehiclesTask()
+Timer timer
 ;%0A  
@@ -1087,32 +1087,107 @@
 pMapIfNeeded();%0A
+        if (mMap != null) %7B%0A            fetchRealTimeVehicles();%0A        %7D%0A
     %7D%0A%0A    /**%0A 
@@ -2964,33 +2964,144 @@
     
+%7D%0A%0A
     
-fetchRealTimeVehicles
+@Override%0A    protected void onPause() %7B%0A        if (timer != null) %7B%0A            timer.cancel();%0A        %7D%0A        super.onPause
 ();%0A
@@ -3150,33 +3150,92 @@
 les() %7B%0A        
-f
+timer = new Timer();%0A        timer.scheduleAtFixedRate(new F
 etchRealTimeVehi
@@ -3246,17 +3246,22 @@
 Task
-.execute(
+(), 3000, 5000
 );%0A 
@@ -3322,150 +3322,62 @@
 nds 
-AsyncTask%3CVoid, Void, VehicleMonitoringDelivery%3E %7B%0A        @Override%0A        protected VehicleMonitoringDelivery doInBackground(Void... params
+TimerTask %7B%0A        @Override%0A        public void run(
 ) %7B%0A
@@ -3776,22 +3776,75 @@
         
-return
+final VehicleMonitoringDelivery vehicleMonitoringDelivery =
  realTim
@@ -3992,27 +3992,67 @@
 0);%0A        
-%7D%0A%0A
+    runOnUiThread(new Runnable() %7B%0A        
         @Ove
@@ -4069,124 +4069,206 @@
     
+   
-protected void onPostExecute(VehicleMonitoringDelivery vehicleMonitoringDelivery) %7B%0A            super.onPostExecute(
+     public void run() %7B%0A                    onPostExecute(vehicleMonitoringDelivery);%0A                %7D%0A            %7D);%0A        %7D%0A%0A        protected void onPostExecute(VehicleMonitoringDelivery 
 vehi
@@ -4281,33 +4281,34 @@
 itoringDelivery)
-;
+ %7B
 %0A            // 
@@ -4423,16 +4423,36 @@
 tivity :
+%0A                   
  vehicle
@@ -4469,16 +4469,37 @@
 Delivery
+%0A                    
 .getVehi

Use Timer to repeat fetching vehicle information