package syobotsum.actor

import com.badlogic.gdx.scenes.scene2d.Action
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Skin

class Counter(skin: Skin) : Label("00", skin, "counter") {

    var count: Int = 0
    set(value) {
        field = value
        setText(String.format("%02d", value))
    }

    var callback: Runnable? = null

    fun start() {
        val decrement = Actions.run(Runnable {
            count = count - 1
        })
        val delay = Actions.delay(1f, decrement)
        val repeat = Actions.repeat(count, delay)
        val run = callback.let { Actions.run(it) }
        addAction(Actions.sequence(repeat, run))
    }
}
