package jasenmoloy.wirelesscontrol;

import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

/**
 * Created by jasenmoloy on 2/17/16.
 */
public class AddGeofenceActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 1;

    private GoogleMap mMap;
    private LocationManager mLocationManager;
    private Location mLocation;

    /// ----------------------
    /// Public Methods
    /// ----------------------

    /// ----------------------
    /// Overwritten Methods
    /// ----------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_addgeofence);

        //Set the toolbar according to the activity layout
        Toolbar myChildToolbar =
                (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(myChildToolbar);

        //Enable the "Up" button to go back to the parent activity
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.addgeofence_geofencemap);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_ACCESS_FINE_LOCATION);
        }
        else {
            InitMyLocationOnMap();
        }
    }

    /// ----------------------
    /// Private Methods
    /// ----------------------

    private void InitMyLocationOnMap()
    {
        try {
            mMap.setMyLocationEnabled(true);

            if( mLocationManager == null ) {
                mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }

            LatLng currentPos = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentPos, (mMap.getMaxZoomLevel() * 0.8f));


            //Update the camera to point to where the user is located and zoom in a bit.
            mMap.animateCamera(cameraUpdate);
        }
        catch(SecurityException secEx) {
            //TODO Request permissions to access the user's location.
        }
        catch(Exception ex) {
            //TODO Print out a log.
        }

    }
}

@@ -64,32 +64,63 @@
 PackageManager;%0A
+import android.graphics.Color;%0A
 import android.l
@@ -137,16 +137,16 @@
 cation;%0A
-
 import a
@@ -393,44 +393,8 @@
 bar;
-%0Aimport android.view.LayoutInflater;
 %0A%0Aim
@@ -693,22 +693,134 @@
 s.model.
-LatLng
+CircleOptions;%0Aimport com.google.android.gms.maps.model.LatLng;%0Aimport com.google.android.gms.maps.model.MarkerOptions
 ;%0A%0A/**%0A 
@@ -3374,16 +3374,532 @@
 Update);
+%0A%0A            //Add a marker to your current location%0A            MarkerOptions markOps = new MarkerOptions();%0A            markOps.position(currentPos);%0A            mMap.addMarker(markOps);%0A%0A            //Add a radius to the current marker%0A            CircleOptions circleOps = new CircleOptions();%0A            circleOps.center(currentPos);%0A            circleOps.radius(60.0);%0A            circleOps.fillColor(Color.argb(50, 0, 0, 255));%0A            circleOps.strokeWidth(4.0f);%0A            mMap.addCircle(circleOps);
 %0A       

Add marker and radius with color to the current location the user is at