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._totoalTime = null
        this._nowTime = 0
        this._pause = false
        this.clip = null
    },
    onEnable() {
        this._nowTime = 0
        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) {
        if (!this.au) {
            this.au = new cc.AudioSource()
        }
        this.onEvent()
        this.node.active = false
        cc.assetManager.loadRemote(url, (err, audioClip) => {
            if (err) return
            this.clip = audioClip
            this._nowTime = 0
            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.au.clip = this.clip
            this.node.active = true
        })
    },

    onAudioFinish() {
        if (this._play) {
            this.pauseBtn.active = false
            this.playBtn.active = true
            this._play = false
        }
        this._nowTime = 0
    },

    onTouchStart() {
        if (this._play) {
            this._pause = true
            this._play = false
            this.au.pause()
        }
    },

    /** 触摸移动 */
    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
            this.au.play()
        }
        this._nowTime = this._totoalTime * (this.moveBtn.x - 12) / 364
        this.au.setCurrentTime(this._nowTime)
    },

    onPlay() {
        if (this._play) return
        this._play = true
        this.pauseBtn.active = true
        this.playBtn.active = false
        this.au.play()
        this.au.setCurrentTime(this._nowTime)
        cc.systemEvent.emit('stopWordsMusic')
    },

    onPause() {
        if (!this._play) return
        this._play = false
        this.pauseBtn.active = false
        this.playBtn.active = true
        this.au.stop()
    },

    /** 定时回调 */
    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
        if (this._nowTime == this._totoalTime) {
            this.onPause()
            this._nowTime = 0
        }
    },

    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
    }
});
