package com.xt.hb.tdt
{
	import com.esri.ags.SpatialReference;
	import com.esri.ags.geometry.Extent;
	import com.esri.ags.geometry.MapPoint;
	import com.esri.ags.layers.TiledMapServiceLayer;
	import com.esri.ags.layers.supportClasses.LOD;
	import com.esri.ags.layers.supportClasses.TileInfo;
	
	import flash.net.URLRequest;
	
	//import mx.controls.Alert;
	
	public class TianDiTuTiledMapServiceLayer extends TiledMapServiceLayer
	{
		private var _tileInfo:TileInfo;
		private var _baseURL:String;
		private var _baseURLs:String;
		private var _initExtent:String;
		private var _id:String;
		private var _name:String;
		private var _alpha:Number;
		private var _visible:Boolean;
		private var _showLevel:String;
		
		public function TianDiTuTiledMapServiceLayer()
		{
			super();
			this._tileInfo = new TileInfo();
			this._baseURLs = "";
			this._initExtent = null;
			this.buildTileInfo();
			setLoaded(true);
		}
		
		
		public function set baseURL(baseurl:String):void
		{
			_baseURL = baseurl;
		}
		public function set baseURLs(baseurls:String):void
		{
			_baseURLs = baseurls;
		}
		
		public function set showLevel(showlevel:String):void
		{
			_showLevel=showlevel;
		}
		override public function get fullExtent() : Extent
		{
			return new Extent(-180, -90, 180, 90, new SpatialReference(4490));
		}
		
		public function set initExtent(initextent:String):void
		{
			this._initExtent = initextent;
		}
		override public function get spatialReference() : SpatialReference
		{
			return new SpatialReference(4490);
		}
		override public function get initialExtent() :Extent
		{
			if (this._initExtent == null){
				return new Extent(-180, -90, 180, 90, new SpatialReference(4490));    
			}
			var coors:Array = this._initExtent.split(",");
			return new Extent(Number(coors[0]), Number(coors[1]), Number(coors[2]) ,Number(coors[3]), new SpatialReference(4490));
		}
		override public function get tileInfo() : TileInfo
		{
			return this._tileInfo;
		}
		private function buildTileInfo() : void
		{
			this._tileInfo.height = 256;
			this._tileInfo.width = 256;
			this._tileInfo.origin = new MapPoint(-180, 90);
			this._tileInfo.spatialReference = new SpatialReference(4490);
			this._tileInfo.lods = new Array();
			this._tileInfo.lods = [
				new LOD(0, 0.7031250000000002, 295497593.05875003),
				new LOD(1, 0.3515625000000001, 147748796.52937502),
				new LOD(2, 0.17578125000000006, 73874398.26468751),
				new LOD(3, 0.08789062500000003, 36937199.132343754),
				new LOD(4, 0.043945312500000014, 18468599.566171877),
				new LOD(5, 0.021972656250000007, 9234299.783085939),
				new LOD(6, 0.010986328125000003, 4617149.891542969),
				new LOD(7, 0.005493164062500002, 2308574.9457714846),
				new LOD(8, 0.002746582031250001, 1154287.4728857423),
				new LOD(9, 0.0013732910156250004, 577143.7364428712),
				new LOD(10, 0.0006866455078125002, 288571.8682214356),
				new LOD(11, 0.0003433227539062501, 144285.9341107178),
				new LOD(12, 0.00017166137695312505, 72142.9670553589),
				new LOD(13, 8.583068847656253e-005, 36071.48352767945),
				new LOD(14, 4.2915344238281264e-005, 18035.741763839724),
				new LOD(15, 2.1457672119140632e-005, 9017.870881919862), 
				new LOD(16, 1.0728836059570316e-005, 4508.935440959931),				
				new LOD(17, 5.364418029785158e-006, 2254.4677204799655),
				new LOD(18, 2.682209014892579e-006, 1127.2338602399827),
				new LOD(19, 1.3411045074462895e-006, 563.6169301199914)
			];
		}
		override protected function getTileURL(level:Number, row:Number, col:Number) : URLRequest
		{
			var tempLevel:Number=Number(_showLevel);
			var urlRequest:String="";
			if(level<tempLevel)
			{
				var rd:Number = Math.ceil(Math.random() * 7);
				
				urlRequest = "http://t" + rd + ".tianditu.com/"+_baseURLs+"_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER="+ _baseURLs+"&STYLE=default&TILEMATRIXSET=c&TILEMATRIX="+ String((level + 1)) + "&TILEROW=" + String(row) + "&TILECOL=" + String(col) + "&FORMAT=tiles" ;
			}
			return new URLRequest(urlRequest);
			
		}
	}
	
}