package com.adealink.weparty.commonui.util

import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.locale.language.data.Language
import com.adealink.frame.locale.language.languageManager
import com.adealink.weparty.R
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

private val monthEn = arrayOf(
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sept",
    "Oct",
    "Nov",
    "Dec"
)

private val monthAr = arrayOf(
    "يناير",
    "فبراير",
    "مارس",
    "أبريل",
    "مايو",
    "يونيو",
    "يوليو",
    "أغسطس",
    "سبتمبر",
    "اكتوبر",
    "نوفمبر",
    "ديسمبر",
)

//阿语0-9
//private val arNum = arrayOf("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩")

fun getDateTimeFromToStr(fromMs: Long, toMs: Long): String {
    val language = languageManager?.getLanguageCode() ?: Language.EN.code
    return getCompatString(
        R.string.commonui_date_from_to,
        getTimeStr(fromMs, language),
        getTimeStr(toMs, language)
    )
}

private val DDFormat = SimpleDateFormat("d", Locale.ENGLISH)
private val HmFormat = SimpleDateFormat("HH:mm", Locale.ENGLISH)

private fun getTimeStr(timeMs: Long, language: String): String {
    val date = Date(timeMs)
    val calendar = Calendar.getInstance().apply {
        time = date
    }
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_WEEK)
    return getCompatString(
        R.string.commonui_date_mmddhM,
        getMonthStr(month, language),
        getDayStr(date, day, language),
        getHMStr(date)
    )
}

private fun getMonthStr(month: Int, language: String): String {
    return if (language == Language.AR.code) {
        monthAr[month]
    } else {
        monthEn[month]
    }
}

private fun getDayStr(date: Date, day: Int, language: String): String {
    return if (language == Language.AR.code) {
        DDFormat.format(date)
    } else {
        val dayStr = DDFormat.format(date)
        val daySuffix = when (day % 10) {
            1 -> {
                "st"
            }

            2 -> {
                "nd"
            }

            3 -> {
                "rd"
            }

            else -> {
                "th"
            }
        }
        dayStr + daySuffix
    }
}

private fun getHMStr(date: Date): String {
    return HmFormat.format(date)
}

fun formatSecondsTo00(timeSeconds: Int): String {
    val second = timeSeconds % 60
    val minuteTemp = timeSeconds / 60
    if (minuteTemp > 0) {
        val minute = minuteTemp % 60
        val hour = minuteTemp / 60
        return if (hour > 0) {
            ((if (hour >= 10) (hour.toString() + "") else ("0$hour")) + ":" + (if (minute >= 10) (minute.toString() + "") else ("0$minute")) + ":"
                    + (if (second >= 10) (second.toString() + "") else ("0$second")))
        } else {
            (if (minute >= 10) (minute.toString() + "") else ("0$minute")) + ":" + (if (second >= 10) (second.toString() + "") else ("0$second"))
        }
    } else {
        return "00:" + (if (second >= 10) (second.toString() + "") else ("0$second"))
    }
}