/*----------------    FILE HEADER ---------------------------------------
 This file is part of Geoide.
 Copyright (C) 2005-2006 by:
 IDgis B.V.
 http://www.idgis.nl

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

 Contact:

 Herman Assink
 IDgis bv
 P.O. Box 15
 7450 AA Holten
 The Netherlands
 E-Mail: herman.assink@idgis.nl

 * @version 1.4.0
 * @author IDgis team
 * 
 ------------------------------------------------------------------------*/
import mx.controls.Button;
import mx.utils.Delegate;

import nl.idgis.giclient.gui.ContainerContent;
import nl.idgis.giclient.framework.IDComboBox;
import nl.idgis.giclient.synch.SynchSocket;
import nl.idgis.giclient.framework.SinglePaneComponent;
import nl.idgis.giclient.framework.Frame;
import nl.idgis.giclient.framework.buttons.ActionButtonConfig;
import nl.idgis.giclient.framework.buttons.ActionButton;
import nl.idgis.giclient.framework.buttons.GIButton;
import nl.idgis.giclient.framework.buttons.GIButtonEventListener;
import nl.idgis.giclient.util.SRSInfo;

import mx.controls.ComboBase;

import nl.idgis.giclient.util.Strings;

class nl.idgis.giclient.gui.mapviewer.CoordinateInputFrame extends ContainerContent implements GIButtonEventListener {
    private var synchSocket:SynchSocket = null;
    private var getButton:Button = null;
    private var cancelButton:Button = null;
    private var crsPicker:IDComboBox = null;
    private var srsInfo:SRSInfo = null;
    private var statusDelayIntervalID:Number = 0;

    
    function onLoad():Void {
    	contentHeight = 150; 
	 synchSocket = frame.contentProperties.synchSocket;
     if (ruler==undefined){
     	ruler =  frame.contentProperties.ruler;
     }	
     var textFormat:TextFormat = new TextFormat();
     textFormat.font = "_sans";
     createTextField("status", this.getNextHighestDepth(), 0, 115, 300, 20, {enabled:false});
     this["status"].setNewTextFormat(textFormat);
     
     var textExtent:Object = textFormat.getTextExtent(Strings.getFile("Geoide").getString("CrsLabel"));
	 var textWidth:Number = textExtent.textFieldWidth + 20;
      createTextField("crsLabel", this.getNextHighestDepth(), 0, 0, textWidth, 20, {enabled:false});
      this["crsLabel"].setNewTextFormat(textFormat);
      this["crsLabel"].text =  Strings.getFile("Geoide").getString("CrsLabel");
      crsPicker = IDComboBox(attachMovie("IDComboBox", "crsPicker", this.getNextHighestDepth(), {_x: textWidth + 20, _y: 0, _width:250}));
      crsPicker.textField.setNewTextFormat(textFormat);
      crsPicker.addEventListener("select", Delegate.create(this, onSelectSrs));
      var sRSs:Array = ruler.getGIS().getActiveMap().getSupportedSRS();
      var crsProvider:Array = new Array({label: Strings.getFile("Geoide").getString("SelectCrsLabel"), crs:null});
      srsInfo =  ruler.getSRSInfo();
      var crsObject = new Object();
      var WGS84Found:Boolean = false;
      var srs = ruler.getGIS().getActiveMap().getSRS();
      var srsIndex:Number = -1;
      for(var n:Number = 0;n<sRSs.length;n++){
      	crsObject = {label:srsInfo.getTitle(sRSs[n])  , crs:sRSs[n]};
      	crsProvider.push(crsObject);
      	if(sRSs[n]=="EPSG:4326"){
      		WGS84Found = true;
      	}	
      	if(srs==sRSs[n]){
      		srsIndex = n;
      	}
      		
      }
      crsPicker.dataProvider = crsProvider;
      
      //TODO:set selected srs to currentSrs of the map
     // crsPicker.open();
      //crsPicker.selectItem(crsProvider[srsIndex + 1]);
      //crsPicker.selectedIndex = srsIndex + 1;

      crsPicker.resize();
      contentWidth = this["crsLabel"]._width + 40 + crsPicker._width;
      createTextField("XLabel", this.getNextHighestDepth(),0, 35, 110,20);
      this["XLabel"].setNewTextFormat(textFormat);
      this["XLabel"].text = "X:";
      createTextField("YLabel", this.getNextHighestDepth(),170, 35, 110,20);
      this["YLabel"].setNewTextFormat(textFormat);
      this["YLabel"].text = "Y:";     
      attachMovie("TextInput", "inputX", this.getNextHighestDepth());
      this["inputX"].setNewTextFormat(textFormat);
      this["inputX"]._x = 20;
      this["inputX"]._y = 35;
      this["inputX"].setSize(130, 20);
      this["inputX"].restrict = "0-9.";
      attachMovie("TextInput", "inputY", this.getNextHighestDepth());
      this["inputY"].setNewTextFormat(textFormat);
      this["inputY"]._x = 190;
      this["inputY"]._y = 35;
      this["inputY"].setSize(130, 20);
      this["inputY"].restrict = "0-9.";
      this.createTextField("exampleField", this.getNextHighestDepth(), 0, 
				60, 400, 30);
	 this["exampleField"].setNewTextFormat(textFormat);
	 if(crsPicker.getSelectedItem().crs!=null){
	 	setExampleText();
	 }
      drawButtonBar();
      frame.setSize(contentWidth, contentHeight);
      _parent.refreshPane(); 
	}
	

	private function onSelectSrs() : Void {
		setExampleText();
	}

	private function setExampleText() : Void {
		if(srsInfo.isProjection(crsPicker.getSelectedItem().crs)){
		 	this["exampleField"].text = Strings.getFile("Geoide").getString("InputCoordinatesInMeter");
		 	//meters
		 } else {
		 	//degrees
		 	this["exampleField"].text = Strings.getFile("Geoide").getString("InputCoordinatesInDegrees");
		 }		
	}

	
	private function drawButtonBar():Void{
		var buttonBar:MovieClip = this.createEmptyMovieClip("buttonBar",this.getNextHighestDepth());
		this["buttonBar"]._x = 20;
		buttonBar._y = 80;
		var initObject:Object = new Object();
		initObject["enabled"] = true;
		initObject["label"] = "test";
   	 	var actionButtonConfigs:Array =  SinglePaneComponent(frame.paneComponent).getGIButtonConfigs();
		if(actionButtonConfigs.length > 0){
		    for (var i:Number = 0; i < actionButtonConfigs.length; i++) {
		      var actionButtonConfig:ActionButtonConfig = ActionButtonConfig(actionButtonConfigs[i]);
		      actionButtonConfig.deflate();
		      var actionButton:ActionButton = ActionButton(actionButtonConfig.inflate(buttonBar, buttonBar.getNextHighestDepth(),this, initObject));
		      GIButton(actionButton).addEventListener(this);
		      
		    }
		} else {    
	    	getButton = Button(buttonBar.attachMovie("Button", "getButton_mc", buttonBar.getNextHighestDepth(), {_x: 0, _y: 7, _width: 80, label: "OK"}));
			getButton.addEventListener("click", Delegate.create(this,onClickGetButton));
        	cancelButton = Button(buttonBar.attachMovie("Button", "cancelButton_mc", buttonBar.getNextHighestDepth(), {_x: 100, _y: 7, _width: 80, label: "Annuleren"}));
        	cancelButton.addEventListener("click", Delegate.create(this,onClickCancelButton));
		}
     	   
    }
    
    public function getX():String{
    	return this["inputX"].text;
    }
    
     public function getY():String{
     	return this["inputY"].text;	
    }
    
    public function getCrs():String{
		return crsPicker.getSelectedItem().crs;	
    }
    
     
    private function onClickGetButton(eventObject:Object):Void {
		checkCoordinates();
    }
    
    private function checkCoordinates():Void{
	   	var error:Boolean = false;
	    if(crsPicker.selectedItem.crs == null){
	      error = true;
	      state(Strings.getFile("Geoide").getString("ChooseCoordinateSystem"));//"Kies eerst een coordinaat systeem !");
	    } else {
	      if(this["inputX"].text ==""){
	        error = true;
	        state(Strings.getFile("Geoide").getString("EnterX"));//"Voer de X coordinaat in !");
	      } else {
	          
	        if(this["inputY"].text ==""){
	          error = true;
	          state(Strings.getFile("Geoide").getString("EnterY"));//"Voer de Y coordinaat in !");
	        }
	      }
	    }   
	    if(!error){ 
	      	synchSocket.setSynchObjectValue("confirmed", true);
	        synchSocket.setSynchObjectValue("xInput", Number(this["inputX"].text));
	        synchSocket.setSynchObjectValue("yInput", Number(this["inputY"].text));
	        synchSocket.setSynchObjectValue("crs", crsPicker.selectedItem.crs);
	        synchSocket.invoke();
	    } 
    }
    
    function onReleaseGIButton(giButton:GIButton):Void {  	
    	state("");	
    }
    
    
    private function onClickCancelButton(eventObject:Object):Void {
      synchSocket.setSynchObjectValue("confirmed", false);
      synchSocket.invoke();
    }
    
    private function state(text:String){
      if (statusDelayIntervalID != 0) {
        clearInterval(statusDelayIntervalID);
        }
      statusDelayIntervalID = setInterval(this, "clearStatus", 5000);
      this["status"].text = text;
    }
    
    private function clearStatus():Void{
        clearInterval(statusDelayIntervalID);
      statusDelayIntervalID = 0;
      this["status"].text = "";
    }
    

    

    

  
}