package com.gh.gamecenter.core.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.log10

object TimeUtils {

    /**
     * 获取今天日期
     */
    @JvmStatic
    fun getToday(): String {
        var date = Date()
        val calendar = GregorianCalendar()
        calendar.time = date
        //把日期往后增加一天.整数往后推,负数往前移动
        calendar.add(GregorianCalendar.DATE, 0)
        date = calendar.time
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        return formatter.format(date)
    }

    /**
     * 获取昨天日期
     */
    @JvmStatic
    fun getYesterday(): String {
        var date = Date()
        val calendar = GregorianCalendar()
        calendar.time = date
        calendar.add(GregorianCalendar.DATE, -1)
        date = calendar.time
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        return formatter.format(date)
    }

    @JvmStatic
    fun getFormatTime(time: Long, pattern: String = "yyyy-MM-dd"): String {
        val f = SimpleDateFormat(pattern, Locale.CHINA)
        f.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
        return f.format(Date(getJavaTimestamp(time)))
    }

    fun getFormatDate(timestamp: Long): String {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        format.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
        val testDate: String
        val today = format.parse(format.format(Date())).time
        val day = timestamp * 1000
        val timeFormat = SimpleDateFormat("HH:mm", Locale.CHINA)
        val time = timeFormat.format(day)

        testDate = if (day >= today && day < today + 86400 * 1000) {
            "今天 $time"
        } else if (day >= today + 86400 * 1000 && day < today + 86400 * 1000 * 2) {
            "明天 $time"
        } else {
            SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(day)
        }
        return testDate
    }

    //获取当前的小时（24小时制）
    fun getCurrentHour(): Int {
        val calendar = Calendar.getInstance()
        return calendar.get(Calendar.HOUR_OF_DAY)
    }

    //判断是不是今天
    fun isToday(timestamp: Long): Boolean {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        format.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
        val today = format.parse(format.format(Date())).time
        val day = timestamp * 1000
        if (day >= today && day < today + 86400 * 1000) {
            return true
        }
        return false
    }

    //判断是不是明天
    fun isTomorrow(timestamp: Long): Boolean {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        format.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
        val today = format.parse(format.format(Date())).time
        val day = timestamp * 1000
        if (day >= today + 86400 * 1000 && day < today + 86400 * 1000 * 2) {
            return true
        }
        return false
    }

    //判断是不是本周
    fun isThisWeek(timestamp: Long): Boolean {
        val calendar = Calendar.getInstance()
        val currentWeek = calendar[Calendar.WEEK_OF_YEAR]
        calendar.time = Date(timestamp)
        val paramWeek = calendar[Calendar.WEEK_OF_YEAR]
        return paramWeek == currentWeek
    }

    /**
     * 判断时间戳是多少天前
     */
    fun getBeforeDays(timestamp: Long): Int {
        var days: Long = 0
        val format = SimpleDateFormat("yyyyMMdd HH:mm", Locale.getDefault())
        try {
            val today = format.parse(format.format(Date())).time
            val day = timestamp * 1000
            days = (today - day) / 86400000
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return days.toInt()
    }

    /**
     * 判断传入时间戳与当前日期的差值，返回单位为天
     */
    fun getDaysOffset(timestamp: Long): Int {
        var days: Long = 0
        val format = SimpleDateFormat("yyyyMMdd", Locale.getDefault())
        try {
            val today = format.parse(format.format(Date())).time
            val day = timestamp * 1000
            days = (day - today) / 86400000
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return days.toInt()
    }


    /**
     * 格式化视频时长
     */
    @JvmStatic
    fun formatVideoDuration(length: Long): String {
        val minute = length / 60
        val second = length % 60
        return String.format(Locale.CHINA, "%02d:%02d", minute, second)
    }

    /**
     * 格式化时长（将秒数转化为HH:mm:ss格式）
     */
    @JvmStatic
    fun formatDuration(seconds: Long): String {
        val hour = seconds / 3600
        val minute = seconds % 3600 / 60
        val second = seconds % 60
        return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, minute, second)
    }

    @JvmStatic
    fun getDayString(date: Long): String {
        val offSet = Calendar.getInstance().timeZone.rawOffset
        val today = (System.currentTimeMillis() + offSet) / 86400000
        val start = (date * 1000 + offSet) / 86400000
        return when ((start - today).toInt()) {
            -2 -> "（前天）"
            -1 -> "（昨天）"
            0 -> "（今天）"
            1 -> "（明天）"
            2 -> "（后天）"
            else -> ""
        }
    }

    @JvmStatic
    fun getStartTimeOfToday(): Long = getStartTimeOfDay(System.currentTimeMillis())

    @JvmStatic
    fun getStartTimeOfDay(currentTime: Long): Long {
        val calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"))
        calendar.timeInMillis = getJavaTimestamp(currentTime)
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        return calendar.timeInMillis
    }

    @JvmStatic
    fun getTimeOfToday(hour: Int) = getTimeOfToday(hour, 0)

    @JvmStatic
    fun getTimeOfToday(hour: Int, minute: Int): Long {
        val calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"))
        calendar.timeInMillis = getJavaTimestamp(System.currentTimeMillis())
        calendar.set(Calendar.HOUR_OF_DAY, hour)
        calendar.set(Calendar.MINUTE, minute)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        return calendar.timeInMillis
    }

    @JvmStatic
    fun getJavaTimestamp(timestamp: Long): Long {
        return if ((log10(timestamp.toDouble()) + 1).toInt() == 10) {
            timestamp * 1000
        } else {
            timestamp
        }
    }
}