package com.adealink.weparty.util

import java.util.Calendar
import java.util.Locale

//格式化数字 --数值加逗号
fun formatDotNotNull(long: Long?): String? {
    if(long == null) return long
    return "%,d".format(Locale.US, long)
}

//格式化数字 -- 保留所有位数
fun formatNumberStrFull(number: Long): String {
    return when {
        number < 1000 -> {
            number.toString()
        }
        number < 1000000 -> {
            val value = number.toDouble() / 1000
            val valueStr = value.toString().trimEnd('0').trimEnd('.') // 去掉多余的0和点
            String.format(Locale.US, "%sK", valueStr)
        }
        else -> {
            val value = number.toDouble() / 1000000
            val valueStr = value.toString().trimEnd('0').trimEnd('.') // 去掉多余的0和点
            String.format(Locale.US, "%sM", valueStr)
        }
    }
}


//格式化数字
fun formatNumberStr(number: Long): String {
    return when {
        number < 1000 -> {
            number.toString()
        }

        number < 1000000 -> {
            String.format(Locale.US, "%.1fK", (number.toFloat() / 1000))
        }

        else -> {
            String.format(Locale.US, "%.1fM", (number.toFloat() / 1000000))
        }
    }
}

fun formatStar(star: Float?): String {
    return if (star == null || star < 0) {
        "-"
    } else {
        String.format(Locale.US, "%.1f", star)
    }
}

fun isWithinTwoDays(registerTime: Long): Boolean {
    val twoDaysInMillis = 2 * 24 * 60 * 60 * 1000 // 两天的毫秒数
    return System.currentTimeMillis() - registerTime <= twoDaysInMillis
}

/**
 * 通过生日计算年龄
 */
fun getAgeByBirthday(birthday: Long): Int {
    val currentCalendar: Calendar = Calendar.getInstance()
    currentCalendar.timeInMillis = System.currentTimeMillis()
    val currentYear: Int = currentCalendar.get(Calendar.YEAR)

    val birthCalendar: Calendar = Calendar.getInstance()
    birthCalendar.timeInMillis = birthday
    val birthYear: Int = birthCalendar.get(Calendar.YEAR)

    var age = currentYear - birthYear
    if (currentCalendar.before(birthCalendar)) {
        // 如果未过生日，减去一年
        age -= 1
    }
    return age
}