package mn.devfest.map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import mn.devfest.R;

/**
 * Fragment that displays an event map
 *
 * @author bherbst
 */
public class MapFragment extends Fragment implements OnMapReadyCallback {
    //TODO extract constants to an easy to update file
    private static final double CONFERENCE_CENTER_SOUTHWEST_CORNER_LAT = 44.973986;
    private static final double CONFERENCE_CENTER_SOUTHWEST_CORNER_LONG = -93.278120;
    private static final double CONFERENCE_CENTER_NORTHEAST_CORNER_LAT = 44.974714;
    private static final double CONFERENCE_CENTER_NORTHEAST_CORNER_LONG = -93.276806;
    private static final float CONFERENCE_CENTER_ZOOM_LEVEL = 18.5f;
    private static final int CONFERENCE_CENTER_FLOOR_OVERLAY_COUNT = 3;

    @Bind(R.id.map_view)
    MapView mMapView;
    //TODO add a re-center button
    //TODO add a widget to select floor

    GoogleMap mMap;
    ArrayList<GroundOverlay> mFloorOverlayArray = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_map, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mMapView != null){
            mMapView.onDestroy();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        //Move the camera to focus on the conference center
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getConferenceCenterBounds().getCenter(), CONFERENCE_CENTER_ZOOM_LEVEL));
        //Add ground overlays
        GroundOverlayOptions firstFloorOverlayOptions = new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromResource(R.drawable.schultze_level_one))
                .positionFromBounds(getConferenceCenterBounds());
        mFloorOverlayArray.add(mMap.addGroundOverlay(firstFloorOverlayOptions));
    }

    /**
     * Resets the maps camera to it's original location
     */
    private void recenterMap() {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getConferenceCenterBounds().getCenter(), CONFERENCE_CENTER_ZOOM_LEVEL));
    }

    /**
     * Provides the Lat-Long bounds of the conference center
     * @return The Lat-Long bounds of the conference center
     */
    private LatLngBounds getConferenceCenterBounds() {
        LatLngBounds CONFERENCE_CENTER =
                new LatLngBounds(
                        new LatLng(CONFERENCE_CENTER_SOUTHWEST_CORNER_LAT,CONFERENCE_CENTER_SOUTHWEST_CORNER_LONG),
                        new LatLng(CONFERENCE_CENTER_NORTHEAST_CORNER_LAT,CONFERENCE_CENTER_NORTHEAST_CORNER_LONG));
        return CONFERENCE_CENTER;
    }
}

@@ -1460,51 +1460,133 @@
  int
- CONFERENCE_CENTER_FLOOR_OVERLAY_COUNT = 3;
+%5B%5D FLOOR_OVERLAY_ID_ARRAY = %7BR.drawable.schultze_level_one, R.drawable.schultze_level_two, R.drawable.schultze_level_three%7D;%0A
 %0A%0A  
@@ -2977,16 +2977,26 @@
         
+ArrayList%3C
 GroundOv
@@ -3011,15 +3011,149 @@
 ions
+%3E
  f
-irstF
+loorOverlayArray = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C FLOOR_OVERLAY_ID_ARRAY.length; i++) %7B%0A            GroundOverlayOptions f
 loor
@@ -3212,16 +3212,20 @@
         
+    
 .image(B
@@ -3264,40 +3264,71 @@
 rce(
-R.drawable.schultze_level_one))%0A
+FLOOR_OVERLAY_ID_ARRAY%5Bi%5D))%0A                    .zIndex(i)%0A    
     
@@ -3385,24 +3385,28 @@
 rBounds());%0A
+    
         mFlo
@@ -3451,13 +3451,8 @@
 ay(f
-irstF
 loor
@@ -3465,24 +3465,34 @@
 yOptions));%0A
+        %7D%0A
     %7D%0A%0A    /

Move to an array of drawable IDs (in the interest of open-sourcing things for different floor-counts easier later)