/// <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 keyQuery = null;
		public volumeType = 1;
		public totlePage = 1;	

		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,
				keyQuery: this.keyQuery,
				volumeType: this.volumeType
			}

			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);
		    console.log(this.totalSize);
		    if(num < this.currentPageNum)		    	
			  num =  this.currentPageNum;			
			return num;
		}
	}
	function createLabel(cityName:string, districtName:string, systemName:string, version:string, year:string){
		return{
			cityName: cityName,
			districtName: districtName,
			systemName: systemName,
			version: "版本"+version,
			year: year
		}
	}
	function createAlias(cityName:string,  districtName:string, systemName:string, version:string, year:string){
		return  cityName + "_" + districtName + "_" + systemName + "_" + year + "_版本" + 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 createItemsAlias(items: Array<any>){
		var result = [];
		angular.forEach(items, (item) => {
			item.alias = createAlias(item.cityName, item.districtName, item.systemName, item.dataVersion, item.year);
			result.push(item);
		});
		return result;
	}

	function populateLabel(item){
		var result = item
		result["labels"] = createLabel(item.cityName, item.districtName, item.systemName, item.dataVersion, item.year);
		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();
	}

	function formatTask(items:Array<any>){
		var result = [];
		angular.forEach(items, (item) => {
			var tmp_batch="";
			if(item.submittedBatch.indexOf("批次")==-1){
				tmp_batch="批次"+item.submittedBatch;
			}else{
				tmp_batch=item.submittedBatch;
			}
			item["_key"] = item.regionalismCode + "-" + item.systemCode + "-" + item.dataVersion;
			item["name"] = item.cityName+ "-" + item.districtName;
			item["status"] = item.completeStatus;
			item["from"] = item.dataPath;
			item["process"] = item.rate;
			item["to"]  = item.dstPath;
			item["labels"] ={
				dataType: item.dataType,
				batch: tmp_batch,					
				dataVersion: "版本"+item.dataVersion,
				dataYear: item.year
			}
			result.push(item);
		});
		return result;
	}

	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;	
					paramOptions.totlePage = data.page.totlePage;								
				} 
			});			
			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);
			this.data = createItemsAlias(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 = formatTask(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;
 	}]);
}