package net.pokeranalytics.android.util.extensions

import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.*


// Double

fun Double.round(): String {
    val formatter = DecimalFormat("##.##")
    return formatter.format(this)
}

fun Double.formatted(): String {
    val format = NumberFormat.getNumberInstance()
    format.maximumFractionDigits = 2
    format.minimumFractionDigits = 0
    return format.format(this)
}

fun Double.toCurrency(currency: Currency? = null): String {

    val currencyFormatter = NumberFormat.getCurrencyInstance()
	currency?.let {
		currencyFormatter.currency = currency
	}

    currencyFormatter.maximumFractionDigits = 2
    currencyFormatter.minimumFractionDigits = 0
    return currencyFormatter.format(this)
}

fun Double.formattedHourlyDuration() : String {
    return (this * 1000 * 3600).toLong().toMinutes()
}

// Return the time from milliseconds to hours:minutes
fun Long.toMinutes() : String {
    val totalMinutes = this / (1000 * 60)
    val hours = totalMinutes / 60
    val minutesLeft = totalMinutes % 60
    var duration = ""
    duration += if (hours < 10) "0$hours" else hours.toString()
    duration += ":"
    duration += if (minutesLeft < 10) "0$minutesLeft" else minutesLeft.toString()
    return duration

}