
/*
	Global UserInfo
*/

window.UserInfo = {
	/*
		积分
	*/
	shareLimit : 3,		//分享次数限制
	shareAddGold : 10,	//分享增加金币数
	platform :  CC_WECHATGAME ? 1 : 0,		//1为微信小游戏模式
	shareUrl : 'https://pub.dwstatic.com/wxgame/budingGame/share/shareImage2.jpg',//分享图片地址
	shareDesc : '超级可爱的布丁，快来跟我一起玩吧',
	shareDescChanllenge : '顽强的布丁',
	wxversion : '6.6.3',
	localversion : '1.0.1',
    ewmurl: 'https://pub.dwstatic.com/wxgame/escape/share/2weima.jpg',//公众号二维码

	GameCount : 2,
	getItem (key) {
		if(CC_QQPLAY) {
			return BK.localStorage.getItem(key)
		} else {
			return cc.sys.localStorage.getItem(key)
		}
	},

	setItem (key, value) {
		if(value) {
			if(CC_QQPLAY) {
				BK.localStorage.setItem(key, value.toString())
			} else {
				cc.sys.localStorage.setItem(key, value.toString())
			}
		}
	},

	// 获取每周排行榜分数
	getScore: function () {
		let score = this.getItem('score');
		return score ? parseInt(score) : 0;
	},

	setScore: function (value) {
		this.setItem('score', "" + value);
	},

	getCurrLocalversion: function () {
		if (this.IsEmpty(this.localversion))
			return "";
		return this.localversion;
	},

	setLocalversion: function (version) {
		this.setItem("localversion", "" + version);
	},

	getLocalversion: function () {
		let version = this.getItem('localversion');
		if (this.IsEmpty(version)) {
			this.setItem('localversion', '');
			version = '';
		}
		return version;
	},

	addScore: function (value) {
		let score = this.getItem(this.getCurWeekScoreValue());
		let inputScore = (score ? parseInt(score) : 0) + parseInt(value) + "";
		this.setItem(this.getCurWeekScoreValue(), inputScore);
	},

	/*
		金币
	*/
	getGold: function () {
		let gold = this.getItem('gold');
		return gold ? parseInt(gold) : 0;
	},

	setGold: function (value) {
		this.setItem('gold', "" + value);
	},

	addGold: function (value) {
		let gold = this.getItem('gold');
		let inputGold = (gold ? parseInt(gold) : 0) + parseInt(value) + "";
		this.setItem('gold', inputGold);

		this.addTotalGold(value);
	},

	addTotalGold: function (value) {
		let goldTotal = this.getItem('goldTotal');
		let inputGold = (goldTotal ? parseInt(goldTotal) : 0) + parseInt(value) + "";
		this.setItem('goldTotal', inputGold);
	},

	getTotalGold: function () {
		let goldTotal = this.getItem('goldTotal');
		return goldTotal ? parseInt(goldTotal) : 0;
	},

	addStageProp: function(value) {//累计道具数
		let propcnt = this.getItem('propcnt');
		let inputCnt = (propcnt ? parseInt(propcnt) : 0) + parseInt(value) + "";
		this.setItem('propcnt', inputCnt);
	},

	setStageProp: function(value) {//累计道具数		
		let inputCnt = parseInt(value) + "";
		this.setItem('propcnt', inputCnt);
	},

	getStageProp: function () {
		let propcnt = this.getItem('propcnt');
		return propcnt ? parseInt(propcnt) : 0;
	},

	getCurrentLevel: function () {
		let level = this.getItem('levelSign');
		if (this.IsEmpty(level)) {
			this.setItem('levelSign', '0');
			level = '0';
		}
		return parseInt(level);
	},

	setCurrentLevel: function (value) {
		this.setItem('levelSign', value + "");
	},

	setFlyCnt: function(cnt) {
		this.setItem('flycnt', cnt + "");
	},

	getFlyCnt: function() {
		let level = this.getItem('flycnt');
		if (this.IsEmpty(level)) {
			this.setItem('flycnt', '0');
			level = '0';
		}
		return parseInt(level);
	},

	setSkinIndex: function(skinIndex){
		this.setItem('skinIndex',skinIndex + "");
	},

	getSkinIndex: function()
	{
		let skinIndex = this.getItem('skinIndex');
		if (this.IsEmpty(skinIndex)) 
		{
			this.setItem('skinIndex','0');
			skinIndex = '0';
		}
		return parseInt(skinIndex);
	},

	/*
		当前等级
	*/
	getCurrentPlane: function () {
		let level = this.getItem('level');
		if (this.IsEmpty(level)) {
			this.setItem('level', '1');
			level = '1';
		}

		var intLevel = parseInt(level);
		if (intLevel <= 0) {
			intLevel = 1;
		}
		return intLevel;
	},

	setCurrentPlane: function (value) {
		this.setItem('level', value + "");
	},


	getLevelStage: function () {
		let level = this.getItem('levelStage');
		if (this.IsEmpty(level)) {
			this.setItem('levelStage', '');
			level = '';
		}

		return level;
	},

	setLevelStage: function (value) {
		this.setItem('levelStage', value + "");
	},

	/*
		设置自定义数据
	*/
	setString: function (str, value) {
		this.setItem(str, "" + value);
	},

	getString: function (str) {
		return this.getItem(str);
	},

	addString: function (str, value) {
		let data = this.getItem(str);
		let inputData = (data ? parseInt(data) : 0) + parseInt(value) + "";
		this.setItem(str, inputData);
	},

	removeString: function (str) {
		cc.sys.localStorage.removeItem(str);
	},

	IsEmpty(obj) {//判断是否为空
        if (obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
	},

	getMaxTime() {//历史最长时间
		var score = this.getString('maxtime');
		if (this.IsEmpty(score)) {
			this.setItem('maxtime', "0");
			score = "0";
		}
		return parseInt(score);
	},

	setMaxTime: function (value) {
		this.setItem('maxtime', value+"");
	},

	getHistoryScore() {//历史最高分
		var score = this.getString('historyScore');
		if (this.IsEmpty(score)) {
			this.setItem('historyScore', "0");
			score = "0";
		}
		return parseInt(score);
	},
	
	// 获取用户保存在QQ轻游戏云端的数据
	getUserGameData(callback) {
		if(CC_QQPLAY) {
			BK.QQ.loadGameData(function(errCode, cmd, data) {
				// BK.UI.showAlert({
				// 	title: 'loadGameData',
				// 	content: JSON.stringify(data)
				// })

				callback && callback(data)
			});
		} else {
			callback && callback({
				maxScore: 0
			})
		}
	},

	// 设置用户在QQ轻游戏的云端数据
	setUserGameData(score) {
		var data = {
			maxScore: score
		}
		
		if(CC_QQPLAY) {
			// 保存个人数据
			BK.QQ.saveGameData(data, function(errCode, cmd, data) {
				// BK.UI.showAlert({
				// 	title: 'saveGameData',
				// 	content: JSON.stringify(data)
				// })
			});
		}
	},

	getCurWeekScoreValue() {
		return this.getNowFormatDate();
	},

	getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		// var month = date.getMonth() + 1;
		// if (month >= 1 && month <= 9) {
		// 	month = "0" + month;
		// }
		var week = this.Getweek();
		var currentdate = year + seperator1 + week;
		return currentdate;
	},

	getMonthWeek() {
		var date = new Date(),
			w = date.getDay(),
			d = date.getDate();
		return Math.ceil(
			(d + 7 - w) / 7
		);
	},

	Getweek(dateString) {
		var da = '';
		if (dateString == undefined) {
			var now = new Date();
			var now_m = now.getMonth() + 1;
			now_m = (now_m < 10) ? '0' + now_m : now_m;
			var now_d = now.getDate();
			now_d = (now_d < 10) ? '0' + now_d : now_d;
			da = now.getFullYear() + '-' + now_m + '-' + now_d;
		} else {
			da = dateString; //日期格式2015-12-30
		}
		var date1 = new Date(da.substring(0, 4), parseInt(da.substring(5, 7)) - 1, da.substring(8, 10)); //当前日期
		var date2 = new Date(da.substring(0, 4), 0, 1); //1月1号
		//获取1月1号星期（以周一为第一天，0周一~6周日）
		var dateWeekNum = date2.getDay() - 1;
		if (dateWeekNum < 0) {
			dateWeekNum = 6;
		}
		if (dateWeekNum < 4) {
			//前移日期
			date2.setDate(date2.getDate() - dateWeekNum);
		} else {
			//后移日期
			date2.setDate(date2.getDate() + 7 - dateWeekNum);
		}
		var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
		var year = date1.getFullYear();
		var week = Math.ceil((d + 1) / 7);
		return week;
	},

	showIndexBanner() {
		this.indexBanner = BK.Advertisement.createBannerAd({
            viewId:1001,
            style: {x: 0, y: 0}
		});

		this.indexBanner.show()
	},

	destroyIndexBanner() {
		this.indexBanner.destory()
	},

	showDeadBanner() {
		this.deadBanner = BK.Advertisement.createBannerAd({
            viewId:1002,
            style: {x: 0, y: 0}
		});

		this.deadBanner.show()
	},

	destroyDeadBanner() {
		this.deadBanner.destory()
	},
};
