package pxds.lmmd.zmwf

import com.test.basd.supercleanermax.R
import pxds.lmmd.zmwf.Khko.string
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.TimeZone

object Onaw {

    var simpleDateFormat = SimpleDateFormat(R.string.ޢޞފޓޘ_D.string())

    private val TAG = R.string.ޏޏޣހޒ_D.string()

    val THIRTY_DAYS_QUERY = 65

    val SEVEN_DAYS_QUERY = 55

    val YESTERDAY_QUERY = 25

    val TODAY_QUERY = 15

    val PAST_60_MINUS_QUERY = 5

    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()

        Egac.logDebug(TAG, R.string.ތރލހޔ_D.string((hours).toString(), (minutes).toString(), (seconds).toString()))

        return if (hours > 0) {

            String.format(R.string.ޕޞޑގޒ_D.string(), hours)
        } else if (minutes > 0) {

            String.format(R.string.ހކޤޡވ_D.string(), minutes + seconds / 60.0)
        } else {

            String.format(R.string.ޓށނއޣ_D.string(), seconds)
        }
    }

    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)

        Egac.logDebug(TAG, R.string.ހޝޔޖޑ_D.string((timestamp).toString(), (f1).toString(), (daysAgo).toString(), (f2).toString(), (flag).toString()))

        return flag
    }

    fun sevenDaysTimestamps(): Pair<Long, Long> {

        val timeZone = TimeZone.getDefault()

        val calendar = Calendar.getInstance(timeZone)

        calendar.timeInMillis = System.currentTimeMillis()
        calendar.add(Calendar.DAY_OF_YEAR, -7)

        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()

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

        return Pair(calendar.timeInMillis, endTimestamp)
    }

    fun yesterdayTimestamps(): Pair<Long, Long> {

        val timeZone = TimeZone.getDefault()

        val calendarStart = Calendar.getInstance(timeZone)
        val calendarEnd = Calendar.getInstance(timeZone)

        calendarStart.add(Calendar.DATE, -1)
        calendarEnd.add(Calendar.DATE, -1)

        calendarStart[Calendar.HOUR_OF_DAY] = 0
        calendarStart[Calendar.MINUTE] = 0
        calendarStart[Calendar.SECOND] = 0
        calendarStart[Calendar.MILLISECOND] = 0

        calendarEnd[Calendar.HOUR_OF_DAY] = 23
        calendarEnd[Calendar.MINUTE] = 59
        calendarEnd[Calendar.SECOND] = 59

        val startTimestamp = calendarStart.getTimeInMillis()
        val endTimestamp = calendarEnd.getTimeInMillis()
        Egac.logDebug(TAG, simpleDateFormat.format(startTimestamp))
        Egac.logDebug(TAG, simpleDateFormat.format(endTimestamp))
        return Pair(startTimestamp, endTimestamp)
    }

    fun thirtyDaysTimestamps(): Pair<Long, Long> {

        val currentTimeMillis = System.currentTimeMillis()

        val currentCalendar = Calendar.getInstance()
        currentCalendar.timeInMillis = currentTimeMillis

        currentCalendar.set(Calendar.HOUR_OF_DAY, 0)
        currentCalendar.set(Calendar.MINUTE, 0)
        currentCalendar.set(Calendar.SECOND, 0)
        currentCalendar.set(Calendar.MILLISECOND, 0)

        val startOfDayTimestamp = currentCalendar.timeInMillis

        val endCalendar = Calendar.getInstance()
        endCalendar.add(Calendar.DAY_OF_MONTH, -30)

        endCalendar.set(Calendar.HOUR_OF_DAY, 0)
        endCalendar.set(Calendar.MINUTE, 0)
        endCalendar.set(Calendar.SECOND, 0)
        endCalendar.set(Calendar.MILLISECOND, 0)

        val thirtyDaysAgoTimestamp = endCalendar.timeInMillis

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

    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)
        }
    }

    fun recent60Minutes(): Pair<Long, Long> {

        val currentTimestamp = System.currentTimeMillis()

        val sixtyMinutesAgoTimestamp = currentTimestamp - (60 * 60 * 1000)

        return Pair(sixtyMinutesAgoTimestamp, currentTimestamp)
    }

    fun todayTimestamps(): Pair<Long, Long> {

        val timeZone = TimeZone.getDefault()

        val calendar = Calendar.getInstance(timeZone)

        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)

        if (calendar.before(Calendar.getInstance(timeZone))) {
            calendar.add(Calendar.DAY_OF_YEAR, -1)
        }

        val startOfDayTimestamp = calendar.timeInMillis

        val currentTimestamp = System.currentTimeMillis()

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

        return Pair(startOfDayTimestamp, currentTimestamp)
    }
}