/**
 * 选项框（带播放按钮）
 */

cc.Class({
    extends: cc.Component,

    properties: {
        // 声音图标
        iconSound: {
            default: null,
            type: cc.Node,
        },

        // 声音图标
        rightFrame: {
            default: null,
            type: cc.Node,
        },

        // 声音图标
        rightPic: {
            default: null,
            type: cc.Sprite,
        },

        // 星星特效
        particle_star: {
            default: null,
            type: cc.Prefab,
        },

        _opId: 0, // 选项id
        _soundUrl: "", // 音频文件地址
        _opPic: null,
        _isRight: false,
    },

    /**
     * 播放声音
     */
    play() {
        if (this._soundUrl != "") {
            cc.assetManager.loadRemote(this._soundUrl, (err, audioClip) => {
                const audioId = cc.audioEngine.play(audioClip, false, 0.8);
                this.iconSound.getComponent(cc.Animation).play("icon_sound_play");
                cc.audioEngine.setFinishCallback(audioId, () => {
                    this.iconSound.getComponent(cc.Animation).play("icon_sound_finish");
                });
            });
        }
    },

    /**
     * 当碰撞产生的时候调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionEnter: function (other, self) {
        this._opPic = other;
    },

    /**
     * 当碰撞结束后调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionExit: function (other, self) {
        console.log("on collision exit");
    },

    correct(spriteFrame) {
        this.rightFrame.active = true;
        // cc.loader.load({ url }, (err, img) => {
        //     const spriteFrame = new cc.SpriteFrame(img);
        //     this.rightPic.spriteFrame = spriteFrame;
        // });
        this.rightPic.spriteFrame = spriteFrame;

        var par = cc.instantiate(this.particle_star);
        this.node.addChild(par);

        this._isRight = true;

        cc.game.emit("RIGHT");
    },

    wrong(spriteFrame) {
        this.rightFrame.active = true;
        // cc.loader.load({ url }, (err, img) => {
        //     const spriteFrame = new cc.SpriteFrame(img);
        //     this.rightPic.spriteFrame = spriteFrame;
        // });
        this.rightPic.spriteFrame = spriteFrame;
        let time = 0.1;
        let action = cc.repeat(
            cc.sequence(cc.moveBy(time, -10, 0), cc.moveBy(time, 10, 0), cc.moveBy(time, 10, 0), cc.moveBy(time, -10, 0)),
            5
        );

        this.node.runAction(action);
        this.scheduleOnce(function () {
            this.rightFrame.active = false;
            this.rightPic.spriteFrame = null;
            this._opPic.node.active = true;
            this._opPic.node.getComponent("op_pic").resetPos();
        }, 2.2);
    },
});
