import pg from "./pg_dg29_museum";

const { ccclass, property } = cc._decorator;

@ccclass
export default class dg_xiaodi extends cc.Component {

    @property(cc.Node)
    aniRole: cc.Node = null;

    onLoad() {
    }
    hideAll() {
        cc.audioEngine.stopAllEffects();
        this.node.active = false;
    }
    showAll() {
        this.node.active = true;
    }
    private audioIdEnter: any;
    playEnter(audioUrl) {
        return new Promise((resolve, reject) => {
            this.node.active = true;
            let startX = 1500
            this.aniRole.x = startX;
            cc.tween(this.aniRole)
                .call(() => {
                    pg.view.playDBAnimation(this.aniRole, 'run');
                })
                .to(0.5, { x: 800 })
                .call(() => {
                    pg.view.playDBAnimation(this.aniRole, 'talk');

                    pg.audio.playAudioByUrl(audioUrl, () => {
                        this.audioIdEnter = null;
                        this.scheduleOnce(() => {
                            this.playEnterEnd()
                            resolve('');
                        }, 0.5)
                    }, audioId => {
                        this.audioIdEnter = audioId;
                    })
                })
                .start();
        });
    }
    playEnterEnd() {
        // this.node.active = false;
        pg.view.playDBAnimation(this.aniRole, 'normal');
        if (this.audioIdEnter) {
            cc.audioEngine.stopEffect(this.audioIdEnter)
        }
    }


    // private audioIdStart: any;
    // playQuestionStart(audioUrl) {
    //     return new Promise((resolve, reject) => {
    //         pg.view.playDBAnimation(this.aniRole, 'talk');

    //         pg.audio.playAudioByUrl(audioUrl, () => {
    //             this.audioIdStart = null;
    //             this.scheduleOnce(() => {
    //                 this.playQuestionStartEnd()
    //                 resolve('');
    //             }, 0.5)
    //         }, audioId => {
    //             this.audioIdStart = audioId;
    //         })
    //     })
    // }
    // playQuestionStartEnd() {
    //     pg.view.playDBAnimation(this.aniRole, 'normal');
    //     if (this.audioIdStart) {
    //         cc.audioEngine.stopEffect(this.audioIdStart)
    //     }
    // }


    private audioIdVoice: any;
    playQuestionVoice(audioUrl) {
        return new Promise((resolve, reject) => {
            pg.view.playDBAnimation(this.aniRole, 'talk');

            pg.audio.playAudioByUrl(audioUrl, () => {
                this.audioIdVoice = null;
                this.scheduleOnce(() => {
                    this.playQuestionVoiceEnd()
                    resolve('');
                }, 0.5)
            }, audioId => {
                this.audioIdVoice = audioId;
            })
        })
    }
    playQuestionVoiceEnd() {
        pg.view.playDBAnimation(this.aniRole, 'normal');
        if (this.audioIdVoice) {
            cc.audioEngine.stopEffect(this.audioIdVoice)
        }
    }


    private audioIdOver: any;
    playOver(audioUrl) {
        this.node.active = true;
        // let startX = this.defaultX + this.moveX;
        // this.aniRole.x = startX;
        // cc.tween(this.aniRole)
        //     .call(() => {
        //         pg.view.playDBAnimation(this.aniRole, 'run');
        //         if (this.moveX < 0) {
        //             this.aniRole.scaleX = -this.aniRole.scaleX;
        //         }
        //     })
        //     .to(0.3, { x: this.defaultX })
        //     .call(() => {
        //         if (this.moveX < 0) {
        //             this.aniRole.scaleX = -this.aniRole.scaleX;
        //         }
        //         pg.view.playDBAnimation(this.aniRole, 'talk');
        //         pg.audio.playAudioByUrl(audioUrl, () => {
        //             this.audioIdOver = null;
        //             pg.view.playDBAnimation(this.aniRole, 'normal');
        //         }, audioId => {
        //             this.audioIdOver = audioId;
        //         })
        //     })
        //     .start();
    }
    playOverEnd() {
        this.node.active = false;
        if (this.audioIdOver) {
            cc.audioEngine.stopEffect(this.audioIdOver)
        }
    }
    start() {

    }

    update(dt) {

    }
}






