"""
Site fonts, chosen in Admin → Site settings → Fonts.

Each slot (English heading/body/accent/label + Bangla heading/body/UI) can be switched
to any font in the catalog below. All are free Google Fonts. To offer another font,
add it to the matching list with its Google Fonts weights.
"""
from django.utils.html import format_html
from django.utils.safestring import mark_safe

# name: Google Fonts "family=" spec (weights to load)
CATALOG = {
    # English
    'Playfair Display': 'Playfair+Display:ital,wght@0,400;0,700;0,900;1,400;1,700',
    'DM Serif Display': 'DM+Serif+Display:ital@0;1',
    'Libre Baskerville': 'Libre+Baskerville:ital,wght@0,400;0,700;1,400',
    'Lora': 'Lora:ital,wght@0,400;0,600;0,700;1,400',
    'Merriweather': 'Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,400',
    'Crimson Pro': 'Crimson+Pro:ital,wght@0,300;0,400;0,600;1,400',
    'EB Garamond': 'EB+Garamond:ital,wght@0,400;0,600;1,400',
    'Source Serif 4': 'Source+Serif+4:ital,wght@0,300;0,400;0,600;1,400',
    'Cormorant Garamond': 'Cormorant+Garamond:ital,wght@0,300;0,400;1,300;1,400',
    'Inter': 'Inter:wght@300;400;500;600;700',
    'Bebas Neue': 'Bebas+Neue',
    'Oswald': 'Oswald:wght@400;500;600',
    'Barlow Condensed': 'Barlow+Condensed:wght@400;500;600',
    'Anton': 'Anton',
    # Bangla
    'Noto Serif Bengali': 'Noto+Serif+Bengali:wght@400;500;600;700;900',
    'Noto Sans Bengali': 'Noto+Sans+Bengali:wght@400;500;600;700',
    'Tiro Bangla': 'Tiro+Bangla:ital@0;1',
    'Anek Bangla': 'Anek+Bangla:wght@400;500;600;700',
    'Hind Siliguri': 'Hind+Siliguri:wght@300;400;500;600;700',
    'Baloo Da 2': 'Baloo+Da+2:wght@400;500;600;700',
    'Galada': 'Galada',
    'Atma': 'Atma:wght@400;500;600;700',
}

EN_HEADING = ['Playfair Display', 'DM Serif Display', 'Libre Baskerville', 'Lora', 'Merriweather', 'EB Garamond']
EN_BODY = ['Crimson Pro', 'Lora', 'Source Serif 4', 'Merriweather', 'EB Garamond', 'Libre Baskerville', 'Inter']
EN_ACCENT = ['Cormorant Garamond', 'Playfair Display', 'Lora', 'EB Garamond', 'Libre Baskerville']
EN_LABEL = ['Bebas Neue', 'Oswald', 'Barlow Condensed', 'Anton', 'Inter']
BN_HEADING = ['Noto Serif Bengali', 'Tiro Bangla', 'Anek Bangla', 'Noto Sans Bengali', 'Baloo Da 2', 'Hind Siliguri', 'Galada', 'Atma']
BN_BODY = ['Noto Serif Bengali', 'Noto Sans Bengali', 'Tiro Bangla', 'Anek Bangla', 'Hind Siliguri', 'Baloo Da 2']
BN_UI = ['Noto Sans Bengali', 'Anek Bangla', 'Hind Siliguri', 'Baloo Da 2', 'Tiro Bangla', 'Noto Serif Bengali']

SERIF = {'Playfair Display', 'DM Serif Display', 'Libre Baskerville', 'Lora', 'Merriweather', 'Crimson Pro',
         'EB Garamond', 'Source Serif 4', 'Cormorant Garamond', 'Noto Serif Bengali', 'Tiro Bangla'}


def choices(names):
    return [(n, n) for n in names]


def _stack(primary, bangla):
    generic = 'serif' if primary in SERIF else 'sans-serif'
    return f"'{primary}','{bangla}',{generic}"


def font_head(site):
    """<link> for Google Fonts + :root overrides for the chosen fonts."""
    pick = lambda field, default, allowed: (getattr(site, field, '') or default) if (getattr(site, field, '') or default) in allowed else default
    en_h = pick('font_en_heading', 'Playfair Display', EN_HEADING)
    en_b = pick('font_en_body', 'Crimson Pro', EN_BODY)
    en_a = pick('font_en_accent', 'Cormorant Garamond', EN_ACCENT)
    en_l = pick('font_en_label', 'Bebas Neue', EN_LABEL)
    bn_h = pick('font_bn_heading', 'Noto Serif Bengali', BN_HEADING)
    bn_b = pick('font_bn_body', 'Noto Serif Bengali', BN_BODY)
    bn_u = pick('font_bn_ui', 'Noto Sans Bengali', BN_UI)
    families = []
    for name in (en_h, en_b, en_a, en_l, bn_h, bn_b, bn_u):
        spec = CATALOG[name]
        if spec not in families:
            families.append(spec)
    url = 'https://fonts.googleapis.com/css2?' + '&'.join(f'family={f}' for f in families) + '&display=swap'
    css = (":root{"
           f"--ff-display:{_stack(en_h, bn_h)};"
           f"--ff-body:{_stack(en_b, bn_b)};"
           f"--ff-italic:{_stack(en_a, bn_b)};"
           f"--ff-label:{_stack(en_l, bn_u)};"
           f"--ff-bangla-ui:'{bn_u}',sans-serif;"
           f"--ff-bangla-heading:'{bn_h}',serif;"
           "}")
    # Font names come only from the fixed CATALOG above (validated by `pick`), so the CSS is safe as-is.
    return format_html('<link rel="stylesheet" href="{}">\n', url) + mark_safe(f'<style id="site-fonts">{css}</style>')
