package com.node22.breadcrumbs;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HistoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HistoryFragment extends Fragment implements HistoryResponse {
    @Override
    public void processFinish(Location[] locations) {
        Util.debug("Process has finished");

        Fragment fragment = getFragmentManager().findFragmentById(R.id.map);

        MapFragment mapFragment = (MapFragment)fragment;

        GoogleMap map = mapFragment.getMap();

        Util.debug("Adding locations to map");
        for (int i = 0; i < locations.length; i++) {
            Location location = locations[i];
            map.addMarker(
                    new MarkerOptions().position(
                        new LatLng(location.getLatitude(), location.getLongitude())).title("Location #" + Integer.toString(i + 1)));
        }

        map.addMarker(new MarkerOptions().position(new LatLng(43.652527, -79.381961)).title("Toronto"));
    }

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MapFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HistoryFragment newInstance(String param1, String param2) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public HistoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        Util.debug("MapFragment::onCreateView");

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.menu_map, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_refresh) {
            Util.debug("Refresh action is selected!");

            HistoryManager manager = new HistoryManager(this);
            manager.execute("94043");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

        Util.debug("Resume map fragment");
    }
}

@@ -264,16 +264,150 @@
 wGroup;%0A
+import android.widget.Toast;%0A%0Aimport com.google.android.gms.maps.CameraUpdate;%0Aimport com.google.android.gms.maps.CameraUpdateFactory;
 %0Aimport 
@@ -542,16 +542,71 @@
 LatLng;%0A
+import com.google.android.gms.maps.model.LatLngBounds;%0A
 import c
@@ -1223,16 +1223,84 @@
 o map%22);
+%0A%0A        LatLngBounds.Builder builder = new LatLngBounds.Builder();
 %0A       
@@ -1407,156 +1407,179 @@
     
-map.addMarker(%0A                    new MarkerOptions().position(%0A                        new LatLng(location.getLatitude(), location.getLongitude())
+LatLng position = new LatLng(location.getLatitude(), location.getLongitude());%0A            MarkerOptions marker = new MarkerOptions();%0A            marker.position(position
 ).ti
@@ -1621,26 +1621,26 @@
 (i + 1))
-)
 ;
+ 
 %0A       
  %7D%0A%0A    
@@ -1631,28 +1631,21 @@
 %0A       
- %7D%0A%0A
      
-   
 map.addM
@@ -1654,88 +1654,272 @@
 ker(
-new MarkerOptions().position(
+marker);%0A            builder.include(position);%0A        %7D%0A%0A        CameraUpdate cu = CameraUpdateFactory.
 new
- 
 LatLng
-(43.652527, -79.381961)).title(%22Toronto%22)
+Bounds(builder.build(), 10);%0A        map.animateCamera(cu);%0A%0A        Toast.makeText(getActivity(), %22Breadcrumbs refreshed.%22, Toast.LENGTH_SHORT).show(
 );%0A 

Update bounds and camera position