cc.Class({
    extends: cc.Component,

    properties: {
        spfBase: [cc.SpriteFrame],
        sprBg: cc.Sprite,
        sprAudio: cc.Sprite,
        nodLight: cc.Node,
        dragonBone: dragonBones.ArmatureDisplay,
        audRight: cc.AudioClip,
        audWrong: cc.AudioClip,
        spfAudio: [cc.SpriteFrame],
    },

    // onLoad () {},

    start() {

    },

    onEnable() {
        this.node.on('touchstart', this.onTouchStart, this);
    },
    onDisable() {
        this.node.off('touchstart', this.onTouchStart, this);
    },
    onTouchStart() {
        if (GameData.isMoving) {
            return;
        }
        if (GameData.audioCount != 0) {
            return;
        }
        if (GameData.currOptionIndex == this.index) {
            this.chooseRight();
        } else {
            this.chooseWrong();
        }
    },

    setInfo(data, index, scene) {
        this.scene = scene;
        this.data = data;
        this.index = index;
        this.loadDragonBone();
        if (GameData.questionIndex % 2 == 0) {
            this.sprBg.spriteFrame = this.spfBase[0];
        } else {
            this.sprBg.spriteFrame = this.spfBase[1];
        }
    },
    loadDragonBone() {
        this.dragonBone.dragonAtlasAsset = null;
        this.dragonBone.dragonAsset = null;
        this.dragonBone.armatureName = '';
        var imageUrl = this.data.optionDragonBone.texPngData2.url;
        var skeUrl = this.data.optionDragonBone.skeJsonData2.url;
        var atlasUrl = this.data.optionDragonBone.texJsonData2.url;
        // imageUrl = 'http://127.0.0.1/NewProject_2_tex.png';
        // skeUrl = 'http://127.0.0.1/NewProject_2_ske.json';
        // atlasUrl = 'http://127.0.0.1/NewProject_2_tex.json';
        cc.assetManager.loadAny([{ url: atlasUrl, ext: '.txt' }, { url: skeUrl, ext: '.txt' }], (error, assets) => {
            if (error) {
                console.log(error)
            }
            else {
                cc.assetManager.loadRemote(imageUrl, (error, texture) => {
                    if (error) {
                        console.log(error)
                    }
                    else {
                        var atlas = new dragonBones.DragonBonesAtlasAsset();
                        atlas._uuid = atlasUrl;
                        atlas.atlasJson = assets[0];
                        atlas.texture = texture;

                        var asset = new dragonBones.DragonBonesAsset();
                        asset._uuid = skeUrl;
                        asset.dragonBonesJson = assets[1];

                        this.dragonBone.dragonAtlasAsset = atlas;
                        this.dragonBone.dragonAsset = asset;
                        let data = asset._dragonBonesJsonData.armature[0];
                        if (!data) {
                            return;
                        }
                        this.dragonBone.armatureName = data.name;
                        this.animationName = data.animation[0].name;

                        // this.dragonBone.premultipliedAlpha = true;
                        // atlas.texture.setPremultiplyAlpha(true);
                    }
                });
            }
        });
    },
    chooseRight() {
        this.node.zIndex = 5;
        this.playAudioRight();
        this.nodLight.active = true;
        this.scene.moveToQuestion(this);
    },
    chooseWrong() {
        this.playAudioWrong();
        cc.tween(this.node)
            .to(0.2, { y: -150 })
            .to(0.2, { y: 0 })
            .start()

    },

    playAudioRight() {
        GameData.audioCount++;
        let id = cc.audioEngine.play(this.audRight, false, 1);
        cc.audioEngine.setFinishCallback(id, () => {
            GameData.audioCount--;
        })
    },
    playAudioWrong() {
        GameData.audioCount++;
        let id = cc.audioEngine.play(this.audWrong, false, 1);
        cc.audioEngine.setFinishCallback(id, () => {
            GameData.audioCount--;
        })
    },
    changeAudioSpfCb() {
		this.sprAudio.spriteFrame = this.sprAudio.spriteFrame == this.spfAudio[0] ? this.spfAudio[1] : this.spfAudio[0];
	},
    onBtnClickAudio() {
        if(GameData.audioCount > 0){
            return;
        }
        this.dragonBone.playAnimation(this.animationName, 1);
        this.sprAudio.spriteFrame = this.spfAudio[0];
        this.schedule(this.changeAudioSpfCb, 0.2)
        this.playAudioOption(() => {
            this.unschedule(this.changeAudioSpfCb)
            this.sprAudio.spriteFrame = this.spfAudio[1];
            GameData.isAudio = false;
        });
    },

    playAudioOption(cb) {
        GameData.audioCount++;
        let url = this.data.optionAudio;
        // url = 'http://127.0.0.1/iamflying.mp3';
        cc.assetManager.loadRemote(url, null, (err, clip) => {
            if (err) {
                console.log(err)
                return;
            }
            let id = cc.audioEngine.play(clip, false, 1);
            cc.audioEngine.setFinishCallback(id, () => {
                if (!cc.isValid(this)) {
                    return;
                }
                GameData.audioCount--;
                cb && cb();
            })
        });
    },
    // update (dt) {},
});
