const tools = require("../script/tools");
var onlyOne
cc.Class({
    extends: cc.Component,

    properties: {
        quan: cc.Node,
        tipsAnim: cc.Animation,
        handAnim: cc.Animation,
        hand: cc.Node,
        sprite: cc.Sprite,
        tipClip: cc.AudioClip,
        btnX: cc.Node,
        icon_play: cc.Node,
        icon_stop: cc.Node,
        quan_lan: cc.SpriteFrame,
        quan_huang: cc.SpriteFrame
    },

    data: null,

    onLoad() {
        this._playing = false
        this.quan.active = false
        this.tipsAnim.node.active = false
        this.hand.active = false
        // this.btnX.active = false
        this.icon_stop.active = false
    },

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    },

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    },

    initWithData(data, scale) {
        this.data = data
        let rect = this.data.rect
        this.node.x = (rect.x + rect.width / 2) / scale
        this.node.y = (-rect.y - rect.height / 2) / scale
        this.node.width = rect.width / scale * 0.8
        this.node.height = rect.height / scale * 0.8
        this.sprite.node.active = false
        if (data.useHand) {
            this.hand.active = true
            this.hand.getComponent("btn_rotate").play()
        }
        let comps = this.getComponentsInChildren(cc.Widget)
        for (let one of comps) {
            one.updateAlignment()
        }
        this.audioId = null
    },

    onTouchStart() {
        return new Promise((resolve, reject) => {
            if (!this.data.audio_url) {
                this.hand.active = false
                this.stopAudio();
                if (this.data.useBorder) {
                    this.quan.active = true
                }
                return;
            }
            if (this._playing || this.audioId) {
                //增加暂停功能。
                this._playing = false;
                //播放切换
                this.icon_play.active = true;
                this.icon_stop.active = false;
                if (!this.audioId) return;
                cc.audioEngine.stop(this.audioId);
                this.audioId = null;
                return
            }

         
            //暂停连续播放的音频
            const canvas = cc.find("Canvas");
            canvas.getComponent('Book_Read').stopAll();
            //暂停其他正在播放的音频
            cc.systemEvent.emit('stopMusic')

            if (this.data) {
                this.quan.getComponent(cc.Sprite).spriteFrame = this.quan_lan;
                // this.btnX.active = true
                this.icon_stop.active = true
                this.icon_play.active = false;
                if (this.data.pic_url && this.data.gIdx == 0) {
                    tools.getSpriteFrimeByUrl(this.data.pic_url, (sp) => {
                        this.sprite.spriteFrame = sp
                        this.sprite.node.width = this.node.width
                        this.sprite.node.height = this.node.height
                        this.sprite.node.active = true
                    })
                }
                if (this.data.useBorder) {
                    this.quan.active = true
                }
                this.hand.getComponent("btn_rotate").stop()
                this.hand.active = false

                // if (onlyOne && onlyOne != this) {
                //     onlyOne.hideBox()
                // }
                onlyOne = this
                if (!this._playing) {
                    this._playing = true
                    cc.systemEvent.once('stopMusic', this.onStopAudio, this)
                    // let id = cc.audioEngine.play(this.tipClip)
                    // cc.audioEngine.setFinishCallback(id, () => {
                    if (onlyOne != this) {
                        this._playing = false
                        return
                    }
                    let url = this.data.audio_url
                    if (url && url != '') {
                        this.playAudio(url)
                    } else {
                        this._playing = false
                    }
                    // })
                }
            }
            if (this.timer) clearInterval(this.timer);
            this.timer = setInterval(() => {
                if (!this._playing) {
                    if (this.timer) clearInterval(this.timer);
                    this.timer = null;
                    resolve('');
                }
            }, 60);
        });
    },

    onClickHide() {
        this.sprite.node.active = false
        this.hideBox()
        this.stopAudio()
    },

    /**隐藏边框 */
    showBox() {
        if (this.data.useBorder) {
            this.quan.active = true
        }
        this.hand.active = false
        // this.btnX.active = true
        // this.icon_stop.active = true
        this.quan.getComponent(cc.Sprite).spriteFrame = this.quan_lan;
    },

    /**隐藏边框 */
    hideBox() {
        // this.quan.active = false
        // this.btnX.active = false
        // this.icon_stop.active = false
        this.hand.active = false
        this.quan.getComponent(cc.Sprite).spriteFrame = this.quan_huang;
    },

    /** 播放音乐 */
    playAudio(url) {
        cc.assetManager.loadRemote(url, (err, audioClip) => {
            if (err) return
            if (!this._playing) return
            this.audioId = cc.audioEngine.play(audioClip, false, 0.8);
            cc.audioEngine.setFinishCallback(this.audioId, this.stopAudio.bind(this));
            // this.tipsAnim.node.active = true
            // this.tipsAnim.play()
        });
    },

    onStopAudio() {
        this._playing = false
        this.stopAudio()
    },
    stopAudio() {
        this._playing = false
        if (this.audioId != null) {
            cc.audioEngine.stop(this.audioId)
            this.audioId = null
        }
        // this.tipsAnim.stop()
        // this.tipsAnim.node.active = false
        //变成黄色框框
        this.quan.getComponent(cc.Sprite).spriteFrame = this.quan_huang;
        this.icon_stop.active = false;
        cc.systemEvent.off('stopMusic', this.onStopAudio, this)
    },
    /** 音频id */
    audioId: null,
});
