import { Group, Option } from "../../DataCenter/DCGame";
import { DCGame } from "./../../DataCenter/DCGame";
import AssetMgr from "../Module/AssetMgr/AssetMgr";
import EventMgr from "../Module/EventMgr/EventMgr";
import { UICtrl } from "../UICtrl/UICtrl";

const { ccclass, property } = cc._decorator;

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

		this.initProperty();

		this.updateUI();
	}
	private layQuestionText: cc.Node;
	private labText: cc.Node;
	private sptPhoto: cc.Sprite;
	private layQuestionSpt: cc.Node;
	private btnAudio: cc.Node;
	private labTitle: cc.Label;
	initProperty() {
		this.layQuestionText = this.view["layQuestionText"];
		this.labText = this.view["labText"]

		this.layQuestionSpt = this.view["layQuestionSpt"]
		this.sptPhoto = this.view["sptPhoto"].getComponent(cc.Sprite);

		this.btnAudio = this.view["btnAudio"];
		this.labTitle = this.view["labTitle"].getComponent(cc.Label);
	}

	private dmGroup: Group

	setData(value: Group) {
		this.dmGroup = value;
		this.updateUI();
	}

	updateUI() {
		if (!this.view) return;
		this.btnAudio.active = DCGame.getInstance().questionIdx == 0 ? true : false;
		this.labTitle.node.active = DCGame.getInstance().questionIdx == 1 ? true : false;
		//设置标题
		this.labTitle.string = this.dmGroup.group_text;
	}

	setAABB() {
		var node = this.node

		let svLeftBottomPoint = node.parent.convertToWorldSpaceAR(
			cc.v2(
				node.x - node.width / 2,
				node.y - node.height / 2,
			)
		);
		console.log("世界坐标目标点" + svLeftBottomPoint);


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

	Effect_1(labText, dmOption: Option) {
		this.AddText1(labText)
		AssetMgr.getSpriteFrimeByUrl(dmOption.option_pic_url, (spriteFrame) => {
			this.sptPhoto.spriteFrame = spriteFrame
			let sptPic = cc.instantiate(this.sptPhoto.node);
			sptPic.active = true;
			this.AddSpt(sptPic);
		})
	}

	//添加一个文字
	AddText1(labText) {
		labText.scale = 1.5
		labText.parent = this.layQuestionText;
		cc.tween(labText)
			.to(0.4, { scale: 1 })
			.call(() => {
				labText.color = cc.color(60, 0, 0);
				//开始下一题
				EventMgr.emit("nextAnswer");
			})
			.start();
	}

	AddSpt(sptPic) {
		sptPic.y = 0;
		sptPic.parent = this.layQuestionSpt;
		this.photoScare(sptPic, 90)
	}

	Effect_2(sptPic, dmOption: Option) {
		this.AddSpt(sptPic)
		this.AddText2(dmOption.option_text)
	}

	//
	AddText2(Value) {
		let labText = cc.instantiate(this.labText);
		labText.parent = this.layQuestionText;
		labText.getComponent(cc.Label).string = Value;
		labText.active = true;
		cc.tween(labText)
			.to(0.4, { scale: 1.5 })
			.to(0.4, { scale: 1 })
			.call(() => {
				//开始下一题
				EventMgr.emit("nextAnswer");
			})
			.start();
	}

	_tap_btnAudio() {
		let path = this.dmGroup.group_audio_url
		AssetMgr.playAudioByUrl(path)
	}

	_tap_labTitle() {
		let path = this.dmGroup.group_audio_url
		AssetMgr.playAudioByUrl(path)
	}
}
