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.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.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 com.xt.hb.utils.ServicesUtility;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.rpc.events.AbstractEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class GpsMgr extends ShipMgr
	{
		private var gpsUpdateTimer:Timer;
		private static var  instance:GpsMgr;
		private static var showAis:Boolean;
		private static var gpsArr:ArrayCollection;
//		private static var  currLevel;
		/** 船舶闪烁定时器 */
		public function GpsMgr(singletonEnforcer:SingletonEnforcer)
		{
			//gpsUpdateTimer = new Timer(MapUtils.gpsRefreshTime*10000);
			//gpsUpdateTimer.addEventListener(TimerEvent.TIMER ,function(event:TimerEvent=null):void
			//{
				//instance.refreshGps();
			//});
		}
		/**
		 * 获取单例服务
		 */
		public static function getInstance():GpsMgr {
			if(instance == null){
				instance = new GpsMgr(new SingletonEnforcer());
			}
			return instance;
		}
//		/** 当前选中的船舶 */
//		private var selected:Graphic;
		/**
		 * 停止显示Gps
		 */
		public function stopGps():void{
			var hscbLayer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_GPS);
			hscbLayer.clear();
			gpsArr = null;
			//gpsUpdateTimer.stop();
		}
		/**
		 * 根据当前地图加载海事Gps数据
		 */
		public function refreshGps():void{
			//gpsUpdateTimer.stop();
			if(null == gpsArr) {
				var extend:Extent = MapUtils.map.extent;
				RemoteUtils.callRemote("gpsCtl","queryGpsRealtime",queryGpsCallback);
			} else {
				dealHscbData(gpsArr);
			}
		}
		/**
		 * 海事Gps查询结果处理
		 */
		private function queryGpsCallback(e:AbstractEvent):void
		{
			if(e is FaultEvent){
				trace("error");
			}else {
				if(ConfigUtil.getMapLayerState(ConstUtil.L_ID_GPS) == ConstUtil.CHECK_STATE_0) {
					return;
				}
				gpsArr = ResultEvent(e).result as ArrayCollection;
				dealHscbData(gpsArr);
			}
		}
		private function dealHscbData(arr:ArrayCollection):void {
			var hscbLayer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_GPS);
			if(null != arr && arr.length>0){
				hscbLayer.clear();
			}
			var map:Map = FlexGlobals.topLevelApplication.getMap();
			// 获取地图级别大小
			var currScale:Number =  map.scale;
			var pointFlg:Boolean = false;
			if(currScale>70000) {
				pointFlg = true;
			}
			if(null != arr && arr.length>0){
				trace(arr.length);
				MapUtils.setCbSize(map.scale);
				for(var i:int=0;i<arr.length;i++)
				{
					var ship:Object =arr[i];
					var lon:Number=Number(ship.jd);
					var lat:Number=Number(ship.wd);
					if(lon == 0 || lat == 0) {
						continue;
					}
					var course:Number=0;
					var length:Number=100;
					var width:Number=20;
					var speed:Number=Number(ship.sd);
					var cbGraphic:Graphic = null;
					if(pointFlg) {
						cbGraphic = new Graphic(new MapPoint(lon,lat),
							new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE,
								2,0x0000FF,1,0,0,0,new SimpleLineSymbol("solid",0x0000FF,1,1) ));
					} else {
						// 颜色
						var color:Number = 0x0000FF;
						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){
							if(selected.attributes.mmsi && selected.attributes.mmsi == ship.mmsi){
								HscbGeometry(cbGraphic.geometry).selected();
							}
						}
					}
					hscbLayer.add(cbGraphic);
				}
			}
		}
		/**
		 * 海事船舶移入事件
		 */ 
		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)
			];
			model.showInfoWindow(infoArr, e);
		}
		/**
		 * 海事船舶移出事件
		 */ 
		private function hscbMouseOut(e:MouseEvent):void
		{
			var g:Graphic=e.currentTarget as Graphic;
			MapUtils.map.infoWindow.hide();
		}
		/**
		 * 海事船舶点击事件
		 */ 
		private function hscbClick(e:MouseEvent):void
		{
			currSelType = "gps";
			var g:Graphic=e.currentTarget as Graphic;
			selected = g;
			HscbGeometry(g.geometry).selected();
			//openPopWin(g);
			// 弹出信息
			ServicesUtility.showFeatrueToMap(
				ConstUtil.L_ID_GPS,g,g.attributes,e);
		}
		//显示弹窗
		private var pop:ShipViewPanel = new ShipViewPanel();
		/**
		 * 打开船舶窗口
		 */
		private function openPopWin(cbgraphic:Graphic):void{
			pop.ship = cbgraphic.attributes ;
			if(!MapUtils.getShipPopWin().contains(pop)){
				MapUtils.getShipPopWin().addChild(pop);
			}
		}
	}
}
class SingletonEnforcer {}