/// <reference path="../../includes.ts"/>
/// <reference path="developerPlugin.ts"/>
module Developer{
	export class OptionsParams{
		public pagerSizeOption = [20,50,100];
		public dataType =null;
		public currentTableSize =20;
		public dataBatch =null;
		public labels={};
		public currentPageNum=1;		
		public totalSize=null;
		public priorTableSize = 20;			

		public createParamData(){
			var extendValue =["cityName", "districtName", "dataVersion", "systemName", "dataYear"];
			var result={
				currentPageNum: this.currentPageNum,
				dataType: this.dataType,
				submittedBatch: this.dataBatch,
				limit: this.currentTableSize,
				priorTableSize: this.priorTableSize
			}

			angular.forEach(this.labels,(value, key) =>{
				if(extendValue.indexOf(key))
					result[key] = value;
			});

			return result;
		}

		public getPageSizeNum(){			
		    var num = Math.ceil(this.totalSize/this.currentTableSize);	
		    if(num < this.currentPageNum)		    	
			  num =  this.currentPageNum;			
			return num;
		}
	}
	function createLabel(cityName:string, districtName:string, systemName:string, version:string){
		return{
			cityName: cityName,
			districtName: districtName,
			systemName: systemName,
			version: "版本"+version
		}
	}

	function createKey(regionalismCode, systemId, version){
		return regionalismCode + "-" + systemId + "-" + version;
	}

	function populateKey(item){
		var result = item;
		result["_key"] = createKey(item.regionalismCode,  item.systemCode, item.dataVersion);
		return result;
	}

	function populateLabel(item){
		var result = item
		result["labels"] = createLabel(item.cityName, item.districtName, item.systemName, item.dataVersion);
		return result;
	}

	function populateLabels(items:Array<any>){
		var result = [];
		angular.forEach(items, (item) => {			
			result.push(populateLabel(item));
		});
		return result;
	}

	function populateKeys(items:Array<any>){
		var result =[];
		angular.forEach(items, (item) =>{
			result.push(populateKey(item));
		});
		return result;
	}

	function createName(cityName, districtName){
		return cityName + "-" + districtName;
	}

	function populateName(item){
		var result = item;
		result["name"] = createName(item.cityName, item.districtName);
		return result;
	}

	function populateNames(items:Array<any>){
		var result = [];
		angular.forEach(items, (item) =>{			
			result.push(populateName(item));
		});
		return result;
	}

	function createParamData(options: OptionsParams){
		return options.createParamData();
	}

	export class DataModelService{
		public data:Array<any> = [];
		public paramOptions: OptionsParams = new OptionsParams();
		public transferTasks: Array<any> = [];

		constructor(){				
			this.updateModel();							
		}

		//更新数据模型
		protected getDataModel(paramOptions){
			var result ;
			$.ajax({ 
				async: false, 
				type : "POST", 
				url : "/java/console/api/data.json", 
				dataType : 'json',
				data: createParamData(paramOptions),		
				success : function(data) {				
					result = data.data;
					paramOptions.totalSize=data.length;									
				} 
			});
			return result;	
		}

		public initParamOptions(){
			this.paramOptions = new OptionsParams();
		}

		public updateModel(){
			this.data = this.getDataModel(this.paramOptions);
			this.maybeFormat();
		}

		//格式数据模型中的每个单条记录
		public maybeFormat(){
			this.data = populateKeys(this.data);
			this.data = populateNames(this.data);
			this.data  = populateLabels(this.data);
		}

		//更新用户选择参数
		public updateParamOption(option:string, value:any){
			this.paramOptions[option] = value;
		}	

		//根据key获取用户选择参数
		public getParamOption(key:string){
			return this.paramOptions[key];
		}

		public startIntervalTask($interval, $http){
			var result;
			var timer = $interval(() => {	 			
		 		$.ajax({
					async: false,
					type : "POST",
					url : "/java/console/api/task/transfer/list",
					success : function(data) { 
					    if(data){			
						    result = data;					   
						}												
					} 	
				});
				this.transferTasks = result;							
 		    },1500);

 		    timer.then(() =>{
 		    	console.log("Done!");
 		    }, () =>{
 		    	console.log("error");
 		    }, () =>{ 		    	
 		    	console.log("每次都更新"); 		    	
 		    });
		}		
	}

	//创建数据模型服务
 	_module.factory("DataModel", ['$rootScope', '$http', '$interval', '$location', '$resource', ($rootScope, $http, $interval, $location, $resource) => {
 		var $scope = new DataModelService();  
 		$scope.startIntervalTask($interval, $http);
 	    return $scope;
 	}]);
}