package com.fabledsword.minstrel.theme

import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.googlefonts.Font
import androidx.compose.ui.text.googlefonts.GoogleFont
import androidx.compose.ui.unit.sp
import com.fabledsword.minstrel.R

/**
 * Google Fonts provider — fetches font files via Play Services Fonts at
 * runtime, caches them across launches. Matches the Flutter client's
 * `google_fonts` package behaviour (no bundled .ttf files in either tree).
 *
 * Per FabledSword design system:
 *   - Fraunces — display + headline (mythic serif)
 *   - Inter — body + label (clean sans for UI text)
 *   - JetBrains Mono — technical / monospace
 * Weights are restricted to 400 (regular) and 500 (medium) only.
 */
private val GoogleFontProvider = GoogleFont.Provider(
    providerAuthority = "com.google.android.gms.fonts",
    providerPackage = "com.google.android.gms",
    certificates = R.array.com_google_android_gms_fonts_certs,
)

private val FrauncesFont = GoogleFont("Fraunces")
private val InterFont = GoogleFont("Inter")
private val JetBrainsMonoFont = GoogleFont("JetBrains Mono")

private val Fraunces = FontFamily(
    Font(
        googleFont = FrauncesFont,
        fontProvider = GoogleFontProvider,
        weight = FontWeight.W400,
        style = FontStyle.Normal,
    ),
    Font(
        googleFont = FrauncesFont,
        fontProvider = GoogleFontProvider,
        weight = FontWeight.W500,
        style = FontStyle.Normal,
    ),
)

private val Inter = FontFamily(
    Font(
        googleFont = InterFont,
        fontProvider = GoogleFontProvider,
        weight = FontWeight.W400,
        style = FontStyle.Normal,
    ),
    Font(
        googleFont = InterFont,
        fontProvider = GoogleFontProvider,
        weight = FontWeight.W500,
        style = FontStyle.Normal,
    ),
)

private val JetBrainsMono = FontFamily(
    Font(
        googleFont = JetBrainsMonoFont,
        fontProvider = GoogleFontProvider,
        weight = FontWeight.W400,
        style = FontStyle.Normal,
    ),
)

/**
 * FabledSword Material 3 Typography mapping. Display/headline slots use
 * Fraunces; title/body/label use Inter; the smallest label slots step down
 * to JetBrains Mono for technical/code surfaces. Sizes follow Material 3
 * defaults; the project's "Fraunces ≥18px" rule is satisfied because every
 * Fraunces-bearing slot is 18sp or larger.
 */
val MinstrelTypography = Typography(
    displayLarge   = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 32.sp),
    displayMedium  = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 28.sp),
    displaySmall   = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 24.sp),
    headlineLarge  = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 22.sp),
    headlineMedium = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 20.sp),
    headlineSmall  = TextStyle(fontFamily = Fraunces, fontWeight = FontWeight.W500, fontSize = 18.sp),
    titleLarge     = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W500, fontSize = 18.sp),
    titleMedium    = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W500, fontSize = 16.sp),
    titleSmall     = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W500, fontSize = 14.sp),
    bodyLarge      = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W400, fontSize = 16.sp),
    bodyMedium     = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W400, fontSize = 14.sp),
    bodySmall      = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W400, fontSize = 12.sp),
    labelLarge     = TextStyle(fontFamily = Inter,    fontWeight = FontWeight.W500, fontSize = 14.sp),
    labelMedium    = TextStyle(fontFamily = JetBrainsMono, fontWeight = FontWeight.W400, fontSize = 12.sp),
    labelSmall     = TextStyle(fontFamily = JetBrainsMono, fontWeight = FontWeight.W400, fontSize = 11.sp),
)
