package fishrungames.bashgid;



import java.util.ArrayList;

import android.app.AlertDialog;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapController;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;

import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.OverlayItem.HotspotPlace;

import fishrungames.bashgid.core.LocationHolder;


public class OsmMapHolderFragment extends Fragment
{
	
	private class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>{
	    private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();

	    public CustomItemizedOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
	        super(pDefaultMarker, pResourceProxy);
	    }

	    @Override
	    protected OverlayItem createItem(int pos) {
	        return mOverlayItems.get(pos);
	    }

	    @Override
	    public int size() {
	        if(mOverlayItems!=null) return mOverlayItems.size();
	        else return 0;
	    }

	    public void addOverlayItem(GeoPoint geoPoint){
	        
	    	
	    	
	    	/*if(Preferences.DEBUG) Log.d("MapActivity", "Latitude: "+latitude+" Longitude: "+longitude);
	        MarkerDrawable drawable = new MarkerDrawable(MapActivity.this, bitmap);
	        OverlayItem item = new OverlayItem("a", "a", new GeoPoint((int)(latitude*1e6), (int)(longitude*1e6)));
	        item.setMarker(drawable);
	        item.setMarkerHotspot(HotspotPlace.CENTER);
	        mOverlayItems.add(item);*/
	    	
	    	OverlayItem item = new OverlayItem("Title", "Lol, snippet", geoPoint);
	        item.setMarker(getResources().getDrawable(R.drawable.marker_default));
	        item.setMarkerHotspot(HotspotPlace.CENTER);
	        
	        mOverlayItems.add(item);
	        
	        populateNow();
	    }

	    public void populateNow(){
	        populate();
	    }

		@Override
		public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3)
		{
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		protected boolean onTap(int index) {
		  OverlayItem item = mOverlayItems.get(index);
		  AlertDialog.Builder dialog = new AlertDialog.Builder(OsmMapHolderFragment.this.getActivity());
		  dialog.setTitle(item.getTitle());
		  dialog.setMessage(item.getSnippet());
		  dialog.show();
		  return true;
		}
	}
	
	CustomItemizedOverlay itemizedOverlay;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			
		View rootView = inflater.inflate(R.layout.fragment_osm_map_page, container, false);
		
		MapView mapView = (MapView) rootView.findViewById(R.id.mapview);
		
		mapView.setMaxZoomLevel(20);
		mapView.setMinZoomLevel(4);
		
		mapView.setBuiltInZoomControls(true);
		mapView.setMultiTouchControls(true);
		
		
		IMapController mapController = mapView.getController();		
		
		
		GeoPoint center = new GeoPoint(LocationHolder.CONST_UFA_LATITUDE, LocationHolder.CONST_UFA_LONGITUDE);
			
		mapController.setZoom(10);
        mapController.animateTo(center);
        
        
        GeoPoint markerPoint = new GeoPoint( LocationHolder.CONST_UFA_LATITUDE, LocationHolder.CONST_UFA_LONGITUDE);
        
        
        ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getActivity());
        itemizedOverlay = new CustomItemizedOverlay(getResources().getDrawable(R.drawable.russia_icon), resourceProxy);
        
        itemizedOverlay.addOverlayItem(markerPoint);
        
        mapView.getOverlays().add(itemizedOverlay);
        
		return rootView;
	}
	

}
