package com.xt.hb.ais
{
	import com.esri.ags.Graphic;
	import com.esri.ags.Map;
	import com.esri.ags.geometry.Extent;
	import com.esri.ags.geometry.Geometry;
	import com.esri.ags.geometry.MapPoint;
	import com.esri.ags.layers.GraphicsLayer;
	import com.esri.ags.symbols.SimpleFillSymbol;
	import com.esri.ags.symbols.SimpleLineSymbol;
	import com.esri.ags.symbols.SimpleMarkerSymbol;
	import com.esri.ags.utils.JSONUtil;
	import com.xt.hb.common.InfoModel;
	import com.xt.hb.utils.AisUtils;
	import com.xt.hb.utils.CommonUtils;
	import com.xt.hb.utils.ConfigUtil;
	import com.xt.hb.utils.ConstUtil;
	import com.xt.hb.utils.MapUtils;
	import com.xt.hb.utils.RemoteUtils;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.FlexGlobals;
	import mx.rpc.events.AbstractEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class AisMgr extends ShipMgr
	{
		private var cbUpdateTimer:Timer;
		private static var  instance:AisMgr;
		private static var showAis:Boolean;
//		private static var  currLevel;
		/** 船舶闪烁定时器 */
		public function AisMgr(singletonEnforcer:SingletonEnforcer)
		{
			cbUpdateTimer = new Timer(MapUtils.aisRefreshTime);
			cbUpdateTimer.addEventListener(TimerEvent.TIMER ,function(event:TimerEvent=null):void
			{
				if(showAis){
					instance.refreshAis();
				}
			});
		}
		/**
		 * 获取单例服务
		 */
		public static function getInstance():AisMgr {
			if(instance == null){
				instance = new AisMgr(new SingletonEnforcer());
			}
			return instance;
		}
//		/** 当前选中的船舶 */
//		public var selected:Graphic;
		/**
		 * 停止显示ais
		 */
		public function stopAis():void{
			showAis=false;
			var hscbLayer:GraphicsLayer = MapUtils.getHscbLayer();
			hscbLayer.clear();
			cbUpdateTimer.stop();
		}
		/**
		 * 根据当前地图加载海事Ais数据
		 */
		public function refreshAis():void{
			showAis = true;
			cbUpdateTimer.stop();
			var extend:Extent = MapUtils.map.extent;
			if(ConfigUtil.getMapLayerState(ConstUtil.L_ID_AIS) == ConstUtil.CHECK_STATE_1) {
				RemoteUtils.callRemote("aisCtl","queryAis",queryAisCallback,JSONUtil.encode(extend));
			}
		}
		/**
		 * 海事Ais查询结果处理
		 */
		private function queryAisCallback(r:AbstractEvent):void
		{
			if(r is FaultEvent){
				var fault:	FaultEvent =r as FaultEvent;
			}else {
				if(ConfigUtil.getMapLayerState(ConstUtil.L_ID_AIS) == ConstUtil.CHECK_STATE_0) {
					return;
				}
				var hscbLayer:GraphicsLayer = MapUtils.getHscbLayer();
				var resEvent:	ResultEvent =r as ResultEvent;
				var hscbInfoArr:Array = resEvent.result as Array;
				if(null != hscbInfoArr && hscbInfoArr.length>0){
					hscbLayer.clear();
				}
				if(null !=selected && currSelType == "ais"){
					MapUtils.clearAISGraphic();
				}
				var map:Map = FlexGlobals.topLevelApplication.getMap();
				// 获取地图级别大小
				var currScale:Number =  map.scale;
				var pointFlg:Boolean = false;
				if(currScale>70000) {
					pointFlg = true;
				}
				if(null != hscbInfoArr && hscbInfoArr.length>0){
					MapUtils.setCbSize(map.scale);
					for(var i:int=0;i<hscbInfoArr.length;i++)
					{
					 	var ship:Object =hscbInfoArr[i];
						var lon:Number=Number(ship.lon);
						var lat:Number=Number(ship.lat);
						var course:Number=Number(ship.course);
						var length:Number=Number(ship.length);
						var width:Number=Number(ship.width);
						var speed:Number=Number(ship.speed);
						var allShiptype:String = ship.ship_type;
						var shiptype:String = allShiptype.charAt(0);
						// 根据daner属性判断船的颜色
						var danger:String = ship.danger;
						var cbGraphic:Graphic = null;
						if(pointFlg) {
							cbGraphic = new Graphic(new MapPoint(lon,lat),
								new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE,
									2,0x00FF00,1,0,0,0,new SimpleLineSymbol("solid",0x00FF00,1,1) ));
							hscbLayer.add(cbGraphic);
						} else {
							// 颜色
							//var color:Number = AisUtils.getColorByShipType(shiptype);
							var color:Number = AisUtils.getColorByDanger(danger);
							cbGraphic = new Graphic();
							cbGraphic.geometry =new  HscbGeometry(lon,lat,course,length,width,speed);
							cbGraphic.symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,color,1,new SimpleLineSymbol("solid",0xFFC400,1,0.5) );
							cbGraphic.attributes = ship;
							cbGraphic.useHandCursor=true;
							cbGraphic.buttonMode = true;
							cbGraphic.mouseChildren=false;
							cbGraphic.addEventListener(MouseEvent.CLICK,hscbClick);
							cbGraphic.addEventListener(MouseEvent.MOUSE_OVER,hscbMouseOver);
							cbGraphic.addEventListener(MouseEvent.MOUSE_OUT,hscbMouseOut);
							if(null !=selected && currSelType == "ais"){
								if(selected.attributes.mmsi && selected.attributes.mmsi == ship.mmsi){
									HscbGeometry(cbGraphic.geometry).selected();
								}
							}
							hscbLayer.add(cbGraphic);
							// 增加文字描述
							//构建标签  
//							var textSymbol:RemarkSymbol = new RemarkSymbol(ship.name, 0);
//							var mygeometry:Geometry=new MapPoint(lon,lat);
//							var grahic2:Graphic=new Graphic(mygeometry,textSymbol);  
//							//添加标签  
//							hscbLayer.add(grahic2);
						}
					}
				}
			}
			cbUpdateTimer.start();
		}
		/**
		 * 海事船舶移入事件
		 */ 
		private function hscbMouseOver(e:MouseEvent):void
		{
			var g:Graphic=e.currentTarget as Graphic;
			var model:InfoModel = new InfoModel();
			var infoArr: Array = [
				//"船名 ： " + g.attributes.name,
				"IMO ： " + g.attributes.imo,
				"MMSI ： " + g.attributes.mmsi,
				"呼号 ： " + g.attributes.callsign,
				"最后时间： " + CommonUtils.formatPatternDate(
					new Date(Number(g.attributes.time)*1000),
					CommonUtils.DATE_MIN_FMT)
			];
			RemoteUtils.callRemote("aisCtl","getShipName",function(ae:AbstractEvent):void{
				var nameArr : Array = [];
				if(ae is FaultEvent){
					nameArr.push("船名 ： " + g.attributes.name);
				}else {
					var shipName:String = ResultEvent(ae).result as String;
					nameArr.push("船名 ： " + shipName);
				}
				model.showInfoWindow(nameArr.concat(infoArr), e);
			},g.attributes.name);
		}
		/**
		 * 海事船舶移出事件
		 */ 
		private function hscbMouseOut(e:MouseEvent):void
		{
			var g:Graphic=e.currentTarget as Graphic;
			MapUtils.map.infoWindow.hide();
		}
		/**
		 * 海事船舶点击事件
		 */ 
		private function hscbClick(e:MouseEvent):void
		{
			currSelType = "ais";
			var g:Graphic=e.currentTarget as Graphic;
			selected = g;
			HscbGeometry(g.geometry).selected();
			openPopWin(g);
		}
		//显示弹窗
		private var pop:ShipViewPanel = new ShipViewPanel();
		/**
		 * 打开船舶窗口
		 */
		private function openPopWin(cbgraphic:Graphic):void{
			RemoteUtils.callRemote("aisCtl","getShipName",function(ae:AbstractEvent):void{
				var nameArr : Array = [];
				if(ae is FaultEvent){
					pop.ship = cbgraphic.attributes;
				}else {
					var shipName:String = ResultEvent(ae).result as String;
					var shipObj:Object = cbgraphic.attributes;
					shipObj.name = shipName;
					pop.ship = shipObj;
				}
				if(!MapUtils.getShipPopWin().contains(pop)){
					MapUtils.getShipPopWin().addChild(pop);
				}
			},cbgraphic.attributes.name);
		}
	}
}
class SingletonEnforcer {}