package com.aiosman.riderpro.exp

import android.content.Context
import android.icu.text.SimpleDateFormat
import android.icu.util.Calendar
import androidx.compose.ui.res.stringResource
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.api.ApiClient
import java.util.Date
import java.util.Locale

/**
 * 格式化时间为 xx 前
 */
fun Date.timeAgo(context: Context): String {
    val now = Date()
    val diffInMillis = now.time - this.time

    val seconds = diffInMillis / 1000
    val minutes = seconds / 60
    val hours = minutes / 60
    val days = hours / 24
    val years = days / 365

    return when {
        seconds < 60 -> context.getString(R.string.second_ago, seconds)
        minutes < 60 -> context.getString(R.string.minute_ago, minutes)
        hours < 24 -> context.getString(R.string.hour_ago, hours)
        days < 365 -> context.getString(R.string.days_ago, days)
        else -> SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this)
    }
}

/**
 * 格式化时间为 xx-xx
 */
fun Date.formatPostTime(): String {
    val now = Calendar.getInstance()
    val calendar = Calendar.getInstance()
    calendar.time = this
    val year = calendar.get(Calendar.YEAR)
    var nowYear = now.get(Calendar.YEAR)
    val dateFormat = if (year == nowYear) {
        SimpleDateFormat("MM-dd", Locale.getDefault())
    } else {
        SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
    }
    return dateFormat.format(this)
}

/**
 * YYYY.DD.MM HH:MM
 */
fun Date.formatPostTime2(): String {
    val calendar = Calendar.getInstance()
    calendar.time = this
    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH) + 1
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    val minute = calendar.get(Calendar.MINUTE)
    return "$year.$month.$day $hour:$minute"
}