package com.adealink.weparty.util

import com.adealink.weparty.App
import com.adealink.weparty.R
import com.adealink.weparty.module.profile.data.Gender
import java.util.Calendar

fun formatNumber(number: Int, group: Boolean): String {
    val locale = App.instance.languageManager.getLanguage().locale
    return if (group) {
        "%,d".format(locale, number)
    } else {
        "%d".format(locale, number)
    }
}

fun formatNumber(number: Long, group: Boolean): String {
    val locale = App.instance.languageManager.getLanguage().locale
    return if (group) {
        "%,d".format(locale, number)
    } else {
        "%d".format(locale, number)
    }
}

fun formatNumber(number: Float, group: Boolean): String {
    val locale = App.instance.languageManager.getLanguage().locale
    return when {
        number % 1f == 0f -> {
            //整数
            if (group) {
                "%,.0f".format(locale, number)
            } else {
                "%.0f".format(locale, number)
            }
        }

        number * 10 % 1f == 0f -> {
            //一位小数
            if (group) {
                "%,.1f".format(locale, number * 10f / 10)
            } else {
                "%.1f".format(locale, number * 10f / 10)
            }
        }

        else -> {
            //默认2位小数
            if (group) {
                "%,.2f".format(locale, number * 100f / 100)
            } else {
                "%.2f".format(locale, number * 100f / 100)
            }
        }
    }
}

fun formatNumber(number: Double, group: Boolean): String {
    val locale = App.instance.languageManager.getLanguage().locale
    return when {
        number % 1.0 == 0.0 -> {
            //整数
            if (group) {
                "%,.0f".format(locale, number)
            } else {
                "%.0f".format(locale, number)
            }
        }

        number * 10.0 % 1.0 == 0.0 -> {
            //一位小数
            if (group) {
                "%,.1f".format(locale, number * 10f / 10)
            } else {
                "%.1f".format(locale, number * 10f / 10)
            }
        }

        else -> {
            //默认2位小数
            if (group) {
                "%,.2f".format(locale, number * 100f / 100)
            } else {
                "%.2f".format(locale, number * 100f / 100)
            }
        }
    }
}

/**
 *  @param omitted, 缩写
 *  @param group, 千分位
 */
fun formatNumberStr(number: Int, omitted: Boolean = true, group: Boolean = false): String {
    if (!omitted) {
        return formatNumber(number, group)
    }
    return when {
        number < 1000 -> {
            formatNumber(number, group)
        }

        number < 1000_000 -> {
            val number = (number.toFloat() / 1000)
            "${formatNumber(number, group)}K"
        }

        number < 1000_000_000 -> {
            val number = (number.toFloat() / 1000_000)
            "${formatNumber(number, group)}M"
        }

        else -> {
            val number = (number.toFloat() / 1000_000_000)
            "${formatNumber(number, group)}B"
        }
    }
}

fun formatNumberStr(number: Long, omitted: Boolean = true, group: Boolean = false): String {
    if (!omitted) {
        return formatNumber(number, group)
    }
    return when {
        number < 1000 -> {
            formatNumber(number, group)
        }

        number < 1000_000 -> {
            val number = (number.toDouble() / 1000)
            "${formatNumber(number, group)}K"
        }

        number < 1000_000_000 -> {
            val number = (number.toDouble() / 1000_000)
            "${formatNumber(number, group)}M"
        }

        else -> {
            val number = (number.toDouble() / 1000_000_000)
            "${formatNumber(number, group)}B"
        }
    }
}

fun formatNumberStr(number: Double, omitted: Boolean = true, group: Boolean = false): String {
    if (!omitted) {
        return formatNumber(number, group)
    }
    return when {
        number < 1000 -> {
            formatNumber(number, group)
        }

        number < 1000_000 -> {
            val number = (number / 1000)
            "${formatNumber(number, group)}K"
        }

        number < 1000_000_000 -> {
            val number = (number / 1000_000)
            "${formatNumber(number, group)}M"
        }

        else -> {
            val number = (number / 1000_000_000)
            "${formatNumber(number, group)}B"
        }
    }
}

fun formatNumberStr(number: Float, omitted: Boolean = true, group: Boolean = false): String {
    if (!omitted) {
        return formatNumber(number, group)
    }
    return when {
        number < 1000 -> {
            formatNumber(number, group)
        }

        number < 1000_000 -> {
            val number = (number / 1000)
            "${formatNumber(number, group)}K"
        }

        number < 1000_000_000 -> {
            val number = (number / 1000_000)
            "${formatNumber(number, group)}M"
        }

        else -> {
            val number = (number / 1000_000_000)
            "${formatNumber(number, group)}B"
        }
    }
}

/**
 * @param distance 单位km
 */
fun formatDistanceStr(distance: Float, omitted: Boolean = true, group: Boolean = false): String {
    if (!omitted) {
        return "${formatNumber(distance, group)}Km"
    }
    return when {
        distance < 1000 -> {
            "${formatNumber(distance, group)}Km"
        }

        distance < 1000_000 -> {
            val distance = distance / 1000
            "${formatNumber(distance, group)}Mm"
        }

//        distance < 1000_000_000 -> {
//            String.format(App.instance.languageManager.getLanguage().locale, "%.2fBm", (distance / 1000_000))
//        }

        else -> {
            val distance = distance / 1000_000_000
            "${formatNumber(distance, group)}Bm"
        }
    }
}


fun formatStar(star: Float?, default: String = "-"): String {
    return if (star == null || star < 0) {
        default
    } else {
        if (star % 1f == 0f) {
            "%.0f".format(App.instance.languageManager.getLanguage().locale, star)
        } else {
            "%.1f".format(App.instance.languageManager.getLanguage().locale, star * 10f / 10)
        }
    }
}

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
    }
}


/**
 * 毫秒转秒
 */
fun Long?.getDuration(): Int {
    if (this == null || this <= 0) {
        return 0
    }
    return if (this % 1000f == 0f) {
        (this / 1000).toInt()
    } else {
        ((this + 1000) / 1000).toInt()
    }
}

/**
 * 秒转秒
 */
fun Int?.getDuration(): Int {
    if (this == null || this <= 0) {
        return 0
    }
    return this
}