/**
 * 游戏主逻辑
 */
var game = cc.Class({
	extends: cc.Component,

	properties: {
		Item_0: {
			default: [],
			type: cc.Node,
			displayName: "预制体"
		},

		contentArr_2: {
			default: [],
			type: cc.Node,
			displayName: "底下内容2"
		},

		contentArr_1: {
			default: [],
			type: cc.Node,
			displayName: "目标位置"
		},

		lb_title: {
			default: null,
			type: cc.Label,
			displayName: "大标题"
		},

		//先分有几种类型
		contType: {
			default: [],
			type: cc.Node,
			displayName: "底下内容2"
		}
	},

	ctor: function () {
		game.inst = this;
		g.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 () {
		g.event_mgr.send("adjustUI");
	},

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

	setAABB(Id) {
		var node = this.contentArr_1[Id]

		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 - 120,
			svLeftBottomPoint.y - 100,
			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 = g.data_mgr.getSheepArr();
		this.idx = this.checkNodeParent();
		g.data_mgr.nowNum = itemArr.length;

		itemArr.sort(function () { return Math.random() > 0.5 ? -1 : 1; })


		for (var i = 0; i < itemArr.length; i++) {
			let newItem_0 = cc.instantiate(this.Item_0[itemArr.length > 14 ? 0 : 1]);
			//更新子项
			var com = newItem_0.getChildByName("item").getComponent("item");
			let itemInfo = itemArr[i];
			com.updateUI(itemInfo);
			newItem_0.active = true;
			//设置类别
			let sheepInfo = g.data_mgr.getSheep(i);
			newItem_0.getChildByName("item").type = this.checkType(sheepInfo.sheepfoldId);


			newItem_0.parent = this.contentArr_2[this.idx];
		};
	},

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

		//
		if (itemArr.length <= 7) {
			this.contentArr_2[1].active = true;
			var idx = 1;
		} else {
			this.contentArr_2[0].active = true;
			var idx = 0;
		}
		return idx;
	},

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

	//设置目标节点类型
	setOptionType: function () {
		//设置类型
		this.OptionType = g.data_mgr.getSheepfoldArr().length - 3;
	},

	getOptionType: function () {
		return this.OptionType;
	},

	//更新界面信息
	UpdataUi: function () {
		//设置目标节点类型
		this.setOptionType();
		//设置大标题
		this.lb_title.string = g.data_mgr.getTitle();
		//设置类型
		this.contType[this.OptionType].active = true;

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

	//播放标题音效
	playAudioTitle: function () {
		//获得播放路径
		var path = g.data_mgr.data.audio_url;
		g.res_mgr.playAudioByUrl(path, (url) => {
			g.snd_mgr.playEffect(url, null);
		});
	},

	//重置UI界面
	resetUI: function () {
		//移除所有子节点
		this.contentArr_2[0].removeAllChildren();
		this.contentArr_2[1].removeAllChildren();
		//移除所有子节点
		g.event_mgr.send("reSetGame");
	},

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

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


	checkAnswer(caller, option_i) {
		if (caller == undefined) {
			return;
		}

		if (option_i != -1) {
			let nodOption = this.contType[this.OptionType].children[option_i - 1]
			if (nodOption) {

				let AABB = nodOption.getComponent("DragGameOptionObject").setAABB();
				let callerAABB = this.getCallerAABB(caller);
				var isIntersect = AABB.containsRect(callerAABB);//判断是否被包含
				console.log(isIntersect)
				if (isIntersect) {
					return nodOption;
				}
				return false
			}
		}
	},

	getCallerAABB(caller) {
		let svLeftBottomPoint = caller.parent.convertToWorldSpaceAR(
			cc.v2(
				caller.x,
				caller.y
			)
		);
		var posNode_1 = cc.rect(
			svLeftBottomPoint.x,
			svLeftBottomPoint.y,
			0,
			0
		);
		return posNode_1
	},
});
