package edu.cmu.sv.trailscribe.view;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;

import edu.cmu.sv.trailscribe.R;
import edu.cmu.sv.trailscribe.dao.DBHelper;
import edu.cmu.sv.trailscribe.dao.LocationDataSource;

public class BaseActivity extends Activity implements 
	LocationListener,
	GooglePlayServicesClient.ConnectionCallbacks,
	GooglePlayServicesClient.OnConnectionFailedListener {
	
	public final static ActivityTheme ACTIVITY_THEME = new ActivityTheme("Default", "default", R.color.blue);
	public final static String MSG_TAG = "BaseActivity";
	
//	Application
	protected TrailScribeApplication mApplication;
	
//	Database
	protected DBHelper mDBHelper;
	
//	Location
	protected Location mLocation;
	protected LocationClient mLocationClient;
	
//	View
    protected DrawerLayout mDrawerLayout;
    protected ActionBar mActionBar;
    protected ActionBarDrawerToggle mDrawerToggle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication = (TrailScribeApplication) getApplication();
		mDBHelper = mApplication.getDBHelper();
		
		setLocationClient();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		setLocationClient();
    }
	
	@Override
	protected void onStop() {
		mLocationClient.disconnect();
		super.onStop();
    }
	
	@Override
	public void onConnectionFailed(ConnectionResult connectionResult) {
		Log.e(MSG_TAG, "Application has failed to connect to location service");
		Toast.makeText(getApplicationContext(), 
				"Application has failed to connect to location service", Toast.LENGTH_SHORT).show();

//		Google Play service can resolve some connection error. 
//		However, it requires to start the Google Play services activity.
//		For the simplicity of the application, this feature is not implemented for now.		
	}

	@Override
	public void onConnected(Bundle bundle) {
            if (mLocationClient.isConnected()) {
                Log.d(MSG_TAG, "Application is connected to Google Play services");
                mLocation = mLocationClient.getLastLocation();
                if (mLocation == null) {
                    Log.e(MSG_TAG, "Null last location");
                } else {
                    saveLocationToDatabase();
                }
            } else {
                Log.e(MSG_TAG, "onConnect called but client is not connected");
            }
	}

	@Override
	public void onDisconnected() {
		Log.e(MSG_TAG, "Application is disconnected from location service");
		Toast.makeText(getApplicationContext(), 
				"Application is disconnected from location service", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onLocationChanged(Location location) {
	    saveLocationToDatabase();
//		Action when location has changed will be decided by the activity 
	}
	
	private void saveLocationToDatabase() {
	    LocationDataSource dataSource = new LocationDataSource(mDBHelper);
	    edu.cmu.sv.trailscribe.model.Location loc = 
	            new edu.cmu.sv.trailscribe.model.Location(
	                    (int) (Math.random() * Integer.MAX_VALUE), "default time", 
	                    mLocation.getLongitude(), mLocation.getLatitude(), mLocation.getAltitude(), 
	                    0, 0, 0);
        dataSource.add(loc);
	}
	
	private void setLocationClient() {
            setLocationClient(new LocationClient(this, this, this));
	}

    // code to allow overiding of location client, needed for testing
    public void setLocationClient(LocationClient client) {
        mLocationClient = client;
        try {
            if (!TrailScribeApplication.isPlayServicesAvailable()) {
                Log.e(MSG_TAG, "Google Play service is not available");
                return;
            }
            mLocationClient.connect();
        } catch (Exception e) {
            Log.e(MSG_TAG, e.getMessage());
        }
    }

    // override the dbhelper so a test database can be used instead of
    // the default one
    public void setDBHelper(DBHelper helper) {
        mDBHelper = helper;
    }

	
	protected void setActionBar(String color) {
	    mActionBar = getActionBar();
        mActionBar.setTitle("");
        mActionBar.setIcon(R.drawable.icon_trailscribe);
        
        mActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(color)));
	}
}

@@ -929,30 +929,24 @@
 %7B%0A%09%0A%09public 
-final 
 static Activ
@@ -1038,14 +1038,8 @@
 lic 
-final 
 stat
@@ -1102,16 +1102,23 @@
 otected 
+static 
 TrailScr
@@ -1171,16 +1171,23 @@
 otected 
+static 
 DBHelper
@@ -1215,32 +1215,39 @@
 tion%0A%09protected 
+static 
 Location mLocati
@@ -1261,16 +1261,23 @@
 otected 
+static 
 Location
@@ -2422,73 +2422,10 @@
 ) %7B%0A
-            if (mLocationClient.isConnected()) %7B%0A                
+%09%09
 Log.
@@ -2488,32 +2488,30 @@
 ices%22);%0A
-                
+%09%09%0A%09%09try %7B%0A%09%09%09
 mLocatio
@@ -2549,32 +2549,19 @@
 tion();%0A
-                
+%09%09%09
 if (mLoc
@@ -2577,36 +2577,20 @@
 null) %7B%0A
-                    
+%09%09%09%09
 Log.e(MS
@@ -2623,210 +2623,121 @@
 %22);%0A
-                %7D else %7B%0A                    saveLocationToDatabase();%0A                %7D%0A            %7D else %7B%0A                Log.e(MSG_TAG, %22onConnect called but client is not connected%22);%0A            
+%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09saveLocationToDatabase();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09Log.e(MSG_TAG, e.getMessage());%0A%09%09
 %7D%0A%09%7D
@@ -4388,17 +4388,16 @@
 ;%0A    %7D%0A
-%0A
 %09%0A%09prote

Revert BaseActivity to near cmu/master state