package com.safecheckin.app.utils

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit

object TimeUtils {

    fun getStartOfDay(timestamp: Long = System.currentTimeMillis()): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timestamp
        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
    }

    fun getEndOfDay(timestamp: Long = System.currentTimeMillis()): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = getStartOfDay(timestamp)
        calendar.add(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    fun formatDate(timestamp: Long, pattern: String = "yyyy-MM-dd"): String {
        return SimpleDateFormat(pattern, Locale.getDefault()).format(Date(timestamp))
    }

    fun formatTime(timestamp: Long, pattern: String = "HH:mm"): String {
        return SimpleDateFormat(pattern, Locale.getDefault()).format(Date(timestamp))
    }

    fun formatDateTime(timestamp: Long): String {
        return SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).format(Date(timestamp))
    }

    fun getRelativeTimeString(timestamp: Long): String {
        val now = System.currentTimeMillis()
        val diff = now - timestamp

        return when {
            diff < TimeUnit.MINUTES.toMillis(1) -> "刚刚"
            diff < TimeUnit.HOURS.toMillis(1) -> "${TimeUnit.MILLISECONDS.toMinutes(diff)}分钟前"
            diff < TimeUnit.DAYS.toMillis(1) -> "${TimeUnit.MILLISECONDS.toHours(diff)}小时前"
            diff < TimeUnit.DAYS.toMillis(7) -> "${TimeUnit.MILLISECONDS.toDays(diff)}天前"
            else -> formatDate(timestamp)
        }
    }

    fun getHoursUntilWarning(lastCheckIn: Long, gracePeriodHours: Int): Int {
        val warningTime = lastCheckIn + TimeUnit.HOURS.toMillis(gracePeriodHours.toLong())
        val remaining = warningTime - System.currentTimeMillis()
        return if (remaining > 0) {
            TimeUnit.MILLISECONDS.toHours(remaining).toInt()
        } else {
            0
        }
    }

    fun isSameDay(timestamp1: Long, timestamp2: Long): Boolean {
        val cal1 = Calendar.getInstance().apply { timeInMillis = timestamp1 }
        val cal2 = Calendar.getInstance().apply { timeInMillis = timestamp2 }
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
    }

    fun getConsecutiveDays(checkInDates: List<Long>): Int {
        if (checkInDates.isEmpty()) return 0

        val sortedDates = checkInDates.sortedDescending()
        var consecutiveDays = 1
        var currentDate = getStartOfDay(sortedDates.first())

        for (i in 1 until sortedDates.size) {
            val prevDay = currentDate - TimeUnit.DAYS.toMillis(1)
            val checkInDay = getStartOfDay(sortedDates[i])

            if (checkInDay == prevDay) {
                consecutiveDays++
                currentDate = prevDay
            } else if (checkInDay < prevDay) {
                break
            }
        }

        return consecutiveDays
    }
}