const { ccclass, property } = cc._decorator;
import { DragGameAnswerObject } from "../Common/DragGame/DragGameAnswerObject";
import { DragGameOptionObject_001 } from "../Common/DragGame/DragGameOptionObject";
import AssetMgr from "../Common/Module/AssetMgr/AssetMgr";
import { DCGame, Option } from "../DataCenter/DCGame";
import { UICtrl } from "./../Common/UICtrl/UICtrl";

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

		this.initProperty();

		this.updateUI();
	}
	//
	private labText: cc.Label;//文字节点
	private sptPhoto: cc.Sprite;//图片节点

	private dmOption: Option;

	private DragGameAnswerObject;
	initProperty() {
		this.labText = this.view["nodText/labText"]?.getComponent(cc.Label);
		this.sptPhoto = this.view["nodPhoto/sptPhoto"]?.getComponent(cc.Sprite);

		this.DragGameAnswerObject = this.node.getComponent(DragGameAnswerObject)
	}

	setData(value: Option) {
		this.dmOption = value;
		this.updateUI();
		this.DragGameAnswerObject.setData(value);
	}

	//更新界面使用
	updateUI(): void {
		if (!this.dmOption) return;
		//设置Label文字
		this.labText && this.setText();

		//设置图片
		this.sptPhoto && this.setPhoto();
	};

	//设置labText
	setText() {
		let value = this.dmOption.option_text
		this.labText.string = value;
	}

	//设置图片
	setPhoto() {
		let value = this.dmOption.option_pic_url
		AssetMgr.getSpriteFrimeByUrl(value, (spriteFrame) => {
			this.sptPhoto.node.x = 0;
			this.sptPhoto.node.y = 0;
			this.sptPhoto.spriteFrame = spriteFrame
			this.photoScare(this.sptPhoto.node, 100);
		})
	}

	//
	AnswerRightEffect_1() {
		let objOption = DCGame.getInstance().objOption
		//文字变红
		this.labText.node.color = cc.Color.RED;
		//放到目标点位置
		//放大
		cc.tween(this.node)
			.to(0.4, { scale: 1.5 })
			.call(() => {
				let nodText = cc.instantiate(this.labText.node);
				objOption.getComponent(DragGameOptionObject_001).Effect_1(nodText, this.dmOption);

				this.node.active = false;
				this.labText.node.color = cc.Color.WHITE;
				// this.DragGameAnswerObject.setOldPosition();
			})
			.start();
	}

	//效果2
	AnswerRightEffect_2() {
		//效果2
		console.log("效果2");
		//拷贝节点放在原来的地方
		let objOption = DCGame.getInstance().objOption
		let nodPic = cc.instantiate(this.sptPhoto.node);

		objOption.getComponent(DragGameOptionObject_001).Effect_2(nodPic, this.dmOption);

		this.node.active = false;
		// this.DragGameAnswerObject.setOldPosition();
	}
}
