package bou.amine.apps.readerforselfossv2.utils

import android.text.format.DateUtils
import kotlinx.datetime.*


actual class DateUtils {
    actual companion object {
        actual fun parseDate(dateString: String): Long {
            return try {
                Instant.parse(dateString).toEpochMilliseconds()
            } catch (e: Exception) {
                var str = dateString.replace(" ", "T")
                if (str.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\+\\d{2}".toRegex())) {
                    str = str.split("+")[0]
                }
                LocalDateTime.parse(str).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
            }
        }

        actual fun parseRelativeDate(dateString: String): String {

            val date = parseDate(dateString)

            return " " + DateUtils.getRelativeTimeSpanString(
                date,
                Clock.System.now().toEpochMilliseconds(),
                DateUtils.MINUTE_IN_MILLIS,
                DateUtils.FORMAT_ABBREV_RELATIVE
            )
        }
    }
}