package com.gh.gamecenter.xapk.tool

class ProgressRecorder {

    companion object {
        const val SAMPLE_TIME_INTERVAL = 200
    }

    private var sampleTimeInMills: Long = 0
    private var totalBytes: Long = 0
    private var consumeBytes: Long = 0

    fun start(totalBytes: Long) {
        this.totalBytes = totalBytes
        record(0)
    }

    fun record(consumeBytes: Int): Boolean {
        this.consumeBytes += consumeBytes.toLong()
        val consumeBytesTimeInMills = System.currentTimeMillis()
        if (SAMPLE_TIME_INTERVAL <= consumeBytesTimeInMills - sampleTimeInMills) {
            sampleTimeInMills = consumeBytesTimeInMills
            return true
        }
        return false
    }

    val progress: Float get() = consumeBytes * 1.00F / totalBytes
}