package com.test.basd.supercleanermax.helps

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.TimeZone

object TimeUtils {

    private val TAG = "TimeUtils"

    val PAST_60_MINUS_QUERY = 5
    val TODAY_QUERY = 15
    val YESTERDAY_QUERY = 25
    val SEVEN_DAYS_QUERY = 55
    val THIRTY_DAYS_QUERY = 65

    fun timePair(queryTime: Int): Pair<Long, Long> {
        return when (queryTime) {
            PAST_60_MINUS_QUERY -> recent60Minutes()
            TODAY_QUERY -> todayTimestamps()
            YESTERDAY_QUERY -> yesterdayTimestamps()
            SEVEN_DAYS_QUERY -> sevenDaysTimestamps()
            THIRTY_DAYS_QUERY -> thirtyDaysTimestamps()
            else -> Pair(0, 0)
        }
    }

    var simpleDateFormat = SimpleDateFormat("M-d-yyyy HH:mm:ss")
    fun isDaysAgo(timestamp: Long, days: Int): Boolean {
        // 获取当前时间戳
        val currentTime = System.currentTimeMillis()

        val daysAgo = currentTime - days * 24 * 60 * 60 * 1000L
        val flag = timestamp < daysAgo

        val f1 = simpleDateFormat.format(timestamp)
        val f2 = simpleDateFormat.format(daysAgo)

        LogEx.logDebug(TAG, "timestamp=$timestamp f1=$f1 daysAgo=$daysAgo f2=$f2 flag=$flag")

        //几天前
        return flag
    }


    //过去一小时
    fun recent60Minutes(): Pair<Long, Long> {
        // 获取当前时间的时间戳
        val currentTimestamp = System.currentTimeMillis()

        // 计算前60分钟的时间戳
        val sixtyMinutesAgoTimestamp = currentTimestamp - (60 * 60 * 1000)

        // 返回时间戳对
        return Pair(sixtyMinutesAgoTimestamp, currentTimestamp)
    }

    //今天时间戳
    fun todayTimestamps(): Pair<Long, Long> {
        // 获取系统默认时区
        val timeZone = TimeZone.getDefault()
        // 创建 Calendar 实例，并设置时区
        val calendar = Calendar.getInstance(timeZone)

        // 设置 Calendar 为今天的00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)

        // 检查是否已经是新的一天，如果是，则设置为昨天的00:00:00，以便减去一天
        if (calendar.before(Calendar.getInstance(timeZone))) {
            calendar.add(Calendar.DAY_OF_YEAR, -1)
        }

        // 获取今天的起始时间戳
        val startOfDayTimestamp = calendar.timeInMillis

        // 获取当前时间的时间戳
        val currentTimestamp = System.currentTimeMillis()

        LogEx.logDebug(TAG, simpleDateFormat.format(startOfDayTimestamp))
        LogEx.logDebug(TAG, simpleDateFormat.format(currentTimestamp))

        // 返回时间戳对
        return Pair(startOfDayTimestamp, currentTimestamp)
    }


    //昨天时间戳
    fun yesterdayTimestamps(): Pair<Long, Long> {
        // 获取系统默认时区
        val timeZone = TimeZone.getDefault()
        // 创建 Calendar 实例，并设置时区
        val calendarStart = Calendar.getInstance(timeZone)
        val calendarEnd = Calendar.getInstance(timeZone)

        // 设置为昨天的日期
        calendarStart.add(Calendar.DATE, -1)
        calendarEnd.add(Calendar.DATE, -1)

        // 设置起始时间为昨天的00:00:00
        calendarStart[Calendar.HOUR_OF_DAY] = 0
        calendarStart[Calendar.MINUTE] = 0
        calendarStart[Calendar.SECOND] = 0
        calendarStart[Calendar.MILLISECOND] = 0

        // 设置结束时间为昨天的23:59:59
        calendarEnd[Calendar.HOUR_OF_DAY] = 23
        calendarEnd[Calendar.MINUTE] = 59
        calendarEnd[Calendar.SECOND] = 59

        // 获取昨天的起始和结束时间戳
        val startTimestamp = calendarStart.getTimeInMillis()
        val endTimestamp = calendarEnd.getTimeInMillis()
        LogEx.logDebug(TAG, simpleDateFormat.format(startTimestamp))
        LogEx.logDebug(TAG, simpleDateFormat.format(endTimestamp))
        return Pair(startTimestamp, endTimestamp)
    }

    //过去7天时间戳
    fun sevenDaysTimestamps(): Pair<Long, Long> {
        // 获取系统默认时区
        val timeZone = TimeZone.getDefault()
        // 创建 Calendar 实例，并设置时区
        val calendar = Calendar.getInstance(timeZone)

        // 设置 Calendar 为当前时间，然后减去 7 天得到七天前的日期
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.add(Calendar.DAY_OF_YEAR, -7)

        // 设置七天前的起始时间戳为七天前的00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)

        // 当前的终止时间戳
        val endTimestamp = System.currentTimeMillis()

        LogEx.logDebug(TAG, simpleDateFormat.format(calendar.timeInMillis))
        LogEx.logDebug(TAG, simpleDateFormat.format(endTimestamp))

        // 返回时间戳对
        return Pair(calendar.timeInMillis, endTimestamp)
    }

    fun thirtyDaysTimestamps(): Pair<Long, Long> {
        // 获取当前时间的毫秒时间戳
        val currentTimeMillis = System.currentTimeMillis()

        // 创建 Calendar 实例以获取当前时间
        val currentCalendar = Calendar.getInstance()
        currentCalendar.timeInMillis = currentTimeMillis

        // 将 Calendar 的小时、分钟、秒和毫秒设置为0，确保是当天的开始
        currentCalendar.set(Calendar.HOUR_OF_DAY, 0)
        currentCalendar.set(Calendar.MINUTE, 0)
        currentCalendar.set(Calendar.SECOND, 0)
        currentCalendar.set(Calendar.MILLISECOND, 0)

        // 获取当前时间戳对应的00:00:00的时间戳
        val startOfDayTimestamp = currentCalendar.timeInMillis

        // 设置结束时间为当前时间戳减去30天的天数
        val endCalendar = Calendar.getInstance()
        endCalendar.add(Calendar.DAY_OF_MONTH, -30)

        // 将结束时间的小时、分钟、秒和毫秒设置为0，确保是30天前的开始
        endCalendar.set(Calendar.HOUR_OF_DAY, 0)
        endCalendar.set(Calendar.MINUTE, 0)
        endCalendar.set(Calendar.SECOND, 0)
        endCalendar.set(Calendar.MILLISECOND, 0)

        // 获取30天前00:00:00的时间戳
        val thirtyDaysAgoTimestamp = endCalendar.timeInMillis

        LogEx.logDebug(TAG, simpleDateFormat.format(thirtyDaysAgoTimestamp))
        LogEx.logDebug(TAG, simpleDateFormat.format(currentTimeMillis))
        return Pair(thirtyDaysAgoTimestamp, currentTimeMillis)
    }

    fun longToHoursAndMinutes(timestamp: Long): String {
        // 将毫秒转换为秒
        var totalSeconds: Double = timestamp / 1000.0
        // 计算小时、分钟、秒
        val hours = (totalSeconds / 3600).toLong().toFloat()
        totalSeconds %= 3600.0
        val minutes = (totalSeconds / 60).toLong().toFloat()
        val seconds = (totalSeconds % 60).toFloat()

        LogEx.logDebug(TAG, "hours=$hours minutes=$minutes seconds=$seconds")
        // 根据最大的时间单位返回格式化的时间字符串
        return if (hours > 0) {
            // 如果有小时，则格式化为小时，保留一位小数
            String.format("%.1fh", hours)
        } else if (minutes > 0) {
            // 如果没有小时但有分钟，则格式化为分钟，保留一位小数
            String.format("%.1fm", minutes + seconds / 60.0)
        } else {
            // 如果既没有小时也没有分钟，则格式化为秒，保留一位小数
            String.format("%.1fs", seconds)
        }
    }
}