package com.providence.tapestry.components.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.tapestry.annotations.*;

import org.apache.tapestry.AbstractComponent;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.IScript;
import org.apache.tapestry.IScriptProcessor;
import org.apache.tapestry.PageRenderSupport;
import org.apache.tapestry.TapestryUtils;
import org.apache.tapestry.annotations.InjectScript;
import org.apache.tapestry.asset.ExternalResource;
import org.apache.tapestry.engine.IScriptSource;
import org.apache.tapestry.html.Body;

import com.providence.tapestry.components.map.MapPlotPoint;

public abstract class TapMap extends AbstractComponent{
	
	
	//private Collection List = new ArrayList(0);
	//private IScript _script;
	//MapPlotPoint mapPlotPoint = new MapPlotPoint();
	//int zoom = getZoomLevel();
	// mapZoom = "map.setCenter(new GLatLng("+getMapCenterLat()+","+getMapCenterLong()+"),"+zoom+");";
	
	
	
	public abstract List<MapPlotPoint> getMapPlotPoints();
	//public abstract String getKey(); // Google Map API Key
	public abstract Boolean getAutoSize(); // Tells the map whether to automatically position and size itself (based on the objects it will plot on the map using the mapPlotPoints list;
	public abstract Double getMapCenterLat(); // Lattitude of the map center if autoSize is set to false
	public abstract Double getMapCenterLong(); // Longitude of the map center if autoSize is to false
	public abstract Integer getZoomLevel(); //sets the zoom level of the map if autoSize is false
	public abstract Integer getMapHeight(); // height of the map in pixels
	public abstract Integer getMapWidth(); // width of the map in pixels
	public abstract Boolean getShowControls(); //Shows or hides the map controls
	public abstract String getMapId(); // map id (div id)
	
	@InjectScript("TapMap.script") // Annotation to import the javascript file
	public abstract IScript getTapMapScript(); // Injected script 
	
    protected void renderComponent(IMarkupWriter writer,IRequestCycle cycle){
		
    	
		//writer.beginEmpty("body");
		//writer.attribute("bgcolor","red");
    	
		System.out.println("MapCenterLat : "+getMapCenterLat());
    	System.out.println("MapCenterLong : "+getMapCenterLong());
    	System.out.println("ZoomLevel : "+getZoomLevel());
    	System.out.println("MapHeight : "+getMapHeight());
    	System.out.println("MapWidth : "+getMapWidth());
    	System.out.println("ShowControls : "+getShowControls());
    	System.out.println("Array Contents : "+getMapPlotPoints());
    	//System.out.println("GoogleMapAPIKey : "+getKey());
		
    	String styleSheet = "width:"+getMapWidth()+"px;"+"height:"+getMapHeight()+"px";
    	
    	writer.beginEmpty("div");
    	writer.attribute("id",getMapId());
    	writer.attribute("style", styleSheet);
    	
    	
    	Map symbols = new HashMap();
            
        symbols.put("mapCenterLat", String.valueOf(getMapCenterLat()));
        symbols.put("mapCenterLong", String.valueOf(getMapCenterLong()));
        symbols.put("mapId", getMapId());
        symbols.put("mapWidth", String.valueOf(getMapWidth()));
        symbols.put("mapHeight",String.valueOf(getMapHeight()));
        symbols.put("zoomLevel", String.valueOf(getZoomLevel()));
        symbols.put("autoSize", Boolean.valueOf(getAutoSize()));
        symbols.put("showControls", Boolean.valueOf(getShowControls()));    
        //symbols.put("GoogleMapAPIKey", String.valueOf(getKey()));    
               
        PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
        getTapMapScript().execute(cycle, pageRenderSupport, symbols);
        //getTapMapScript().@link execute(cycle, pageRenderSupport, symbols)};
        
        String path = "http://maps.google.com/maps?file=api&v=2&key=ABQIAAAATksKrRgRqRI7G3NrSa3eEhTwM0brOpm-All5BF6PoaKBxRWWERSBSP05QF605GhTmNEsJ_YuTjTO2Q";
        pageRenderSupport.addExternalScript(new ExternalResource(path, null));
		 
	}
    
  
    /**
    public void submitToList()
    	{
    		//List.add(mapPlotPoint);
    	}
    public TapMap()
    	{
    		
    	}
	*/
}
