package de.kif.common

import com.soywiz.klock.DateFormat
import com.soywiz.klock.KlockLocale
import com.soywiz.klock.format
import com.soywiz.klock.locale.german

fun formatDateTime(unix: Long) =
    DateFormat("EEEE, d. MMMM y HH:mm")
        .withLocale(KlockLocale.german)
        .format(unix)

fun formatTimeDiff(time: Long, now: Long): String {
    var dt = (time - now) / 1000

    val seconds = dt % 60
    dt /= 60
    val minutes = dt % 60
    dt /= 60
    val hours = dt % 24
    dt /= 24
    val days = dt

    return when {
        days > 1L -> {
            "in $days Tagen"
        }
        days > 0L -> {
            "morgen"
        }
        hours > 1L -> {
            val nowHour = DateFormat("HH")
                .withLocale(KlockLocale.german)
                .format(now).toIntOrNull() ?: 0

            val ht = DateFormat("HH:mm")
                .withLocale(KlockLocale.german)
                .format(time)

            if ((ht.substringBefore(":").toIntOrNull() ?: 0) < nowHour ) {
                "morgen"
            } else "um $ht"
        }
        hours > 0L -> {
            "in " +hours.toString().padStart(2, '0') + ":" + (minutes + if (seconds > 0) 1 else 0).toString().padStart(2, '0')
        }
        minutes > 0L -> {
            "in 00:" + (minutes + if (seconds > 0) 1 else 0).toString().padStart(2, '0')
        }
        seconds > 0L -> {
            "in > 1 Minute"
        }
        else -> "---"
    }
}
