import pg from "./pg";

// @const
const { ccclass, property } = cc._decorator;

/**
 * 
 */
@ccclass
export default class OptionItem extends cc.Component {

    @property(cc.Node)
    public ani_flower: cc.Node;
    @property(cc.AudioClip)
    public errorClip: cc.AudioClip;
    @property(cc.AudioClip)
    public rightClip: cc.AudioClip;
    @property(cc.AudioClip)
    public flyClip: cc.AudioClip;

    private speed = 1.5;
    private updateView = false;
    private isClick = false;
    /**
     * onLoad
     */
    onLoad(): void {
    }
    protected update(dt: number): void {

    }
    public initItem(data, type) {
        let checkmark = this.node.getChildByName("checkmark");
        if (type == "text") this.initTextItem(data);
        else if (type == "pic") this.initPicItem(data);
        else if (type == "picText") this.initPicTextItem(data);

        this.node.on(cc.Node.EventType.TOUCH_END, () => {
            if (this.isClick) return;
            if (data.answerRight == 0) {
                this.itemError(this.node);
                return;
            }
            this.isClick = true;
            cc.audioEngine.playEffect(this.rightClip, false);
            checkmark.active = true;
            this.itemAction();
        })
    }
    public initTextItem(data) {
        let g_label = this.node.getChildByName("g_label");
        g_label.active = true;
        g_label.getComponent(cc.Label).string = data.text;
    }
    public initPicItem(data) {
        let g_img = this.node.getChildByName("g_img");
        g_img.active = true;
        let head = g_img.getChildByName("head");
        pg.view.setNetImg(head, data.pic_url, { w: 245, h: 160 });
    }
    public initPicTextItem(data) {
        let g_imgText = this.node.getChildByName("g_imgText");
        g_imgText.active = true;
        let head = g_imgText.getChildByName("head");
        let Label = g_imgText.getChildByName("Label");
        pg.view.setNetImg(head, data.pic_url, { w: 245, h: 160 });
        Label.getComponent(cc.Label).string = data.text;
    }
    private itemError(item) {
        cc.audioEngine.playEffect(this.errorClip, false);
        let funTw = cc.tween;
        funTw(item)
            .by(0.01, { angle: 5 })
            .to(0.01, { angle: 0 })
            .by(0.01, { angle: -5 })
            .to(0.01, { angle: 0 })
            .by(0.01, { angle: 5 })
            .to(0.01, { angle: 0 })
            .by(0.01, { angle: -5 })
            .to(0.01, { angle: 0 })
            .by(0.01, { angle: 5 })
            .to(0.01, { angle: 0 })
            .by(0.01, { angle: -5 })
            .to(0.01, { angle: 0 })
            .start();
    }

    private showAniFlower(target) {
        // let pos = target.convertToWorldSpaceAR(cc.Vec2.ZERO);
        // let pos2 = this.node.convertToNodeSpaceAR(pos);
        // let ani_flower = cc.instantiate(this.ani_flower);
        // target.addChild(ani_flower);
        // ani_flower.x = 0;//pos2.x;
        // ani_flower.y = 0;//pos2.y;
        // ani_flower.active = true;

        // cc.audioEngine.playEffect(this.rightClip, false);
        // pg.audio.playAudio(this.rightClip, () => {
        //     // if (this.data.isCheck) this.node.runAction(cc.fadeOut(0.5));
        // })
        // setTimeout(() => {
        //     ani_flower && (ani_flower.parent = null);
        // }, 3000);
    }
    private itemAction() {
        setTimeout(() => {
            cc.audioEngine.playEffect(this.flyClip, false);
        }, 2000);
        cc.tween(this.node)
            .delay(2)
            .to(1, { y: 1550 })
            .call(() => {
                pg.event.emit("mrbr05_click_right");
            })
            .start();
        // this.node.runAction(cc.fadeOut(1.5));
    }
}
