package melody.offline.music.util

class FileSizeConverter(private val sizeInBytes: Long) {

    companion object {
        private const val BYTE: Long = 1
        private const val KILOBYTE: Long = 1024
        private const val MEGABYTE: Long = 1024 * KILOBYTE
        private const val GIGABYTE: Long = 1024 * MEGABYTE
        private const val TERABYTE: Long = 1024 * GIGABYTE

        fun formatSize(sizeInBytes: Long): String {
            return when {
                sizeInBytes >= TERABYTE -> String.format("%.2f TB", sizeInBytes / TERABYTE.toDouble())
                sizeInBytes >= GIGABYTE -> String.format("%.2f GB", sizeInBytes / GIGABYTE.toDouble())
                sizeInBytes >= MEGABYTE -> String.format("%.2f MB", sizeInBytes / MEGABYTE.toDouble())
                sizeInBytes >= KILOBYTE -> String.format("%.2f KB", sizeInBytes / KILOBYTE.toDouble())
                else -> String.format("%d B", sizeInBytes)
            }
        }
    }

    fun toBytes(): Long {
        return sizeInBytes
    }

    fun toKilobytes(): Double {
        return sizeInBytes / KILOBYTE.toDouble()
    }

    fun toMegabytes(): Double {
        return sizeInBytes / MEGABYTE.toDouble()
    }

    fun toGigabytes(): Double {
        return sizeInBytes / GIGABYTE.toDouble()
    }

    fun toTerabytes(): Double {
        return sizeInBytes / TERABYTE.toDouble()
    }

    fun formattedSize(): String {
        return formatSize(sizeInBytes)
    }
}
