package com.remax.notification.utils

import java.util.Calendar
import kotlin.arrayOf
import kotlin.math.abs



object TimeCheckUtil {
    private var mCurrentHour = 0

    private val TARGET_TIMES = arrayOf<IntArray>(
        intArrayOf(9, 10),
        intArrayOf(12, 10),
        intArrayOf(13, 10),
        intArrayOf(19, 10),
        intArrayOf(21, 0),
        intArrayOf(22, 30),
        intArrayOf(23, 30),
    )

    fun isTargetTime(): Boolean {
        val calendar = Calendar.getInstance()
        val currentHour = calendar.get(Calendar.HOUR_OF_DAY) // 24小时制
        val currentMinute = calendar.get(Calendar.MINUTE)

        for (time in TARGET_TIMES) {
            val targetHour = time[0]
            val targetMinute = time[1]
            if (currentHour == targetHour && currentMinute == targetMinute) {
                return true
            }
        }
        return false
    }

    fun isDifferentHour(): Boolean {
        val calendar = Calendar.getInstance()
        val currentHour = calendar.get(Calendar.HOUR_OF_DAY)

        if (currentHour != mCurrentHour) {
            mCurrentHour = currentHour
            return true
        }

        return false
    }


    fun isTargetTimeWithTolerance(tolerance: Int): Boolean {
        val calendar = Calendar.getInstance()
        val currentHour = calendar.get(Calendar.HOUR_OF_DAY)
        val currentMinute = calendar.get(Calendar.MINUTE)

        for (time in TARGET_TIMES) {
            val targetHour = time[0]
            val targetMinute = time[1]
            val diff = abs((currentHour * 60 + currentMinute) - (targetHour * 60 + targetMinute))
            if (diff <= tolerance) {
                return true
            }
        }
        return false
    }
}