package com.adealink.weparty.util

import com.adealink.weparty.R
import com.adealink.weparty.module.profile.data.Gender
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 < 1000_000 -> {
            String.format(Locale.US, "%.2fK", (number.toFloat() / 1000))
        }

        number < 1000_000_000 -> {
            String.format(Locale.US, "%.2fM", (number.toFloat() / 1000_000))
        }

        else -> {
            String.format(Locale.US, "%.2fB", (number.toFloat() / 1000_000_000))
        }
    }
}


fun formatNumberStr(number: Double): String {
    return when {
        number < 1000 -> {
            if (number % 1 == 0.0) {
                "%.0f".format(number)
            } else {
                "%.2f".format(number)
            }
        }

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

        number < 1000_000_000 -> {
            String.format(Locale.US, "%.2fM", (number.toFloat() / 1000_000))
        }

        else -> {
            String.format(Locale.US, "%.2fB", (number.toFloat() / 1000_000_000))
        }
    }
}


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
}


fun getGenderResource(gender: Int?): Int {
    gender ?: return 0
    return when (Gender.map(gender)) {
        Gender.UNKNOWN -> 0
        Gender.FEMALE -> R.drawable.common_female_ic
        Gender.MALE -> R.drawable.common_male_ic
    }
}