package com.xty.network.utils

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

object TimeTransformUtils {

    fun calculateStartTimeAndEndTime(targetTime: String): Pair<String, String> {
        val targetTimeObj = SimpleDateFormat("HH:mm", Locale.getDefault()).parse(targetTime)
        val calendar = Calendar.getInstance()
        calendar.time = targetTimeObj

        val targetMinutesLastDigit = calendar.get(Calendar.MINUTE) % 10

        // Calculate the start time
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, targetMinutesLastDigit)
        val startTime = SimpleDateFormat("HH:mm", Locale.getDefault()).format(calendar.time)

        // Calculate the end time as the last occurrence of the target minutes last digit
        calendar.set(Calendar.HOUR_OF_DAY, 23)
        calendar.set(Calendar.MINUTE, 59 - (9 - targetMinutesLastDigit)) // Calculate end minute
        val endTime = SimpleDateFormat("HH:mm", Locale.getDefault()).format(calendar.time)

        return Pair(startTime, endTime)
    }

    fun calculateStartTimeAndEndTime(timestamp: Long): Pair<Date, Date> {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timestamp

        val targetMinutesLastDigit = calendar.get(Calendar.MINUTE) % 10

        // Calculate the start time
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, targetMinutesLastDigit)
        val startTime = calendar.time

        // Calculate the end time as the last occurrence of the target minutes last digit
        calendar.set(Calendar.HOUR_OF_DAY, 23)
        calendar.set(Calendar.MINUTE, 59 - (9 - targetMinutesLastDigit)) // Calculate end minute
        val endTime = calendar.time

        return Pair(startTime, endTime)
    }
}