package com.xt.hb.utils
{
	import com.esri.ags.Graphic;
	import com.esri.ags.Map;
	import com.esri.ags.layers.GraphicsLayer;
	import com.esri.ags.symbols.CompositeSymbol;
	import com.esri.ags.symbols.SimpleFillSymbol;
	import com.esri.ags.symbols.SimpleLineSymbol;
	import com.esri.ags.symbols.TextSymbol;
	
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.FlexGlobals;

	/**
	 * 地图工具类，主要是地图操作相关接口
	 */
	public class MapUtils
	{
		/** 地图AIS刷新间隔 */
		public static const aisRefreshTime:Number=10000;
		/** 地图GPS刷新间隔 */
		public static const gpsRefreshTime:Number=10000;
		/** 船舶单位长度，按照长、宽系数同比例缩放,100米长度 */
		public static var c_x:Number;
		/** 船舶单位长度，按照长、宽系数同比例缩放,100米长度 */
		public static var c_y:Number;
		/** 速度 */
		public static var c_j:Number;
		/** 速度 */
		public static var c_j_1:Number;
		/** layerIndex */
		public static var layerIndex:Number=100;
		/**
		 * 不需要创建对象
		 */
		public function MapUtils(singletonEnforcer:SingletonEnforcer)
		{
		}
		/** 首页地图 */
		private static var _map:Map= null;
		/**
		 * 首页地图控件，首页初始化时需要设置，其他地方仅使用
		 */
		public static function get map():Map
		{
			if(_map == null){
				_map = FlexGlobals.topLevelApplication.map;
			}
			return _map;
		}
		/**
		 * 获取临时图标缓存图层
		 */
		public static function getTempGraphicLayer():GraphicsLayer{
			var tempGLayer:GraphicsLayer = map.getLayer("L_ID_TEMPG") as GraphicsLayer;
			if(null == tempGLayer){
				tempGLayer = new GraphicsLayer();
				tempGLayer.id="L_ID_TEMPG";
				tempGLayer.name="L_NAME_TEMPG";
				map.addLayer(tempGLayer, layerIndex++);
			}
			return tempGLayer;
		}
		/**
		 * 获取海事船舶图层
		 */
		public static function getHscbLayer():GraphicsLayer{
			var hscbLayer:GraphicsLayer = map.getLayer("L_ID_HSCB") as GraphicsLayer;
			if(null == hscbLayer){
				hscbLayer = new GraphicsLayer();
				hscbLayer.id="L_ID_HSCB";
				hscbLayer.name="L_NAME_HSCB";
				map.addLayer(hscbLayer, 999);
			}
			return hscbLayer;
		}
		/**
		 * 清除ais选中图标
		 */
		public static function clearAISGraphic():void{
			var tempG:GraphicsLayer = 	getTempGraphicLayer();
			var graphic:Graphic;
			for(var i:Number=0;i<tempG.graphicProvider.length;i++)
			{
				graphic = tempG.graphicProvider[i] as Graphic;
				if(graphic.name=="AISG")
					tempG.remove(graphic);
			}
		}
		/**
		 * 获取船舶弹出窗口
		 */
		public static function getShipPopWin():Canvas{
			return FlexGlobals.topLevelApplication.shipPopWin;
		}
		/**
		 * 根据图层名称获取图层
		 */
		public static function getGraphicLayerByName(name:String):GraphicsLayer{
			var tempGLayer:GraphicsLayer = map.getLayer(name) as GraphicsLayer;
			if(null == tempGLayer){
				tempGLayer = new GraphicsLayer();
				tempGLayer.id=name;
				tempGLayer.name=name;
				map.addLayer(tempGLayer, layerIndex++);
				map.reorderLayer("L_ID_HSCB", 999);
				
			}
			return tempGLayer;
		}
		/**
		 * 设置船舶单位长度
		 */
		public static function setCbSize(scale:Number):void{
			var default_x:Number=0.000150;
			var default_y:Number=0.000300;
			var default_j:Number=0.000900;
			var default_j_1:Number=0.000600;
			if(scale > 9000) {
				c_x=default_x;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
				c_y=default_y;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
				c_j=default_j;//速度
				c_j_1=default_j_1;//速度
			} else {
				var multi:Number = 2;
				while (scale*multi < 9000) {
					multi = multi * 2;
				}
				c_x=default_x/multi;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
				c_y=default_y/multi;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
				c_j=default_j/multi;//速度
				c_j_1=default_j_1/multi;//速度
			}
		}
		
		/**
		 * 海事图形移入事件
		 */ 
		public static function graphicMouseOver(e:MouseEvent):void
		{
			var g:Graphic=e.currentTarget as Graphic;
			g.symbol = getOverSymbolStyle();
		}
		
		/**
		 * 海事图形移出事件
		 */ 
		public static function graphicMouseOut(e:MouseEvent):void
		{
			var g:Graphic=e.currentTarget as Graphic;
			g.symbol = getDefaultSymbolStyle();
		}
		
		/**
		 * 设置默认symbol样式
		 */
		public static function getDefaultSymbolStyle() : SimpleFillSymbol {
			var symbol:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NULL, 0xFF0000, 0.1);
			symbol.outline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, 0xFF0000, 1.0, 2);
			return symbol;
		}
		
		/**
		 * 设置默认symbol样式
		 */
		public static function getOverSymbolStyle() : SimpleFillSymbol {
			var symbol:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, 0x0000FF, 0.3);
			symbol.outline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xF5E500, 1.0, 2);
			return symbol;
		}
		
		/**
		 * 设置自定义symbol样式
		 */
		public static function getOwnerSymbolStyle(color:Number,alpha:Number,
				outColor:Number, outWidth:Number, lineStyle:String = SimpleLineSymbol.STYLE_SOLID
				, fillStyle:String = SimpleFillSymbol.STYLE_SOLID
			) : SimpleFillSymbol {
			var symbol:SimpleFillSymbol = new SimpleFillSymbol(fillStyle, color, alpha);
			symbol.outline = new SimpleLineSymbol(lineStyle, outColor, 1.0, outWidth);
			return symbol;
		}
		
		/**
		 * 设置组合样式
		 */
		public static function getCompositeSymbol(fillSymbol:SimpleFillSymbol
				, textSymbol:TextSymbol):CompositeSymbol {
			var symbolArr:ArrayCollection = new ArrayCollection();
			symbolArr.addItem(textSymbol);
			symbolArr.addItem(fillSymbol);
			var symbol:CompositeSymbol = new CompositeSymbol(symbolArr);
			return symbol;
		}
		
		/**
		 * 设置文本Symbol
		 */
		public static function getTextSymbol(text:String):TextSymbol {
			var symbol:TextSymbol = new TextSymbol(text,text);
			trace(text);
			symbol.alpha = 1;
			symbol.placement="middle";
			symbol.border = false;
			//symbol.backgroundColor = 0xF5CEAD;
			//symbol.color = 0x000000;
			var format:TextFormat = new TextFormat();
			format.bold = true;
			format.color = 0x00FF00;
			symbol.textFormat = format;
			return symbol;
		}
		
	}
}
class SingletonEnforcer {}