package com.applemand.zmanim.core.jewcal

import com.kosherjava.zmanim.hebrewcalendar.Daf
import com.kosherjava.zmanim.hebrewcalendar.YomiCalculator
import com.kosherjava.zmanim.hebrewcalendar.YerushalmiYomiCalculator
import kotlinx.datetime.LocalDate

data class DafYomiRef(
    val masechtaEn: String,
    val masechtaHe: String,
    val daf: Int,
    val amud: Amud,
) {
    enum class Amud { A, B }

    val dafHe: String get() = HebrewNumerals.format(daf)
    val displayEn: String get() = "$masechtaEn $daf${amud.name.lowercase()}"
    val displayHe: String get() = "$masechtaHe $dafHe"
}

class DafYomiCalculator {

    fun bavli(date: LocalDate): DafYomiRef? {
        val jc = date.toJewishCalendar()
        val daf: Daf = runCatching { YomiCalculator.getDafYomiBavli(jc) }.getOrNull() ?: return null
        return DafYomiRef(
            masechtaEn = BavliMasechetNames.en(daf.masechtaNumber),
            masechtaHe = BavliMasechetNames.he(daf.masechtaNumber),
            daf = daf.daf,
            // KosherJava Daf exposes page number; amud a/b is not carried — render 'a' for display
            // parity with getDafYomi webapp function which defaults to 'a'.
            amud = DafYomiRef.Amud.A,
        )
    }

    fun yerushalmi(date: LocalDate): DafYomiRef? {
        val jc = date.toJewishCalendar()
        val daf: Daf = runCatching { YerushalmiYomiCalculator.getDafYomiYerushalmi(jc) }.getOrNull() ?: return null
        return DafYomiRef(
            masechtaEn = YerushalmiMasechetNames.en(daf.masechtaNumber),
            masechtaHe = YerushalmiMasechetNames.he(daf.masechtaNumber),
            daf = daf.daf,
            amud = DafYomiRef.Amud.A,
        )
    }
}

/**
 * Masechet names for Talmud Bavli in KosherJava's 0-based ordering.
 */
internal object BavliMasechetNames {
    private val en = arrayOf(
        "Berachot", "Shabbat", "Eruvin", "Pesachim", "Shekalim", "Yoma", "Sukkah", "Beitzah",
        "Rosh Hashanah", "Taanit", "Megillah", "Moed Katan", "Chagigah", "Yevamot", "Ketubot",
        "Nedarim", "Nazir", "Sotah", "Gittin", "Kiddushin", "Bava Kamma", "Bava Metzia",
        "Bava Batra", "Sanhedrin", "Makkot", "Shevuot", "Avodah Zarah", "Horayot", "Zevachim",
        "Menachot", "Chullin", "Bekhorot", "Arachin", "Temurah", "Keritot", "Meilah",
        "Kinnim", "Tamid", "Midot", "Niddah",
    )
    private val he = arrayOf(
        "ברכות", "שבת", "עירובין", "פסחים", "שקלים", "יומא", "סוכה", "ביצה",
        "ראש השנה", "תענית", "מגילה", "מועד קטן", "חגיגה", "יבמות", "כתובות",
        "נדרים", "נזיר", "סוטה", "גיטין", "קידושין", "בבא קמא", "בבא מציעא",
        "בבא בתרא", "סנהדרין", "מכות", "שבועות", "עבודה זרה", "הוריות", "זבחים",
        "מנחות", "חולין", "בכורות", "ערכין", "תמורה", "כריתות", "מעילה",
        "קינים", "תמיד", "מידות", "נידה",
    )
    fun en(idx: Int): String = en.getOrElse(idx) { "Masechet $idx" }
    fun he(idx: Int): String = he.getOrElse(idx) { "מסכת $idx" }
}

internal object YerushalmiMasechetNames {
    private val en = arrayOf(
        "Berachot", "Pe'ah", "Demai", "Kilayim", "Shevi'it", "Terumot", "Ma'asrot", "Ma'aser Sheni",
        "Chalah", "Orlah", "Bikkurim", "Shabbat", "Eruvin", "Pesachim", "Beitzah", "Rosh Hashanah",
        "Yoma", "Sukkah", "Taanit", "Shekalim", "Megilah", "Chagigah", "Moed Katan", "Yevamot",
        "Ketubot", "Sotah", "Nedarim", "Nazir", "Gittin", "Kiddushin", "Bava Kamma", "Bava Metzia",
        "Bava Batra", "Shevuot", "Makkot", "Sanhedrin", "Avodah Zarah", "Horayot", "Niddah",
    )
    private val he = arrayOf(
        "ברכות", "פאה", "דמאי", "כלאים", "שביעית", "תרומות", "מעשרות", "מעשר שני",
        "חלה", "ערלה", "ביכורים", "שבת", "עירובין", "פסחים", "ביצה", "ראש השנה",
        "יומא", "סוכה", "תענית", "שקלים", "מגילה", "חגיגה", "מועד קטן", "יבמות",
        "כתובות", "סוטה", "נדרים", "נזיר", "גיטין", "קידושין", "בבא קמא", "בבא מציעא",
        "בבא בתרא", "שבועות", "מכות", "סנהדרין", "עבודה זרה", "הוריות", "נידה",
    )
    fun en(idx: Int): String = en.getOrElse(idx) { "Masechet $idx" }
    fun he(idx: Int): String = he.getOrElse(idx) { "מסכת $idx" }
}
