package com.example.base.extensions

import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale


fun Long.getYYYYMMDDHHMMSSSSS(): String {
    val dateFormat = SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDD4(): String {
    val dateFormat = SimpleDateFormat("yyyyMMdd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMM3(): String {
    val dateFormat = SimpleDateFormat("yyyyMMddHHmm", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMMSS(): String {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMMSS2(): String {
    val dateFormat = SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMMSS3(): String {
    val dateFormat = SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMM(): String {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMM2(): String {
    val dateFormat = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDD(): String {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDD2(): String {
    val dateFormat = SimpleDateFormat("yyyy.MM.dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDD3(): String {
    val dateFormat = SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDD5(): String {
    val dateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMM(): String {
    val dateFormat = SimpleDateFormat("yyyy-MM", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMM2(): String {
    val dateFormat = SimpleDateFormat("yyyy.MM", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMM3(): String {
    val dateFormat = SimpleDateFormat("yyyy年MM月", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getMMDD(): String {
    val dateFormat = SimpleDateFormat("MM-dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getMMDD2(): String {
    val dateFormat = SimpleDateFormat("MM月dd日", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getMMDD3(): String {
    val dateFormat = SimpleDateFormat("MM.dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getMMDD4(): String {
    val dateFormat = SimpleDateFormat("MM/dd", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYY(): String {
    val dateFormat = SimpleDateFormat("yyyy", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getHHMMSS(): String {
    val dateFormat = SimpleDateFormat("HH:mm:ss", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getHHMM(): String {
    val dateFormat = SimpleDateFormat("HH:mm", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getDD(): String {
    val dateFormat = SimpleDateFormat("dd日", Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getYYYYMMDDHHMM(format: String): String {
    val dateFormat = SimpleDateFormat(format, Locale.CHINA)
    return dateFormat.format(this)
}

fun Long.getDate(): Date {
    return Date(this)
}

fun Long.formatWeek(): String {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = this
    return when (calendar.get(Calendar.DAY_OF_WEEK)) {
        1 -> "星期日"
        2 -> "星期一"
        3 -> "星期二"
        4 -> "星期三"
        5 -> "星期四"
        6 -> "星期五"
        7 -> "星期六"
        else -> ""
    }
}

fun Long.getMessageDate(): String {
    //所在时区时8，系统初始时间是1970-01-01 80:00:00，注意是从八点开始，计算的时候要加回去
    val offSet = Calendar.getInstance().timeZone.rawOffset
    val today = (System.currentTimeMillis() + offSet) / 86400000
    val start = (this + offSet) / 86400000
    //-2:前天,-1：昨天,0：今天,1：明天,2：后天
    val strDes = when (start - today) {
        0L -> this.getHHMM()      //今天
        -1L -> "昨天 ${this.getHHMM()}"
        else -> this.getYYYYMMDDHHMM()//直接显示时间
    }
    return strDes
}

fun Long.getCourseMMSS(): String {
    val mm = this / 60
    val ss = this % 60
    val ssStr = if (ss <= 9) "0$ss" else "$ss"
    return String.format("%2d:%s", mm, ssStr)
}

fun Long.getCourseHHMMSS(): String {

    val hour = this / 60 / 60      // watchHours秒
    val minute = (this - (hour * 60 * 60)) / 60
    val second = (this - (hour * 60 * 60)) - (minute * 60)
    val mm = this / 60
    val ss = this % 60
    val mmStr = if (mm <= 9) "0$mm" else "$mm"
    val ssStr = if (ss <= 9) "0$ss" else "$ss"
    return "${hour}小时${mmStr}分${ssStr}秒"
}


//转换文件大小
fun Long.formatFileSize(): String {
    val df = DecimalFormat("#.00")
    return when {
        this <= 0 -> "0B"
        this < 1024 -> "${this}B"
        this < 1048576 -> df.format(this / 1024.0) + "K"
        this < 1073741824 -> df.format(this / 1048576.0) + "M"
        else -> df.format(this / 1073741824.0) + "G"
    }
}

//转换文件大小
fun Long.formatFileSizeM(): String {
    val df = DecimalFormat("#.00")
    return df.format(this / (1024 * 1024.0))
}

// 转成 K、M 一位小数
fun Long.toK_M(): String {
    val df = DecimalFormat()
    df.applyPattern("#0.0")
    df.roundingMode = RoundingMode.UP

    val unit: String
    if (this in 0 until 1024 * 1024) {
        unit = "K"
        return df.format(this / 1024.0) + unit
    }
    if (this >= 1024 * 1024) {
        unit = "M"
        return df.format(this / (1024 * 1024.0)) + unit
    }
    return ""

}