package bou.amine.apps.readerforselfossv2.utils

import android.text.format.DateUtils
import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import java.time.Instant
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

actual class DateUtils actual constructor(actual val appSettingsService: AppSettingsService) {

    actual fun parseDate(dateString: String): Long {

        val FORMATTERV1 = "yyyy-MM-dd HH:mm:ss"

        return if (appSettingsService.getApiVersion() >= 4) {
            OffsetDateTime.parse(dateString).toInstant().toEpochMilli()
        } else {
            LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(FORMATTERV1)).toInstant(
                ZoneOffset.UTC).toEpochMilli()
        }
    }

    actual fun parseRelativeDate(dateString: String): String {

        val date = parseDate(dateString)

        return " " + DateUtils.getRelativeTimeSpanString(
            date,
            Instant.now().toEpochMilli(),
            DateUtils.MINUTE_IN_MILLIS,
            DateUtils.FORMAT_ABBREV_RELATIVE
        )
    }
}