package com.microaiddreamworks.gps;

import com.microaiddreamworks.util.ServerMethods;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ThisGps extends Activity implements View.OnClickListener,LocationListener{
	ServerMethods mObjServerMethods = null;
	private Button m_cButton = null;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps);
		m_cButton = (Button) findViewById(R.id.PANIC_BUTTON);
		m_cButton.setBackgroundColor(Color.RED);
		m_cButton.setOnClickListener(this);
		View lViewSettng = findViewById(R.id.RELATIVE_SETTINGS);
        lViewSettng.setOnClickListener(this);
        View lViewGPS = findViewById(R.id.RELATIVE_GPSFIX);
        lViewGPS.setOnClickListener(this);
       // Intent lIntent1 = new Intent(this,ThisGps.class); 
       // startActivityForResult(lIntent1, 0);
        whereAmI();
        
	}
	
	public void onClick(View pView) {
		switch (pView.getId()) {
		case R.id.RELATIVE_SETTINGS:
			Intent lIntent = new Intent(this, PassCode.class);
			startActivity(lIntent);
			break;
//		case R.id.RELATIVE_GPSFIX:
//			Intent lIntent1 = new Intent(this, ThisGps.class);
//			startActivityForResult(lIntent1, 0);
//			break;
		case R.id.PANIC_BUTTON:
			View lPanicText = findViewById(R.id.PANIC_BUTTON);
			lPanicText.setVisibility(View.VISIBLE);
			break;
		}
	}
	
	private void whereAmI(){
		LocationManager locationManager;
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        locationManager.setTestProviderLocation(provider, location);
        //provider = LocationManager.GPS_PROVIDER;
        locationManager.requestLocationUpdates(provider, 5000, 10, this);
	}
	
//	@Override
//	public void onActivityResult(int requestCode, int resultCode, Intent lData) {
//	  super.onActivityResult(requestCode, resultCode, lData);
//	        if (resultCode == Activity.RESULT_OK)
//		      {
//			      String llanflatValue = lData.getStringExtra("value");
//			      showLatLang(llanflatValue);
//		      }
//	    }
	public void showLatLang(String pValue){
		TextView lLocationText = (TextView)findViewById(R.id.LBL_CAPTION_VALSUE);
		lLocationText.setText(pValue);
	}
	@Override
	public void onLocationChanged(Location location) {
		updateWithNewLocation(location, -1);
		
	}
	
	@Override
	public void onProviderDisabled(String provider) {
		System.out.println(provider);
	
		
	}
	@Override
	public void onProviderEnabled(String provider) {
		System.out.println(provider);
		
	}
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	//	updateWithNewLocation(null,status);
		
	}
	private void updateWithNewLocation(Location location, int pStatus) {
		String latLongString = null;
     	TextView myLocationText;
     	myLocationText = (TextView)findViewById(R.id.LBL_CAPTION_VALSUE);
     	if (location != null) {
     	double lat = location.getLatitude();
     	double lng = location.getLongitude();
     		latLongString = lat+","+lng;
     	} else if(1 == pStatus){
     		latLongString = "unable to fetch a location";
     	}
     	else{
     		latLongString = "No location found";
     	}
     	myLocationText.setText("Your Current Position is:\n" + latLongString);
     	//Intent lResultIntent = new Intent();
     	//lResultIntent.putExtra("value",latLongString );
 		//setResult(Activity.RESULT_OK, lResultIntent);
 		//finish();
 		TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 		String imei = mTelephonyMgr.getDeviceId();
 		mObjServerMethods = new ServerMethods();
 		mObjServerMethods.setLocation(imei,latLongString);
 		
     	showLatLang(latLongString);
	}
}
		
		
	
