// 服务
app.service('myService', function($rootScope, $http, $window, debug, Native){
	var self = this;
	/**
	 * 获取地理位置 并 初始化 天气信息
	 * */
	this.initLocation = function(){
		$rootScope.location = self.Cache.get('rootScope.location') || "未知";
		$rootScope.weatherInfo = self.Cache.get('rootScope.weatherInfo');
		// var ipResult = Native.Network.get("http://apis.map.qq.com/ws/location/v1/ip?key=3BFBZ-ZKD3X-LW54A-ZT76D-E7AHO-4RBD5&output=jsonp&callback=ipCallback", true);
		var ipResult = $http.jsonp("http://apis.map.qq.com/ws/location/v1/ip?key=3BFBZ-ZKD3X-LW54A-ZT76D-E7AHO-4RBD5&output=jsonp");
		var ipCallback = function(result){
			var json = result.data;
			//debug.log('ipCallback', json);
			if(json.status==0){
				// 地理位置赋值
				$rootScope.location = json.result.ad_info.province + " " + json.result.ad_info.city;// 地理位置
				self.Cache.set('rootScope.location', $rootScope.location);
				// var weatherResult = Native.Network.get("http://wis.qq.com/weather/common?source=pc&weather_type=observe|tips&province="+json.result.ad_info.province+"&city="+json.result.ad_info.city+"&county=&callback=weatherCallback", true);
				var weatherResult = $http.jsonp("http://wis.qq.com/weather/common?source=pc&weather_type=observe|tips&province="+json.result.ad_info.province+"&city="+json.result.ad_info.city+"&county=");
				weatherResult.then(weatherCallback);
				// eval(weatherResult.data);
			}else{
				debug.error('地理位置信息有误', json);
			}
		};
		var weatherCallback = function(result){
			if(result.status==200){		
				var json = result.data;
				// debug.log('weather', json);
				if(json.status==200){
					// 天气信息 赋值
					$rootScope.weatherInfo = json.data;
					self.Cache.set('rootScope.weatherInfo', $rootScope.weatherInfo);
					// debug.log($rootScope);
				}else{
					debug.error('天气信息有误', json);
				}
			}else{
				debug.error('加载天气信息失败');
			}
		};
		
		ipResult.then(ipCallback);
		//eval(ipResult.data);
	};
	/**
	 * 获取 日历信息，农历等
	 * */
	this.getCalendarData = function() {
		//获取当前系统日期
		var today = new Date()
		var y = today.getFullYear()
		var mo = today.getMonth()
		var da = today.getDate()
		var h = today.getHours()
		var m = today.getMinutes()
		var s = today.getSeconds()
		var weekString = "日一二三四五六"
		var TheDate = new Date();
		var CalendarData = new Array(20);
		var madd = new Array(12);
		var numString = "一二三四五六七八九十";
		var monString = "正二三四五六七八九十冬腊";
		var cYear;
		var cMonth;
		var cDay;
		var cHour;
		var cDateString;
		var DateString;
		var Browser = navigator.appName;
	
		function init() {
			CalendarData[0] = 0x41A95;
			CalendarData[1] = 0xD4A;
			CalendarData[2] = 0xDA5;
			CalendarData[3] = 0x20B55;
			CalendarData[4] = 0x56A;
			CalendarData[5] = 0x7155B;
			CalendarData[6] = 0x25D;
			CalendarData[7] = 0x92D;
			CalendarData[8] = 0x5192B;
			CalendarData[9] = 0xA95;
			CalendarData[10] = 0xB4A;
			CalendarData[11] = 0x416AA;
			CalendarData[12] = 0xAD5;
			CalendarData[13] = 0x90AB5;
			CalendarData[14] = 0x4BA;
			CalendarData[15] = 0xA5B;
			CalendarData[16] = 0x60A57;
			CalendarData[17] = 0x52B;
			CalendarData[18] = 0xA93;
			CalendarData[19] = 0x40E95;
			madd[0] = 0;
			madd[1] = 31;
			madd[2] = 59;
			madd[3] = 90;
			madd[4] = 120;
			madd[5] = 151;
			madd[6] = 181;
			madd[7] = 212;
			madd[8] = 243;
			madd[9] = 273;
			madd[10] = 304;
			madd[11] = 334;
		}
	
		function GetBit(m, n) {
			return(m >> n) & 1;
		}
	
		function e2c() {
			var total, m, n, k;
			var isEnd = false;
			var tmp = TheDate.getYear();
			if(tmp < 1900) tmp += 1900;
			total = (tmp - 2001) * 365 +
				Math.floor((tmp - 2001) / 4) +
				madd[TheDate.getMonth()] +
				TheDate.getDate() -
				23;
			if(TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1)
				total++;
			for(m = 0;; m++) {
				k = (CalendarData[m] < 0xfff) ? 11 : 12;
				for(n = k; n >= 0; n--) {
					if(total <= 29 + GetBit(CalendarData[m], n)) {
						isEnd = true;
						break;
					}
					total = total - 29 - GetBit(CalendarData[m], n);
				}
				if(isEnd) break;
			}
			cYear = 2001 + m;
			cMonth = k - n + 1;
			cDay = total;
			if(k == 12) {
				if(cMonth == Math.floor(CalendarData[m] / 0x10000) + 1)
					cMonth = 1 - cMonth;
				if(cMonth > Math.floor(CalendarData[m] / 0x10000) + 1)
					cMonth--;
			}
			cHour = Math.floor((TheDate.getHours() + 3) / 2);
		}
	
		function GetcDateString() {
			var tmp = "";
			if(cMonth < 1) {
				tmp += "闰";
				tmp += monString.charAt(-cMonth - 1);
			} else
				tmp += monString.charAt(cMonth - 1);
			tmp += "月";
			tmp += (cDay < 11) ? "初" : ((cDay < 20) ? "十" : ((cDay < 30) ? "廿" : "卅"));
			if(cDay % 10 != 0 || cDay == 10){				
				tmp += numString.charAt((cDay - 1) % 10);
			}
			tmp += " ";
			cDateString = tmp;
			cDateString = cDateString.replace('廿 ', '二十').replace('卅 ', '三十').replace(' ', '');
			return tmp;
		}
		init();
		e2c();
		GetcDateString();
		//s设置层txt的内容
		//小于10，加0
		function checkTime(i) {
			if(i < 10) {
				i = "0" + i
			}
			return i
		}
		//调用checkTime（）函数，小于十的数字前加0
		m = checkTime(m);
		s = checkTime(s);
		da = checkTime(da);
		return {
			y: y,// + "年" +
			m: (mo + 1),// + "月" + 
			d: da,// + "日 " + 
			h: h,// + ":" + 
			i: m,// + ":" + 
			s: s,// + 
			week: "星期" + weekString.charAt(today.getDay()),// + " 农历" + 
			cDateString: cDateString
		};
	};
	/**
	 * 缓存类
	 * */
	this.Cache = {
		get: function(key){
			var val = window.localStorage.getItem(key);
			return val ? JSON.parse(val) : null;
		},
		set: function(key, val){
			return window.localStorage.setItem(key, JSON.stringify(val));
		},
		remove: function(key){
			window.localStorage.removeItem(key);
		}
	};
	/**
	 * 右键菜单
	 * */
	this.showRightClickMenu = function(scope, event, btnArr, callback){
		//scope.$apply(function(){
			var x,y;
			x = (event.clientX+1);
			y = (event.clientY+1);
			
			scope.$rightScope = {
				show: true,
				list: btnArr,
				// event: event,
				style: {'top': y+'px', 'left': x+'px'},
				imageX: x > 1000,
				imageY: y > 670,
				clickMenuItemFn: callback
			};
			// 窗口绑定点击事件 隐藏右键菜单
	        angular.element($window).unbind('click');
			angular.element($window).bind('click', function(event) {
				scope.$apply(function() {
					event.preventDefault();
					scope.$rightScope = null;
					angular.element($window).unbind('click');
				});
			});
			// console.log(scope.$rightScope);
		//});
	};
});