// 原生服务
app.service('Native', function(debug){
	var self = this;
	var nativeName = 'Native'; // 原生约定名称
	// 调用原生方法 - 封装
	self.callNativeFun = function(fnClass, fnName, param){
		param = param || {};
		var result = window[nativeName] && window[nativeName]['$call'] && window[nativeName]['$call'](JSON.stringify({
			className: fnClass,
			fnName: fnName,
			param: JSON.stringify(param)
		}));
		try{
			return JSON.parse(result);
		}catch(e){
			//TODO handle the exception
			return {
				code: "1",
				msg: "错误结果",
				error: e,
				result: result,
				arguments: arguments
			};
		}
	};
	
	// 网络请求
	self.Network = {
		get: function(url, isUtf8){
			var result = self.callNativeFun('Network', 'request', {
				method: 'GET',
				url: url,
				data: "",
				isUtf8: isUtf8?"1":"0"
			});
			debug.log('Network->get', result);
			return result;
		},
		post: function(url, data, isUtf8){
			var result = self.callNativeFun('Network', 'request', {
				method: 'POST',
				url: url,
				data: data,
				isUtf8: isUtf8?"1":"0"
			});
			debug.log('Network->post', result);
			return result;
		}
	};
	
	// 窗口类
	self.Window = {
		// 初始化
		init: function(param){
			var defOpt = {
				title: 'Hello Desktop App',
				top: 'auto',
				left: 'auto',
				width: 800,
				height: 600
			};
			return self.callNativeFun("Window", "init", angular.extend(defOpt, param));
		},
		// 最小化
		min: function(){
			return self.callNativeFun("Window", "min");
		},
		// 隐藏
		hide: function(){
			return self.callNativeFun("Window", "hide");
		},
		// 关闭
		close: function(){
			return self.callNativeFun("Window", "close");
		},
		// 移动窗口
		move: function(movementX, movementY){
			return self.callNativeFun('Window', 'move', {
				movementX: movementX,
				movementY: movementY
			});
		}
	};
	
	// DB 数据库类
	self.DB = {
		// 取所有分组列表
		getMenuList: function(){
			var menuList = [];
			var result = self.callNativeFun('DB', 'getMenuList');
			if(result && result.data && result.data.menuList){
				menuList = result.data.menuList;
			}
			return menuList;
		},
		// 取所有菜单项
		getItemList: function(){
			var itemList = [];
			var result = self.callNativeFun('DB', 'getItemList');
			if(result && result.data && result.data.itemList){
				itemList = result.data.itemList;
			}
			return itemList;
		},
		// 添加分组
		addMenu: function(ID, className){
			return self.callNativeFun('DB', 'addMenu', { ID: ID, className: className });
		},
		// 修改分组 名字
		renameMenu: function(className, newName){
			return self.callNativeFun('DB', 'renameMenu', {
				className: className,
				newName: newName
			});
		},
		// 修改分组ID
		modifyMenuID: function(className, ID){
			return self.callNativeFun('DB', 'modifyMenuID', {
				className: className,
				ID: ID
			});
		},
		// 删除分组
		removeMenu: function(className, delIconStr){
			return self.callNativeFun('DB', 'removeMenu', { className: className, delIconStr: delIconStr });
		},
		// 添加菜单
		addItem: function(param){
			// icon、className
			return self.callNativeFun('DB', 'addItem', param);
		},
		// 修改项目
		modifyItem: function(oldName, item){
			return self.callNativeFun('DB', 'modifyItem', angular.extend({
				oldName: oldName
			}, item));
		},
		// 修改项目
		modifyItemMenu: function(name, className){
			return self.callNativeFun('DB', 'modifyItemMenu', {
				name: name,
				className: className
			});
		},
		// 删除项目
		removeItem: function(name, icon){
			return self.callNativeFun('DB', 'removeItem', { name: name, icon: icon });
		}
	};
	
	// 拖拽
	self.Drag = {
		// 注册回调函数， 把 call 放到 全局中。
		registerCallback: function(callback){
			callback = callback || angular.noop;
			window['_Native_Drag_Callback_'] = callback;
			return self.callNativeFun('Drag', 'registerCallback', {
				callback: '_Native_Drag_Callback_'
			});
		}
	};
	
	// 运行交互
	self.Runtime = {
		// 定位文件
		findFile: function(itemName){
			return self.callNativeFun('Runtime', 'findFile', {name: itemName});
		},
		// 运行项目
		runItem: function(item){
			return self.callNativeFun('Runtime', 'runItem', angular.copy(item));
		}
	};
	
});