import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.Map;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.symbols.PictureMarkerSymbol;
import com.xt.hb.utils.ConstUtil;
import com.xt.hb.utils.GisUtils;
import com.xt.hb.utils.MapUtils;
import com.xt.hb.utils.RemoteUtils;
import com.xt.hb.utils.ServicesUtility;
import com.xt.hb.utils.GlobalData;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.navigateToURL;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.Tree;
import mx.rpc.events.AbstractEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

// 港口现状树
[Bindable]
private var areaArr:ArrayCollection = new ArrayCollection();
//视频区域树
[Bindable]
private var areaVideoArr:ArrayCollection;
[Bindable]
private var gkarray:ArrayCollection=new ArrayCollection();

private function changeTreeNodeState(event:Event):void
{
	var tree:Tree = event.currentTarget as Tree;
	var item:Object = tree.selectedItem;
	
	if(tree.dataDescriptor.isBranch(item)){
		tree.expandItem(item,!tree.isItemOpen(item),true);
	}
	
}
//快速定位  
private function initLocationTree(isWh:Boolean):void
{
	getGkgqqyTree(isWh);
}
private function getGkgqqyTree(isWh:Boolean):ArrayCollection
{
	var flg:String = "false";
	if(isWh) {
		flg = "true";
	}
	RemoteUtils.callRemote("aisCtl","queryGkgqqyTree",function(e:AbstractEvent):void{
		if(e is FaultEvent){
			Alert.show("ERROR Call aisCtl queryGkgqqyTree");
		}else {
			var resultObj:Object = ResultEvent(e).result as Object;
			areaArr = dealGkgqjyrResult(resultObj);
			locationTree.validateNow(); //验证并更新此对象的属性和布局，并重绘该对象（如果需要）。 
			//locationTree.selectedIndex = 0;
			// 展开单项
			locationTree.expandItem(locationTree.dataProvider[0],true);
			//全部展开 
			//locationTree.expandChildrenOf(locationTree.selectedItem,true);
			//locationTree.callLater(function ():void{locationTree.expandChildrenOf(locationTree.selectedItem,true);});
			//locationTree.selectedIndex = 0;
			//locationTree.expandChildrenOf(locationTree.selectedItem,true);
		}
	}, flg);
	var ret:ArrayCollection = new ArrayCollection();
	return ret ;
}

private function dealGkgqjyrResult(resultObj:Object):ArrayCollection {
	var ret:ArrayCollection = new ArrayCollection();
	for each(var obj:Object in resultObj) {
		if(null != obj.children && obj.children.length > 0) {
			dealGkgqjyrResult(obj.children);
		} else {
			obj.children = null;
		}
	}
//	for each(var obj:Object in resultObj) {
//		if(null != obj.children && obj.children.length > 0) {
//			for each(var obj1:Object in obj.children) {
//				if(null != obj1.children && obj1.children.length > 0) {
//					for each(var obj2:Object in obj1.children) {
//						obj2.children = null;
//						obj2.type = "JYR";
//					}
//				} else {
//					obj1.children = null;
//				}
//				obj1.type = "GQ";
//			}
//		}
//		obj.type = "GK";
//		//ret.addItem(obj);
//	}
	ret = resultObj as ArrayCollection;
	return ret ;
}

public function locationAction(item:Object):void
{
	var map:Map = FlexGlobals.topLevelApplication.getMap();
	var gisid:String = item.attr;
	var type:String = item.type;
	if(typeof(item.attr) == "object") {
		//Alert.show("定位失败, 未关联GIS信息!");
		return;
	}
	trace(type + gisid);
	switch(type)
	{
		case "1":
			GisUtils.callGisServer(GlobalData.gkLayerUrl,onGkAreaResult,"ID="+gisid);
			break;
		case "2":
			GisUtils.callGisServer(GlobalData.gqLayerUrl,onGqAreaResult,"ID="+gisid);
			break;
		case "3":
			GisUtils.callGisServer(GlobalData.jyrLayerUrl,onJyrCentResult,"ID="+gisid);
			break;
	}
	
}

/**
 * 港口查询结果
 */
private function onGkAreaResult(featureSet:FeatureSet,token:Object = null):void
{
	var layer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_GK);
	layer.clear();
	var map:Map = FlexGlobals.topLevelApplication.getMap();
	for each(var graphic:Graphic in featureSet.features)
	{
		// 定位
		var centerPoint:MapPoint = graphic.geometry.extent.center;
		map.level = 10;
		map.centerAt(centerPoint);
		// 加图层
		graphic.symbol = MapUtils.getOwnerSymbolStyle(0x0000FF,0.3,0xFFFF00,5);
		layer.add(graphic);
	}
}

/**
 * 港口查询结果
 */
private function onGqAreaResult(featureSet:FeatureSet,token:Object = null):void
{
	var layer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_GQ);
	var map:Map = FlexGlobals.topLevelApplication.getMap();
	for each(var graphic:Graphic in featureSet.features)
	{
		// 定位
		var centerPoint:MapPoint = graphic.geometry.extent.center;
		map.level = 12;
		map.centerAt(centerPoint);
		// 加图层
		graphic.useHandCursor=true;
		graphic.buttonMode = true;
		graphic.symbol = MapUtils.getOwnerSymbolStyle(0x0000FF,0.2,0x7030A0,2);
		graphic.addEventListener(MouseEvent.CLICK,gqMouseClick);
		graphic.addEventListener(MouseEvent.MOUSE_OVER, MapUtils.graphicMouseOver);
		graphic.addEventListener(MouseEvent.MOUSE_OUT, gqMouseOut);
		//layer.add(graphic);
		// 触发弹出事件，显示港区信息
		showGqInfo(graphic);
	}
}

/**
 * 海事图形移出事件
 */ 
private function gqMouseOut(e:MouseEvent):void
{
	var g:Graphic=e.currentTarget as Graphic;
	g.symbol = MapUtils.getOwnerSymbolStyle(0x0000FF, 0.3, 0x7030A0, 3);
}

/**
 * 港区图形点击事件
 */ 
public function gqMouseClick(e:MouseEvent):void
{
	var g:Graphic=e.currentTarget as Graphic;
	// 显示港区信息
	showGqInfo(g);
}

/**
 * 显示港区信息
 */ 
public function showGqInfo(g:Graphic):void
{
	var obj:Object = g.attributes;
	// 远程调用
	RemoteUtils.callRemote("aisCtl","querySingleGq",function(ae:AbstractEvent):void{
		if(ae is FaultEvent){
			Alert.show("ERROR Call aisCtl querySingleGq");
		}else {
			var resultObj:Object = ResultEvent(ae).result as Object;
			// 弹出信息
			ServicesUtility.showFeatrueToMap(
				ConstUtil.L_ID_GQ,g,resultObj);
		}
	},obj.ID);
}

/**
 * 经营人数据双击事件
 */
private function onJyrCentResult(featureSet:FeatureSet,token:Object = null):void
{
	var map:Map = FlexGlobals.topLevelApplication.getMap();
	// 获取图层
	var jyrLayer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_JYR);
	var wxhwLayer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_AQ_WXHWQY);
	var zdwxyLayer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_AQ_ZDWXYQY);
	var graphic:Graphic;
	if(featureSet.features.length > 0) {
		graphic = featureSet.features[0];
		var type:int = graphic.attributes.TYPE;
		var tempLayer:GraphicsLayer = null;
		tempLayer = jyrLayer;
		//   >=8 重大危险源    >=5 危货企业     >=0 普通企业
		var imgName:String = "";
		if(type >= 8) {
			tempLayer = zdwxyLayer;
			imgName = "aq_zdwxy.gif";
		} else if(type >= 5) {
			tempLayer = wxhwLayer;
			imgName = "aq_wxhwqy.png";
		} else {
			tempLayer = jyrLayer;
			imgName = "aq_jyr.png";
		}
		// 定位
		var centerPoint:MapPoint = graphic.geometry.extent.center;
		map.level = 15;
		map.centerAt(centerPoint);
		// 加图层
		var cent:Graphic = new Graphic();
		cent.geometry = centerPoint;
		var symbol:PictureMarkerSymbol = new PictureMarkerSymbol("flexResources/png/"+imgName, 25, 25);
		cent.useHandCursor=true;
		cent.buttonMode = true;
		cent.symbol = symbol;
		cent.attributes = graphic.attributes;
		cent.addEventListener(MouseEvent.CLICK,jyrCentMouseClick);
		cent.addEventListener(MouseEvent.DOUBLE_CLICK, function(e:MouseEvent):void{
			var graphic:Graphic=e.currentTarget as Graphic;
			// 定位
			var mp:MapPoint = new MapPoint(graphic.geometry["x"],graphic.geometry["y"],null);
			map.level = 16;
			map.centerAt(mp);
		});
		tempLayer.add(cent);
		// 显示经营人
		showJyrInfo(graphic);
	}
}

/**
 * 经营人图形点击事件
 */ 
public function jyrCentMouseClick(e:MouseEvent):void
{
	var g:Graphic=e.currentTarget as Graphic;
	// 显示经营人
	showJyrInfo(g);
}

/**
 * 显示经营人
 */ 
public function showJyrInfo(g:Graphic):void
{
	var obj:Object = g.attributes;
	trace(obj.ID);
	// 远程调用
	RemoteUtils.callRemote("aisCtl","querySingleJyr",function(ae:AbstractEvent):void{
		if(ae is FaultEvent){
			Alert.show("ERROR Call aisCtl querySingleJyr");
		}else {
			var resultObj:Object = ResultEvent(ae).result as Object;
			// 弹出信息
			ServicesUtility.showFeatrueToMap(
				ConstUtil.L_ID_JYR,g,resultObj)
		}
	},obj.ID);
}

/**
 * 初始化视频树列表
 */
private function initSpjkTree():void
{
//	areaVideoArr = new ArrayCollection([
//		{name:"主控制中心",id:"all" ,fid:"f1" ,CDTB:"" ,STATE:"1" ,CDID:"",
//			children:[
//				{name:"武汉市港航海事局",id:"1547" ,fid:"all" ,CDTB:"" ,STATE:"1" ,CDID:"WH"},
//				{name:"宜昌市港航海事局",id:"1400" ,fid:"all" ,CDTB:"" ,STATE:"1" ,CDID:"YC"}]
//		}
//	]);
	RemoteUtils.callRemote("aisCtl","querySpTree",function(e:AbstractEvent):void{
		if(e is FaultEvent){
			Alert.show("ERROR Call aisCtl querySpTree");
		}else {
			var resultObj:Object = ResultEvent(e).result as Object;
			areaVideoArr = dealSpResult(resultObj);
			//验证并更新此对象的属性和布局，并重绘该对象（如果需要）。 
			spjkTree.validateNow();
			//spjkTree.selectedIndex = 0;
			// 展开单项
			spjkTree.expandItem(spjkTree.dataProvider[0],true);
		}
	});
}

private function dealSpResult(resultObj:Object):ArrayCollection {
	var iconUrl:String = "flexResources/map/sxt.png";
	var ret:ArrayCollection = new ArrayCollection();
	for(var i:int=resultObj.length-1;i>=0;i--) {
		var obj:Object = resultObj[i];
		if("13" == obj.id || "36" == obj.id) {
			resultObj.removeItemAt(i);
			continue;
		}
		setChildren(obj);
	}
	ret = resultObj as ArrayCollection;
	return ret ;
}
/**
 * 递归实现树的url设置
 */
private function setChildren(obj:Object):void {
	var iconUrl:String = "flexResources/map/sxt.png";
	if(null != obj.children && obj.children.length > 0) {
		obj.iconUrl = null;
		for each(var obj1:Object in obj.children) {
			setChildren(obj1);
		}
	} else {
		obj.children = null;
		if(obj.attr == "0") {
			obj.iconUrl = null;
		} else if(obj.attr == "1"){
			obj.iconUrl = iconUrl;
		} else {
			obj.iconUrl = null;
		}
	}
}

/**
 * 视频监控双击动作时间
 */
public function spjkAction(item:Object):void
{
	var id:String = item.id;
	if("1" == item.attr) {
		GisUtils.callGisServer(GlobalData.spLayerUrl,onSpResult,"id="+id);
	}
	
}

//查询成功活进行高亮显示已经数据Grid显示
private function onSpResult(featureSet:FeatureSet,token:Object = null):void
{
	var map:Map = FlexGlobals.topLevelApplication.getMap();
	var layer:GraphicsLayer = MapUtils.getGraphicLayerByName(ConstUtil.L_ID_SP);
	var resultlist:Array=new Array();
	var graphic:Graphic;
	if(featureSet.features.length > 0) {
		// 定位
		graphic = featureSet.features[0];
		var mp:MapPoint = new MapPoint(graphic.geometry["x"],graphic.geometry["y"],null);
		map.level = 15;
		map.centerAt(mp);
		// 显示坐标点，并添加点击事件
		var symbol:PictureMarkerSymbol = new PictureMarkerSymbol("flexResources/png/aq_sxt.png", 14, 22);
		graphic.symbol = symbol;
		graphic.useHandCursor=true;
		graphic.buttonMode = true;
		graphic.addEventListener(MouseEvent.CLICK,spMouseClick);
		layer.add(graphic);
	}
}

/**
 * 海事视频点击事件
 */ 
private function spMouseClick(e:MouseEvent):void
{
	var obj:Object = e.currentTarget.attributes;
	var url:String = GlobalData.spjkUrl + obj.ID;
	navigateToURL(new URLRequest(url),"_blank");
}


/**
 * 视频监控双击动作时间
 */
public function spjkIconAction(item:Object):void
{
	trace("ok");
	var id:String = item.id;
	trace(id);
	
}



