# -*- coding: utf-8 -*-
"""Level 4 — Mission Dialogues: the everyday dialogues of « À vos marques, prêts… parlez ! » 3 (level 1 of each theme)
and 5 (level 2, longer), with the books' own recordings. One lesson per theme."""
import random
from .kit import act, choose, match, order, lesson
from .dial_data import THEMES

ICONS = ["👋", "👨‍👩‍👧", "🎒", "🏫", "🏠", "🌦️", "⛈️", "🧹", "♻️", "🆘", "🎨", "🌳", "💡", "🏊", "🥗", "🛒", "🚦", "🧭", "🚗", "✈️",
         "⛴️", "🎉", "⚽", "🎵", "🖼️", "🎬", "🦷", "🦁", "🐘", "📚", "🐠"]
WHO = ["🧑", "👧"]


def dialogue(lid, n, lines, title, title_ar):
    return act(lid, n, "dialogue", title, title_ar, "J'écoute le dialogue, puis je touche chaque phrase pour la réécouter.",
               "استمع إلى الحوار بأصوات حقيقية، ثم المس كل جملة لتسمعها من جديد.", 10,
               dict(lines=[dict(who=WHO[l["who"]], fr=l["fr"], ar=l["ar"], audio=l["audio"]) for l in lines]))


def dictation(lid, n, lines):
    return act(lid, n, "dictation_type", "Dictée", "إملاء", "J'écoute la phrase et je l'écris.", "استمع إلى الجملة ثم اكتبها كما سمعتها.", 20,
               dict(items=[dict(text=l["fr"], audio=l["audio"], ar=l["ar"]) for l in lines]))


def speak(lid, n, lines):
    return act(lid, n, "speak", "À moi de parler !", "دوري في الكلام!", "J'écoute, puis je dis la phrase à voix haute.",
               "استمع ثم قل الجملة بصوت عالٍ (المس الميكروفون).", 15,
               dict(items=[dict(prompt=l["fr"], ar=l["ar"], expected=l["fr"], audio=l["audio"]) for l in lines]))


def theme_lesson(k, t):
    lid = f"l4-dl{k + 1:02d}"
    p3, p5 = t["books"].get("parlez3"), t["books"].get("parlez5")
    acts, n = [], 1
    if p3:
        acts.append(dialogue(lid, n, p3["lines"], "Dialogue — niveau 1", "الحوار — المستوى 1")); n += 1
    if p5:
        acts.append(dialogue(lid, n, p5["lines"], "Dialogue — niveau 2", "الحوار — المستوى 2")); n += 1
    lines = (p3["lines"] if p3 else []) + (p5["lines"] if p5 else [])
    qs = (p3["questions"] if p3 else []) + (p5["questions"] if p5 else [])
    if len(qs) >= 2:
        acts.append(choose(lid, n, [(q["q"], q["opts"], q["answer"]) for q in qs], "J'écoute et je comprends", "أستمع وأفهم")); n += 1
    pairs = [(l["fr"], l["en"]) for l in lines if l["en"] and len(l["fr"]) <= 60]
    pairs = list({a: (a, b) for a, b in pairs}.values())
    if len(pairs) >= 3:
        acts.append(match(lid, n, pairs[:6], "Français – anglais", "الفرنسية – الإنجليزية")); n += 1
    long = [l for l in lines if len(l["fr"].split()) >= 3]
    rng = random.Random(lid)
    pick = rng.sample(long, min(3, len(long)))
    if pick:
        acts.append(order(lid, n, [l["fr"] for l in pick], "Je remets la phrase en ordre", "أرتّب الجملة")); n += 1
    acts.append(dictation(lid, n, [l for l in lines if l["audio"]][:4] or lines[:3])); n += 1
    acts.append(speak(lid, n, lines[:4])); n += 1
    return lesson(lid, f"Dialogue {k + 1}", t["title"], t["title_ar"] or t["title"], ICONS[k % len(ICONS)], acts, kind="dialogue")


def build():
    return [theme_lesson(k, t) for k, t in enumerate(THEMES)]
