//游戏主逻辑

import { DragGameAnswerObject } from "../Common/DragGame/DragGameAnswerObject";
import { DragGameOptionObject_001 } from "../Common/DragGame/DragGameOptionObject";
import AssetMgr from "../Common/Module/AssetMgr/AssetMgr";
import EventMgr from "../Common/Module/EventMgr/EventMgr";
import { UICtrl } from "../Common/UICtrl/UICtrl";
import { DCGame } from "../DataCenter/DCGame";
import { ItemAnswer } from "./ItemAnswer";

const { ccclass, property } = cc._decorator;

@ccclass
export class Game extends UICtrl {
	onLoad(): void {
		super.onLoad();

		//注册事件
		this.regEvent();

		//初始化组件节点
		this.initProperty()

		// //初始化游戏
		// this.initGame();
	}

	protected onEnable(): void {
		this.initGame();
	}

	protected onDestroy(): void {
		//注销事件
		this.unEvent();
	}

	private regEvent() {
		this.unEvent();
		EventMgr.reg("onRestart", this.onRestart.bind(this), 1)
		EventMgr.reg("nextAnswer", this.OptionMove.bind(this), 1)
	}

	private unEvent() {
		EventMgr.remove("onRestart")
		EventMgr.remove("nextAnswer")
	}

	//
	private nodQuestion: cc.Node;//问题节点
	private itemQuestion: cc.Node;//问题预制节点

	private dbCat: dragonBones.ArmatureDisplay;//猫咪龙骨
	private dbLeaf: dragonBones.ArmatureDisplay;//撒树叶特效
	private nodMask: cc.Node;//遮罩节点

	private nodAnswer1: cc.Node;
	private nodAnswer2: cc.Node;

	//重新开始效果
	private nodRestart: cc.Node;//重新开始游戏节点

	private m_nowQuestion = [];//当前的题目的目标节点
	private m_nowAnswer: cc.Node;//当前拖动选项

	initProperty() {
		this.nodQuestion = this.view["nodQuestion"];
		this.itemQuestion = this.view["itemQuestion"];
		this.dbCat = this.view["dbCat"].getComponent(dragonBones.ArmatureDisplay);
		this.dbLeaf = this.view["dbLeaf"].getComponent(dragonBones.ArmatureDisplay);
		this.nodMask = this.view["mask"];

		this.nodAnswer1 = this.view["nodAnswer/nodAnswer1"];
		this.nodAnswer2 = this.view["nodAnswer/nodAnswer2"];

		this.nodRestart = this.view["nodRestart"]
	}

	//初始化游戏
	initGame() {
		//初始化数据
		DCGame.getInstance().initData();
		this.guideGame();
	}

	//重新开始游戏
	onRestart() {
		//要求直接销毁
		let node = this.m_nowQuestion.shift();
		node.destroy();
		this.initGame();
	}

	//选项回收
	OptionMove() {
		let nodAnswer = DCGame.getInstance().questionIdx == 0 ? this.nodAnswer1 : this.nodAnswer2;
		this.m_nowAnswer = nodAnswer;
		cc.tween(this.m_nowAnswer)
			.to(0.5, { y: 250 })
			.call(() => {
				//顺带初始化
				this.onOptionFinish();
			})
			.start();
	}

	//选项出来
	OptionCome() {
		cc.tween(this.m_nowAnswer)
			.call(() => {
				this.m_nowAnswer.getChildByName("itemAnswer").getComponent(DragGameAnswerObject).setOldPosition();
			})
			.to(0.5, { y: 0 })
			.start();
	}

	//先引导游戏
	guideGame() {
		//播放引导开始音频
		let path = DCGame.getInstance().getAudioBegin();
		if (!path)
			return this.nodMask.active = false;
		this.playAudioByUrl(path, "begin", () => {
			this.nodMask.active = false;
			//开始第一题
			this.getNewQuestion();
			//回收选项时初始化
			this.OptionMove();
		})
	}

	//new一个新的题目
	getNewQuestion() {
		this.carMove();
		this.carCome();
	}

	//开始下一个选项
	onOptionFinish() {
		//获取下一个选项
		let dmOption = DCGame.getInstance().getOption();
		//判断是否还有选项
		if (!dmOption) {
			//判断是否游戏结束
			if (DCGame.getInstance().questionIdx + 1 >= 2) {
				//游戏结束
				this.onGameFinish();
			} else {
				//获取下一个大题
				this.getNextQuestion();
			}
		} else {
			//获得下一个选项
			this.getNextOption(dmOption);
		}
	}

	//游戏结束
	onGameFinish() {
		//获得当前大题结束语音
		let path = DCGame.getInstance().getQuestionEndAudioUrl();
		// //游戏结束 播放撒花特效
		// this.playLeaf();
		//同时播放结束音频
		this.playAudioByUrl(path, "begin", () => {
			this.nodRestart.active = true;
		});
		return;
	}

	//获取下一个大题
	getNextQuestion() {
		//获得当前大题结束语音
		let path = DCGame.getInstance().getQuestionEndAudioUrl();
		DCGame.getInstance().questionIdx += 1;
		//游戏未结束，开始下一大题
		this.playAudioByUrl(path, "finish", () => {
			//重新初始化大题选项数量
			DCGame.getInstance().setOptionArr();
			//获得一个新的大题
			this.getNewQuestion();
			//重新初始化头部选项
			this.OptionMove();
		})
	}

	//获得下一个选项
	getNextOption(dmOption) {
		//开始初始化选项
		let com = this.m_nowAnswer.getChildByName("itemAnswer");
		com.getComponent(ItemAnswer).setData(dmOption);
		com.active = true;

		//把当前正确的篮子节点保存起来
		DCGame.getInstance().objOption = this.getOption(dmOption.option_Idx);
		this.OptionCome();
	}

	//获得当前目标选项
	getOption(Idx: number) {
		return this.m_nowQuestion[0].getChildByName("nodBasket_" + Idx)
	}

	//车走了
	carMove() {
		let node = this.m_nowQuestion.shift()
		if (!node)
			return
		cc.tween(node)
			.to(1, { x: -1080 })
			.call(() => {
				node.destroy()
			})
			.start();
	}

	//车来了
	carCome() {
		//进入游戏
		this.nodMask.active = true;
		let Layer = cc.instantiate(this.itemQuestion);
		this.m_nowQuestion.push(Layer);
		this.initGroup();
		Layer.x = 1080;
		Layer.y = 0;
		Layer.parent = this.nodQuestion;
		Layer.active = true;
		//移动（0,0）点
		cc.tween(Layer)
			.to(1, { x: 0 })
			.call(() => {
				//车子进来后播放题干音频
				let path = DCGame.getInstance().getQuestionAudioUrl();
				this.playAudioByUrl(path)
				this.nodMask.active = false;
			})
			.start();
	}

	//初始化Group
	initGroup() {
		let grop1 = this.getOption(1).getComponent(DragGameOptionObject_001)
		let grop2 = this.getOption(2).getComponent(DragGameOptionObject_001)
		grop1.setData(DCGame.getInstance().getGroup1());
		grop2.setData(DCGame.getInstance().getGroup2());
	}

	//点击猫
	_tap_btnCheckCat() {
		if (DCGame.getInstance().questionIdx >= 2 || this.nodRestart.active == true) {
			//播放结束
			let path = DCGame.getInstance().getAudioReStart();
			if (!path) return;
			this.playAudioByUrl(path);
		} else {
			let path = DCGame.getInstance().getQuestionAudioUrl();
			if (!path) return;
			this.playAudioByUrl(path);
		}
	}

	//播放音频
	playAudioByUrl(path: string, name: string = "begin", cb?: Function) {
		this.playCatDragon("begin");
		cb && (this.nodMask.active = true);
		AssetMgr.playAudioByUrl(path, () => {
			this.nodMask.active = false;
			this.playCatDragon("normal");
			cb && cb();
		})
	}

	//猫咪龙骨
	playCatDragon(name: string) {
		this.dbCat.armatureName = "Armature";
		this.dbCat.playAnimation(name, -1);
	}

	//播放撒树叶特效
	playLeaf(cb?) {
		this.dbLeaf.node.active = true;
		this.dbLeaf.armatureName = "Armature";
		this.dbLeaf.playAnimation("normal", 1);
		cb && this.dbLeaf.addEventListener(dragonBones.EventObject.COMPLETE, cb);
	}
}


