import { DCGame, Option } from "../../DataCenter/DCGame";
import { ItemAnswer } from "../../Game/ItemAnswer";
import { E_AudioName, Speaker } from "../../Game/Speaker";
import AssetMgr from "../Module/AssetMgr/AssetMgr";
import EventMgr from "../Module/EventMgr/EventMgr";

//拖拽节点
const { ccclass, property } = cc._decorator;

@ccclass
export class DragGameAnswerObject extends cc.Component {
	onLoad(): void {
		// super.onLoad();
		//注册事件
		this.regEvent();

		this.oldPos = this.node.getPosition();
	}

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

	private regEvent() {
		this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegan, this);
		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
		this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
		this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
	}

	private unEvent() {
	}

	private m_dragGameManager//管理器
	private m_nAnswerIdx//当前答案的id
	private m_nDeltaPos//移动距离
	private m_bMove;//是否移动

	private dmOption: Option;//当前选项的数据模型

	private oldPos;//原来的节点坐标

	//设置Item数据
	setData(value: Option) {
		this.dmOption = value;

		this.m_dragGameManager = null;
		this.m_nAnswerIdx = this.dmOption.option_Idx;
	}

	private tw1//缓动1

	touchBegan(event) {
		this.m_bMove = false;
		this.m_nDeltaPos = this.node.x + this.node.y;
		if (DCGame.getInstance().questionIdx == 0) {
			let answer_1 = this.node.parent.getChildByName("answer_1")
			this.tw1 = cc.tween(answer_1)
				.to(0.2, { y: 200 })
				.start();
		}
	}

	touchMove(event) {
		var delta = event.getDelta();
		this.node.x += delta.x;
		this.node.y += delta.y;

		var tempDeltaPos = this.node.x + this.node.y;
		if (Math.abs(this.m_nDeltaPos - tempDeltaPos) > 10) {
			this.m_bMove = true;
		}
	}

	touchEnd() {
		//点击
		if (!this.m_bMove) {
			console.log("点击");
			if (DCGame.getInstance().questionIdx == 1) {
				//第二轮点击图片播放声音
				let path = this.dmOption.option_audio_url
				AssetMgr.playAudioByUrl(path);
				this.node.setPosition(this.oldPos);
			} else {
				EventMgr.emit("checkAnswer", this.node, this.m_nAnswerIdx)
			}
		} else {
			console.log("移动");
			//向管理器发送结果  把当前id跟节点发送过去开始检验结果
			EventMgr.emit("checkAnswer", this.node, this.m_nAnswerIdx)
			// this.m_dragGameManager.checkAnswer(this.node, this.m_nAnswerIdx);
		}
	}

	touchCancel() {
		console.log("取消");
		this.node.setPosition(this.oldPos);
	}

	//答对了
	AnswerRight() {
		console.log("答对了");
		//答对播放一遍正确音效
		let path = this.dmOption.option_audio_url
		AssetMgr.playAudioByUrl(path)

		//开始做表现
		if (DCGame.getInstance().questionIdx == 0) {
			this.node.getComponent(ItemAnswer).AnswerRightEffect_1();
		} else {
			this.node.getComponent(ItemAnswer).AnswerRightEffect_2();
		}
	}

	//答错了
	AnswerError() {
		console.log("答错了");
		Speaker.playErrorEffect();
		if (DCGame.getInstance().questionIdx == 0) {
			let answer_1 = this.node.parent.getChildByName("answer_1")
			answer_1.stopAllActions();
			cc.tween(answer_1)
				.to(0.1, { y: -52.5 })
				.start();
		}
		this.node.setPosition(this.oldPos);
	}

	//设置回原来位置
	setOldPosition() {
		this.node.scale = 1;
		if (DCGame.getInstance().questionIdx == 0) {
			let answer_1 = this.node.parent.getChildByName("answer_1");
			answer_1.stopAllActions();
			this.node.parent.getChildByName("answer_1").y = -52;
		}
		this.node.setPosition(this.oldPos);
	}
}