cc.Class({
    extends: cc.Component,

    properties: {
        playBtn: cc.Node,
        pauseBtn: cc.Node,
        label: cc.Label,
        tiao: cc.Node,
        moveBtn: cc.Node
    },
    onload() {
        this._clip = null
        this._play = false
        this._audioId = null
        this._totoalTime = null
        this._nowTime = null
        this._pause == false
    },
    onEnable() {
        cc.systemEvent.on('stopBgMusic', this.onEvent, this)
        this.moveBtn.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.moveBtn.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.moveBtn.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.moveBtn.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        this.schedule(this.onTimeLoop)
    },

    onDisable() {
        cc.systemEvent.off('stopBgMusic', this.onEvent, this)
        this.moveBtn.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.moveBtn.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.moveBtn.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.moveBtn.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        this.unschedule(this.onTimeLoop)
    },


    init(url) {
        this.onEvent()
        this.playBtn.active = true
        this.pauseBtn.active = false
        cc.assetManager.loadRemote(url, (err, audioClip) => {
            if (err) return
            this.clip = audioClip
            this._totoalTime = audioClip.duration
            this.label.string = '0:00/' + this.transTime(this._totoalTime)
            this.tiao.children[0].getComponent(cc.Sprite).fillRange = 0
            this.tiao.children[1].x = 12
            this.onAudioFinish()
        })
    },

    onAudioFinish() {
        if (this._audioId != null) {
            this.pauseBtn.active = false
            this.playBtn.active = true
        }
        this._play = false
        this._nowTime = 0
        this._audioId = cc.audioEngine.play(this.clip)
        cc.audioEngine.pause(this._audioId)
        cc.audioEngine.setFinishCallback(this._audioId, this.onAudioFinish.bind(this))
    },
    onTouchStart() {
        if (this._play) {
            this._pause = true
            this._play = false
            cc.audioEngine.pause(this._audioId)
        }
    },

    /** 触摸移动 */
    onTouchMove(e) {
        let dis = e.getPreviousLocation().sub(e.getLocation())
        dis = this.moveBtn.x - dis.x
        if (dis < 12) dis = 12
        else if (dis > 376) dis = 376
        let scale = (dis - 12) / 364
        this.moveBtn.x = dis
        this.tiao.children[0].getComponent(cc.Sprite).fillRange = scale
        this.tiao.children[1].x = 12 + 364 * scale
        this._nowTime = this._totoalTime * scale
        this.label.string = this.transTime(this._nowTime) + '/' + this.transTime(this._totoalTime)
    },

    onTouchEnd() {
        if (this._pause) {
            this._play = true
            this._pause = false
            cc.audioEngine.resume(this._audioId)
        }
        cc.audioEngine.setCurrentTime(this._audioId, this._totoalTime * (this.moveBtn.x - 12) / 364)
    },

    onPlay() {
        this.pauseBtn.active = true
        this.playBtn.active = false
        this._play = true
        cc.audioEngine.resume(this._audioId)
        cc.systemEvent.emit('stopWordsMusic')
    },

    onPause() {
        this.pauseBtn.active = false
        this.playBtn.active = true
        this._play = false
        this._pause = true
        cc.audioEngine.pause(this._audioId)
    },

    /** 定时回调 */
    onTimeLoop(dt) {
        if (!this._play) return
        this._nowTime += dt
        this.label.string = this.transTime(this._nowTime) + '/' + this.transTime(this._totoalTime)
        if (this._nowTime > this._totoalTime) this._nowTime = this._totoalTime
        let scale = this._nowTime / this._totoalTime
        this.tiao.children[0].getComponent(cc.Sprite).fillRange = scale
        this.tiao.children[1].x = 12 + 364 * scale
    },

    onEvent() {
        if (!this._play) return
        this.onPause()
    },

    /** 秒转分钟 */
    transTime(time) {
        let temp = Math.floor(time / 60)
        time = (time - 60 * temp).toFixed(0)
        if (time < 10) time = '0' + time
        return temp + ':' + time
    }
});
