package com.xhly.corelib.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.icu.math.BigDecimal
import android.util.Base64
import java.lang.reflect.ParameterizedType
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date

fun <T> getClass(t: Any): Class<T> {
    // 通过反射 获取父类泛型 (T) 对应 Class类
    return (t.javaClass.genericSuperclass as ParameterizedType)
        .actualTypeArguments[0]
            as Class<T>
}

fun convertBase64ToPic(base64: String): Bitmap? {
    var value = base64
    if (base64.contains(",")) {
        value = base64.split(",")[1]
    }
    val decode = Base64.decode(value, Base64.DEFAULT)
    return BitmapFactory.decodeByteArray(decode, 0, decode.size)
}

fun Date.toHourDate(format: String = "yyyy-MM-dd HH:mm:ss"): String {
    try {
        var format = SimpleDateFormat(format)
        return format.format(this)
    } catch (e: Exception) {
        return this.toString()
    }
}

fun Long.toFormat(format: String = "yyyy-MM-dd HH:mm:ss"): String {
    try {
        var format = SimpleDateFormat(format)
        return format.format(this)
    } catch (e: Exception) {
        return this.toString()
    }
}

fun String.hourToCurrentDate(formatType: String = "HH:mm:ss"): Date {
    try {
        val simpleDateFormat = SimpleDateFormat(formatType)
        val parse = simpleDateFormat.parse(this)
        val hours = parse.hours
        val minutes = parse.minutes
        val seconds = parse.seconds
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH))
        calendar.set(Calendar.HOUR_OF_DAY, hours)
        calendar.set(Calendar.MINUTE, minutes)
        calendar.set(Calendar.SECOND, seconds)
        return calendar.time
    } catch (e: Exception) {
        return Date()
    }
}

fun String.hourToCurrentDateLong(formatType: String = "HH:mm"): Long {
    try {
        val simpleDateFormat = SimpleDateFormat(formatType)
        val parse = simpleDateFormat.parse(this)
        val calendar = Calendar.getInstance()
        val year= calendar.get(Calendar.YEAR)
        val day = calendar.get(Calendar.DAY_OF_YEAR)
        calendar.timeInMillis=parse.time
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.DAY_OF_YEAR,day)
        return calendar.time.time
    } catch (e: Exception) {
        return Date().time
    }
}

fun Date.getBetweenDay(date: Date): String {
    try {
        var l = (this.time - date.time) / 1000 / 60 / 60 / 24
        return l.toInt().toString()
    } catch (e: Exception) {
        return "0"
    }
}


fun String?.NoEmpty(): String {
    if (this == null) {
        return ""
    } else {
        return this
    }
}

fun Any.LogTag(): String {
    return this.javaClass.name
}

fun Any.LogShow(s: String) {
    LogUtils.d(LogTag(), s)
}

fun Double.getScaleSize(
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): Double {
    return BigDecimal(this.toString()).setScale(DECIMAL_POINT_NUMBER, roundType).toDouble()
}

fun Double.getScaleSizeString(
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    return BigDecimal(this.toString()).setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
        .stripTrailingZeros().toPlainString()
}

fun String.getScaleSizeString(
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this).setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return this
    }
}

fun String.getBigNumString(): String {
    try {
        return BigDecimal(this).toBigDecimal().stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return this
    }
}

fun Double.getBigNumString(): String {
    try {
        return BigDecimal(this).toBigDecimal().stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return this.toString()
    }
}

//对比double大小
fun String.getContrastDouble(
    s: String
): Int {
    return try {
        BigDecimal(this).compareTo(BigDecimal(s))
    } catch (e: Exception) {
        0
    }
}

fun Double.getMulString(
    d: Double,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this.toString()).multiply(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}

fun Double.getSubString(
    s: String,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    return try {
        BigDecimal(this.toString()).subtract(BigDecimal(s))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        "0"
    }
}

fun Double.getMulString(
    d: String,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this.toString()).multiply(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}

fun String.getMulString(
    d: String,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this).multiply(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}


fun Double.getDivString(
    d: Double,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this.toString()).divide(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}


fun String.getDivString(
    d: String,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this).divide(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}

fun Double.getDivString(
    d: String,
    DECIMAL_POINT_NUMBER: Int = 2,
    roundType: Int = BigDecimal.ROUND_DOWN
): String {
    try {
        return BigDecimal(this.toString()).divide(BigDecimal(d))
            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
            .stripTrailingZeros().toPlainString()
    } catch (e: Exception) {
        return "0"
    }
}

fun Double.getPercentageString(
): String {
    val formatter = DecimalFormat("#.##%")
    return formatter.format(this)
}

fun Context.getVersionName(): String {
    var packageManager = this.packageManager
    var packageInfo = packageManager.getPackageInfo(this.packageName, 0)
    return packageInfo.versionName
}

//判断是否是链接
fun String.isUrl(): Boolean {
    var rgex = "^http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?".toRegex()
    return rgex.containsMatchIn(this)
}

//移除标点
fun String.removePunctuation(): String {
    return try {
        if (this.isNotBlank()) {
            this.replace(Regex("[^\\w\\s]"), "")
        } else {
            ""
        }
    } catch (e: java.lang.Exception) {
        ""
    }
}