package com.xt.hb.ais
{
	import com.esri.ags.Graphic;
	import com.esri.ags.SpatialReference;
	import com.esri.ags.geometry.Geometry;
	import com.esri.ags.geometry.MapPoint;
	import com.esri.ags.geometry.Polygon;
	import com.esri.ags.geometry.Polyline;
	import com.esri.ags.layers.GraphicsLayer;
	import com.esri.ags.symbols.SimpleLineSymbol;
	import com.xt.hb.utils.MapUtils;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.effects.Glow;

	/**
	 * 船舶
	 */
	public class HscbGeometry extends Polygon
	{
		private var _lon:Number;
		private	var _lat:Number;
		public function HscbGeometry(lon:Number,lat:Number,course:Number,length:Number,width:Number,speed:Number)
		{
			super(null, new SpatialReference(4326));
			setCbxx(lon,lat,course,length,width,speed);
		}
		/**
		 * 获取海事船舶图层
		 */
		private function getTempGraphicLayer():GraphicsLayer{
			var tempGLayer:GraphicsLayer = MapUtils.map.getLayer("L_ID_TEMPG") as GraphicsLayer;
			if(null == tempGLayer){
				tempGLayer = new GraphicsLayer();
				tempGLayer.id="L_ID_TEMPG";
				tempGLayer.name="L_NAME_TEMPG";
				MapUtils.map.addLayer(tempGLayer);
			}
			return tempGLayer;
		}
		public function selected():void{
			//暂把跟踪框放于临时图层	
			var trackG:Graphic = new Graphic();
			trackG.geometry = drawtracking();
			trackG.symbol = new SimpleLineSymbol("solid",0xF7621A,1,6);
			trackG.name="AISG";
			var tempG:GraphicsLayer = 	getTempGraphicLayer();
			// 清除标记
			MapUtils.clearAISGraphic();
			tempG.add(trackG);
			var glow:Glow =createGlow(trackG);
			glow.play();
			var glowTimer:Timer = new Timer(600);
			glowTimer.addEventListener(TimerEvent.TIMER,function(event:TimerEvent):void{
				glow.play();
			},false, 0, true);
			glowTimer.start();
		}
		//跟踪闪烁效果
		private function createGlow(fea:Graphic):Glow
		{
			var glow:Glow = new Glow(fea);
			glow.color = 0xFDE67B;//0x0000ff;
			glow.blurXFrom = 0;
			glow.blurXTo = 100;
			glow.blurYFrom = 0;
			glow.blurYTo = 100;
			glow.duration = 600;
			return glow;
		} 
		//标绘船舶追踪
		private function drawtracking():Geometry
		{
			var geo:Polyline=new Polyline();
			
			var c_x:Number=0.000700;//依据船舶形状画跟踪外框
			var c_y:Number=0.000700;//依据船舶形状画跟踪外框
			var d:Number=0.000400;//线段长度
			//左上角
			var X_1_1:Number = lon - c_x; var X_1_2:Number = X_1_1 + d;
			var Y_1_1:Number = lat + c_y; var Y_1_2:Number = Y_1_1 - d;
			geo.addPath([new MapPoint(X_1_1,Y_1_2),new MapPoint(X_1_1,Y_1_1),new MapPoint(X_1_2,Y_1_1)]);
			//右上角
			var X_2_1:Number = lon + c_x; var X_2_2:Number = X_2_1 - d;
			var Y_2_1:Number = lat + c_y; var Y_2_2:Number = Y_2_1 - d;
			geo.addPath([new MapPoint(X_2_1,Y_2_2),new MapPoint(X_2_1,Y_2_1),new MapPoint(X_2_2,Y_2_1)]);
			//右下角
			var X_3_1:Number = lon + c_x; var X_3_2:Number = X_3_1 - d;
			var Y_3_1:Number = lat - c_y; var Y_3_2:Number = Y_3_1 + d;
			geo.addPath([new MapPoint(X_3_1,Y_3_2),new MapPoint(X_3_1,Y_3_1),new MapPoint(X_3_2,Y_3_1)]);
			//左下角
			var X_4_1:Number = lon - c_x; var X_4_2:Number = X_4_1 + d;
			var Y_4_1:Number = lat - c_y; var Y_4_2:Number = Y_4_1 + d;
			geo.addPath([new MapPoint(X_4_1,Y_4_2),new MapPoint(X_4_1,Y_4_1),new MapPoint(X_4_2,Y_4_1)]);
			
			return geo;
		}
		/**
		 * 设置船舶信息
		 */
		public function setCbxx(lon:Number,lat:Number,course:Number,length:Number,width:Number,speed:Number):void{
			this._lon = lon;
			this._lat=lat;
			//先以中心点为原点
			//后期考虑转换异常，异常方向取0度
			course = -(course/180) * Math.PI; //弧度   顺时针旋转
			var c_x:Number=MapUtils.c_x;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
			//if(w>1)
			//	c_x=c_x*w/100;		
			var c_y:Number=MapUtils.c_y;//船舶单位长度，按照长、宽系数同比例缩放,100米长度
			var _o_x:Number=0;//船舶顶点
			var _o_y:Number=c_y*2;
			var v_o_x:Number=lon+_o_x*Math.cos(course)-_o_y*Math.sin(course);
			var v_o_y:Number=lat+_o_x*Math.sin(course)+_o_y*Math.cos(course);
			var _1_x:Number=c_x;//船舶角点1
			var _1_y:Number=c_y;
			var v_1_x:Number=lon+_1_x*Math.cos(course)-_1_y*Math.sin(course);
			var v_1_y:Number=lat+_1_x*Math.sin(course)+_1_y*Math.cos(course);
			var _2_x:Number=c_x;//船舶角点2
			var _2_y:Number=-c_y;
			var v_2_x:Number=lon+_2_x*Math.cos(course)-_2_y*Math.sin(course);
			var v_2_y:Number=lat+_2_x*Math.sin(course)+_2_y*Math.cos(course);
			var _3_x:Number=-c_x;//船舶角点3
			var _3_y:Number=-c_y;
			var v_3_x:Number=lon+_3_x*Math.cos(course)-_3_y*Math.sin(course);
			var v_3_y:Number=lat+_3_x*Math.sin(course)+_3_y*Math.cos(course);
			var _4_x:Number=-c_x;//船舶角点4
			var _4_y:Number=c_y;
			var v_4_x:Number=lon+_4_x*Math.cos(course)-_4_y*Math.sin(course);
			var v_4_y:Number=lat+_4_x*Math.sin(course)+_4_y*Math.cos(course);	
			//求一个点围绕任一个非原点的中心点	旋转
			this.addRing([new MapPoint(v_o_x,v_o_y),new MapPoint(v_1_x,v_1_y),new MapPoint(v_2_x,v_2_y),new MapPoint(v_3_x,v_3_y),new MapPoint(v_4_x,v_4_y),new MapPoint(v_o_x,v_o_y)]);
			//船首线与船速成正比 
			if(speed>0)
			{
				var c_j:Number=MapUtils.c_j;
				c_j=c_j*speed/5+MapUtils.c_j_1;
				var v_j_x:Number=lon+0*Math.cos(course)-c_j*Math.sin(course);
				var v_j_y:Number=lat+0*Math.sin(course)+c_j*Math.cos(course);
				this.addRing([new MapPoint(v_o_x,v_o_y),new MapPoint(v_j_x,v_j_y),new MapPoint(v_o_x,v_o_y)]);
			}
		}
		
		public function get lat():Number
		{
			return _lat;
		}
		
		public function get lon():Number
		{
			return _lon;
		}

	}
}