const tools = require("../script/tools");


cc.Class({
    extends: cc.Component,

    properties: {
        quan: cc.Node,
        tipsAnim: cc.Animation,
        handAnim: cc.Animation,
        sprite: cc.Sprite
    },

    data: null,

    onLoad() {
        this.quan.active = false
        this.tipsAnim.node.active = false
        this.handAnim.node.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) {
        console.log(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
        this.node.height = rect.height / scale
        console.log(this.node)
        this.sprite.node.active = false
        if (data.useHand) {
            this.handAnim.node.active = true
            this.handAnim.play()
        }
        let comps = this.getComponentsInChildren(cc.Widget)
        for (let one of comps) {
            one.updateAlignment()
        }
        this.audioId = null
    },

    onTouchStart() {
        if (this.audioId) return
        if (this.data) {
            let url = this.data.audio_url
            if (url && url != '') {
                this.playAudio(url)
                this.tipsAnim.node.active = true
                this.tipsAnim.play()
            }
            if (this.data.pic_url) {
                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.handAnim.stop()
            this.handAnim.node.active = false

        }
    },

    /** 播放音乐 */
    playAudio(url) {
        cc.assetManager.loadRemote(url, (err, audioClip) => {
            if (err) return
            cc.systemEvent.once('stopMusic', this.stopAudio, this)
            this.audioId = cc.audioEngine.play(audioClip, false, 0.8);
            cc.audioEngine.setFinishCallback(this.audioId, this.stopAudio.bind(this));
        });
    },

    stopAudio() {
        if (this.audioId != null) {
            cc.audioEngine.stop(this.audioId)
            this.audioId = null
            this.quan.active = false
            this.tipsAnim.stop()
            this.tipsAnim.node.active = false
            this.sprite.node.active = false
            if (this.data.useHand) {
                this.handAnim.node.active = true
                this.handAnim.play()
            }
            cc.systemEvent.off('stopMusic', this.stopAudio, this)
        }
    },
    /** 音频id */
    audioId: null,
});
