import re, sys, html as H
# usage : build.py source.html sortie.html [c|a]
src, out = sys.argv[1], sys.argv[2]
V = sys.argv[3] if len(sys.argv) > 3 else 'c'
L = lambda f: open('_layer/'+f, encoding='utf-8').read()
h = open(src, encoding='utf-8').read()
def sub1(pattern, repl, s, label):
    n = s.count(pattern)
    if n != 1: sys.exit(f'ERREUR ancre "{label}" trouvee {n} fois')
    return s.replace(pattern, repl)

voices = [
 ("A", "Amira K.", "Particulier · Regroupement familial", "Une équipe qui parle notre langue et qui nous a rassurés à chaque étape. Ma famille m'a rejointe à Lyon en huit mois."),
 ("R", "Ravi S.", "Salarié · Carte Bleue européenne", "Mon employeur et moi avions un seul interlocuteur. Le dossier était prêt avant même mon arrivée à Paris."),
 ("M", "Marta G.", "Particulier · Retraite en France", "J'avais peur de la paperasse. On m'a expliqué tout en espagnol, simplement. Aujourd'hui je vis en Provence."),
 ("D", "DRH, groupe industriel", "Entreprise · 40 recrutements / an", "Délais tenus, conformité sécurisée : notre service RH a enfin repris la main sur la mobilité internationale."),
 ("J", "James & Claire", "Particuliers · Mariage international", "Du mariage à la carte de résident, un suivi humain et précis. On s'est sentis attendus, pas administrés."),
 ("S", "Start-up SaaS", "Entreprise · Passeport Talent", "Trois ingénieurs recrutés à l'étranger en un trimestre. Une vraie équipe à nos côtés, réactive et pédagogue."),
]
def card(v, hidden=False):
    a, n, r, q = map(H.escape, v)
    ah = ' aria-hidden="true"' if hidden else ''
    return f'<figure class="voice"{ah}><span class="voice-tag">Exemple à remplacer</span><q>{q}</q><figcaption class="voice-who"><span class="voice-av" aria-hidden="true">{a[0]}</span><span><b>{n}</b><small>{r}</small></span></figcaption></figure>'
vhtml = '\n      '.join([card(v) for v in voices] + [card(v, True) for v in voices])

if V == 'c':
    css_extra = L('threads.css') + '\n' + L('faces.css'); art = L('threads.html')
    js_extra = L('threads.js') + '\n' + L('faces.js') + L('faces-layer.js')
    title = 'OUI Immigration — Accueil (piste C animée : les fils et les visages)'
else:
    css_extra = L('walk.css'); art = L('walk.html'); js_extra = L('walk.js')
    title = 'OUI Immigration — Accueil (piste A animée : la traversée accompagnée)'

h = sub1('</head>', '<base href="https://myagencyinside.com/maquettes/oui-immigration/">\n<script>document.documentElement.classList.add("js-anim")</script>\n' + L('anim.css') + '\n' + css_extra + '\n</head>', h, 'head')
h = re.sub(r'<title>[^<]*</title>', '<title>' + title + '</title>', h, 1)
h = sub1('</h1>\n', '</h1>\n' + L('rotator.html'), h, 'h1')
h = sub1('<!-- ACTUALITÉS -->', L('voices.html').replace('__VOICES__', vhtml) + '\n<!-- ACTUALITÉS -->', h, 'actualites')
cur_c = ' aria-current="page"' if V == 'c' else ''
cur_a = ' aria-current="page"' if V == 'a' else ''
h = sub1('aria-current="page" title="Le pont">C · Le pont</a>',
         'title="Le pont">C · Le pont</a><a href="#" data-local="v1-piste-a-anime.html"' + cur_a + ' title="La traversée accompagnée">A+ · Traversée</a><a href="#" data-local="v1-piste-c-anime.html"' + cur_c + ' title="Les fils et les visages">C+ · Visages</a>', h, 'switch')
i0 = h.find('<picture class="hero-art">'); i1 = h.find('</picture>', i0)
if i0 < 0 or i1 < 0: sys.exit('ERREUR ancre picture')
h = h[:i0] + art.strip() + h[i1+len('</picture>'):]
# liens entre maquettes animées : relatifs au fichier local (la balise base pointe vers le serveur)
switch_js = "<script>document.querySelectorAll('a[data-local]').forEach(function(a){a.href=new URL(a.dataset.local, location.href.replace(/[^/]*$/,'')).href;});</script>\n"
switch_js = switch_js.replace("location.href.replace(/[^/]*$/,'')", "document.currentScript ? location.href.replace(/[^/]*$/,'') : ''")
h = sub1('</body>', L('anim.js') + '\n' + js_extra + '\n' + switch_js + '</body>', h, 'body')
open(out, 'w', encoding='utf-8').write(h)
print('OK', V, len(h))
