/**
 * 游戏主逻辑
 */

var game = cc.Class({
	extends: cc.Component,

	properties: {
		Item: {
			default: null,
			type: cc.Node,
			displayName: "预制体"
		},

		contentArr: {
			default: null,
			type: cc.Node,
			displayName: "Layer"
		},

		dragonLion: {
			default: null,
			type: dragonBones.ArmatureDisplay,
			displayName: "狮子"
		},

		dragonCat: {
			default: null,
			type: dragonBones.ArmatureDisplay,
			displayName: "猫"
		}
	},

	ctor: function () {
		this._timeoutIds = [];
		this._intervalIds = [];
		game.inst = this;
		LWD_3.game = game;
	},

	// 生命周期 onLoad
	onLoad() {
		//初始化游戏
		this.initGame();

		//监听尺寸变化
		if (window.addEventListener) {
			window.addEventListener('resize', this.scaleEventCallBack, false)
		} else if (window.attachEvent) {
			window.attachEvent('resize', this.scaleEventCallBack, false)
		}
	},

	//屏幕缩放
	scaleEventCallBack: function () {
		LWD_3.event_mgr.send("adjustUI");
	},

	//初始化游戏
	initGame: function () {
		//获得数据
		LWD_3.res_mgr.getFormData();
	},

	setAABB() {
		var node = this.dragonLion.node;

		let svLeftBottomPoint = node.parent.convertToWorldSpaceAR(
			cc.v2(
				node.x - node.anchorX * node.width / 2,
				node.y - node.anchorY * node.height / 2
			)
		);

		// 求出 ScrollView 可视区域在世界坐标系中的矩形（碰撞盒）
		let svBBoxRect = cc.rect(
			svLeftBottomPoint.x - 300,
			svLeftBottomPoint.y - 500,
			node.width,
			node.height
		);
		// console.log(Id + ":" + svLeftBottomPoint.x + "," + svLeftBottomPoint.y);
		return svBBoxRect
	},

	//检查当前缩放倍数
	checkScale: function (num) {
		var scale = 1;
		if (num > 2 && num <= 4) {
			scale = 0.74
		}
		if (num > 4) {
			scale = 0.65
		}
		return scale;
	},

	//添加节点
	addItem: function () {

		let itemArr = LWD_3.data_mgr.getQuestionOneList(LWD_3.data_mgr.quesId);

		itemArr.sort(function () { return Math.random() > 0.5 ? -1 : 1; })
		LWD_3.data_mgr.nowNum = 0;

		for (var i = 0; i < itemArr.length; i++) {
			let newItem = cc.instantiate(this.Item);
			//更新子项
			var com = newItem.getChildByName("item").getComponent("item");
			let itemInfo = itemArr[i];
			com.updateUI(itemInfo);
			newItem.active = true;
			if (itemInfo.is_right == true) {
				LWD_3.data_mgr.nowNum += 1;
			}

			newItem.getChildByName("item").is_right = itemInfo.is_right;

			newItem.parent = this.contentArr;
		};
	},

	//五秒没操作
	hintPlay: function () {
		// this.playLionDragon("openmouth")
		this.playCatDragon("begin")

		LWD_3.speaker.inst.playEffect(LWD_3.enum.E_Audio.CatHint);
		for (var i = 0; i < this.contentArr.childrenCount; i++) {
			cc.tween(this.contentArr.children[i].children[0])
				.to(0.1, { x: -50 })
				.to(0.1, { x: 0 })
				.to(0.1, { x: 50 })
				.to(0.1, { x: 0 })
				.start();
		}
	},

	//隐藏剩余物品并跳转下一题
	resetQues: function () {
		if (LWD_3.data_mgr.quesId >= 4) {
			this.finish();
			LWD_3.data_mgr.gameState = 2
		} else {
			for (var i = 0; i < this.contentArr.childrenCount; i++) {
				cc.tween(this.contentArr.children[i].children[0])
					.to(0.8, { opacity: 0 })
					.start();
			}

			this._timeoutIds.push(setTimeout(() => {
				this.UpdataUi();
			}, 1000));
		}
	},

	//结束
	finish() {
		this.playLionDragon("normal")
		this.playCatDragon("finish")
		this._timeoutIds.push(setTimeout(() => {
			LWD_3.speaker.inst.playEffect(LWD_3.enum.E_Audio.CatComplete);
		}, 500));
	},

	//检测放到哪个节点底下
	checkNodeParent: function () {
		//获取到总列表
		var itemArr = LWD_3.data_mgr.getSheepArr();

		//
		if (itemArr.length <= 7) {
			var idx = 1;
		} else {
			var idx = 0;
		}
		return idx;
	},

	//检查类别
	checkType: function (Id) {
		for (var i in LWD_3.data_mgr.getSheepfoldArr()) {
			var sheepfoldInfo = LWD_3.data_mgr.getSheepfoldArr()[i];
			if (Id == sheepfoldInfo.id) {
				return ~~i + 1;
			}
		}
		return -1;
	},

	//更新界面信息
	UpdataUi: function () {
		LWD_3.data_mgr.gameState == 1;
		var Info = {
			tex_json: LWD_3.data_mgr.data.tex_json,
			ske_json: LWD_3.data_mgr.data.ske_json,
			tex_png: LWD_3.data_mgr.data.tex_png,
		}
		LWD_3.res_mgr.loadSpine(this.dragonLion, Info);
		//刚进入课件
		//播放第一题题干

		//设置俩个人物状态
		this.playCatDragon("begin");
		this.playLionDragon("normal");

		//播放题干
		this.playAudioTitle();

		// LWD_3.speaker.inst.playEffect(E_Audio.BtnCommom);

		//重置UI界面
		this.resetUI();
		//添加项
		this.addItem();

		// //设置上下页按钮状态
		// this.setButtonState();
	},

	//播放猫动画
	playCatDragon(name) {
		this.dragonCat.armatureName = "Armature";
		this.dragonCat.playAnimation(name);
	},

	//播放猫动画
	playLionDragon(name) {
		this.dragonLion.armatureName = "armatureName";
		this.dragonLion.playAnimation(name);
	},

	//播放题干
	playAudioTitle: function () {
		if (LWD_3.data_mgr.quesId >= 4) return
		//获得播放路径
		var path = LWD_3.data_mgr.getQuestionStem(LWD_3.data_mgr.quesId);
		LWD_3.res_mgr.playAudioByUrl(path, (url) => {
			LWD_3.snd_mgr.playEffect(url, () => {
				//结束回调
				this.playCatDragon("normal");
			});
		});
	},

	//重置UI界面
	resetUI: function () {
		//移除所有子节点
		this.contentArr.removeAllChildren();
	},

	//重新开始
	onBtnReStart: function () {
		LWD_3.speaker.inst.play_btn();
		//移除所有计时器
		this.unscheduleAllCallbacks();
		//初始化界面
		this.UpdataUi();
		LWD_3.speaker.inst.play_restart();
	},

	//游戏开始
	gameStart: function () {
		console.log("游戏开始:" + LWD_3.data_mgr);
		//播放一个上面的音乐
		this.setAudioInfo(1);
	},

	//设置上下页按钮状态
	setButtonState: function () {
		//先判断题目长度
		if (LWD_3.data_mgr.data.contentObj.pageArr.length < 2) {
			this.btnList[0].node.active = false;
			this.btnList[1].node.active = false;
		} else {
			//如果第一页
			if (LWD_3.data_mgr.pageId == 0) {
				this.btnList[1].node.active = true;
				this.btnList[0].node.active = false;
			}
			if (LWD_3.data_mgr.pageId == LWD_3.data_mgr.data.contentObj.pageArr.length - 1) {
				this.btnList[0].node.active = true;
				this.btnList[1].node.active = false;
			}
			if (LWD_3.data_mgr.pageId > 0 && LWD_3.data_mgr.pageId < LWD_3.data_mgr.data.contentObj.pageArr.length - 1) {
				this.btnList[0].node.active = true;
				this.btnList[1].node.active = true;
			}
		}
	},

	//下一关
	onBtnNextQues: function () {
		LWD_3.data_mgr.time = 0;
		LWD_3.speaker.inst.playEffect(LWD_3.enum.E_Audio.BtnCommom);

		LWD_3.data_mgr.quesId += 1;
		LWD_3.game.inst.resetQues();
	},

	//重新开始
	onBtnReStart() {
		LWD_3.data_mgr.time = 0;
		LWD_3.data_mgr.resetQuestion();

		LWD_3.speaker.inst.playEffect(LWD_3.enum.E_Audio.BtnCommom);
		LWD_3.data_mgr.quesId = 0;
		this.UpdataUi();
	},

	//上一关
	onBtnLastPage: function () {
		LWD_3.speaker.inst.play_btn();

		if (LWD_3.data_mgr.pageId - 1 >= 0) {
			LWD_3.data_mgr.pageId -= 1;
			this.setButtonState();
			this.onBtnReStart();
		}
	},

	//下一关
	onBtnNextPage: function () {
		LWD_3.speaker.inst.play_btn();

		if (LWD_3.data_mgr.pageId + 1 < LWD_3.data_mgr.data.contentObj.pageArr.length) {
			LWD_3.data_mgr.pageId += 1;
			this.setButtonState();
			this.onBtnReStart();
		}
	},
	_timeoutIds: null,
	_intervalIds: null,
	// 生命周期
	onDestroy() {
		this._timeoutIds.forEach(id => {
			clearTimeout(id);
		});
		this._intervalIds.forEach(id => {
			clearInterval(id);
		});
		LWD_3.game.inst._intervalIds.forEach(id => {
			clearInterval(id);
		});
		LWD_3.data_mgr.quesId = 0;
	},
});
