# -*- coding: utf-8 -*-
"""第4章 第5次课（讲A，4.1–4.3）与第6次课（讲B，4.4–4.9）HTML 课件生成器。
用法: python3 _build56.py
结构:
  lecture5/5.1.html … 5.44.html + lecture5/index.html（讲A 44 页，5 个板块）
  lecture6/6.1.html … 6.45.html + lecture6/index.html（讲B 45 页，5 个板块）
内容: _content5a/b/c.py（讲A）、_content6a/b/c.py（讲B）
依据: 《第5-6次课课件-逐页结构规范.md》＋学校 PPT 第4章（59 页）＋教材第4章 AI 视觉转录（p56–82）
素材: 插图一律用我们 Python 自绘的图（assets/第4章/图4-*.png），不使用课本裁图
标记: ★ = 课后拓展页（供课后复盘与作业）
"""
import os, sys

ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, ROOT)

from _build import slide_page, esc, CSS_VER

from _content5a import SLIDES_5A
from _content5b import SLIDES_5B
from _content5c import SLIDES_5C
from _content6a import SLIDES_6A
from _content6b import SLIDES_6B
from _content6c import SLIDES_6C

LEC5 = SLIDES_5A + SLIDES_5B + SLIDES_5C
LEC6 = SLIDES_6A + SLIDES_6B + SLIDES_6C

STAR5 = {10, 39}
STAR6 = {20, 38}

GROUPS5 = [
    ('一、封面 · 导入案例', 1, 3),
    ('二、4.1 软件项目范围管理概述', 4, 10),
    ('三、4.2 规划范围管理', 11, 18),
    ('四、4.3 收集需求', 19, 38),
    ('五、小结 · 练习 · 作业', 39, 44),
]
GROUPS6 = [
    ('一、封面 · 本讲地图', 1, 2),
    ('二、4.4 定义范围', 3, 8),
    ('三、4.5 创建工作分解结构', 9, 23),
    ('四、4.6 确认范围 · 4.7 控制范围', 24, 34),
    ('五、4.8 交付绩效域 · 4.9 实验衔接 · 小结作业', 35, 45),
]

META = {
    5: dict(folder='lecture5', slides=LEC5, star=STAR5, groups=GROUPS5, total=44,
            label='软件项目管理 · 第 5 次课',
            title='第 5 次课（第 5 周）：软件项目范围管理（一）',
            sub='4.1 概述 · 4.2 规划范围管理 · 4.3 收集需求'),
    6: dict(folder='lecture6', slides=LEC6, star=STAR6, groups=GROUPS6, total=45,
            label='软件项目管理 · 第 6 次课',
            title='第 6 次课（第 6 周）：软件项目范围管理（二）',
            sub='4.4 定义范围 · 4.5 创建 WBS · 4.6 确认范围 · 4.7 控制范围 · 4.8 交付绩效域 · 4.9 实验衔接'),
}


def build_one(no):
    m = META[no]
    slides, total, star, groups = m['slides'], m['total'], m['star'], m['groups']
    assert len(slides) == total, f'第 {no} 次课页数应为 {total}，实际 {len(slides)}'
    folder = os.path.join(ROOT, m['folder'])
    os.makedirs(folder, exist_ok=True)
    for i, (pt, title, body) in enumerate(slides, start=1):
        prev, nxt = i - 1, i + 1
        pt_prev = slides[prev - 1][0] if prev >= 1 else ''
        pt_next = slides[nxt - 1][0] if nxt <= total else ''
        href_prev = f'{no}.{prev}.html' if prev >= 1 else ''
        href_next = f'{no}.{nxt}.html' if nxt <= total else ''
        mark = '　★课后拓展' if i in star else ''
        with open(os.path.join(folder, f'{no}.{i}.html'), 'w', encoding='utf-8') as f:
            f.write(slide_page(no, i, total, f'{title}{mark}', body, pt_prev, pt_next,
                               href_prev, href_next, m['label']))

    items = []
    for name, s, e in groups:
        items.append(f'    <div class="toc-section">{esc(name)} <em>第 {s}-{e} 页</em></div>')
        for i in range(s, e + 1):
            pt, title, _ = slides[i - 1]
            badge = ' <span class="toc-star">★课后</span>' if i in star else ''
            items.append(f'    <a class="toc-item" href="{no}.{i}.html"><span class="toc-num">{no}.{i}</span>'
                         f'<span class="toc-title">{esc(title)}{badge}</span><span class="toc-arrow">→</span></a>')
    n_star = len(star)
    with open(os.path.join(folder, 'index.html'), 'w', encoding='utf-8') as f:
        f.write(f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>第{no}次课 — 目录</title>
<link rel="stylesheet" href="../styles.css?v={CSS_VER}">
<style>
.toc-section{{margin:24px 0 8px;padding:8px 14px;background:linear-gradient(135deg,#1a365d,#2b6cb0);color:#fff;border-radius:8px;font-size:1.05rem;font-weight:700;letter-spacing:.5px;}}
.toc-section:first-of-type{{margin-top:12px;}}
.toc-section em{{font-style:normal;font-weight:400;opacity:.85;font-size:.9rem;margin-left:6px;}}
.toc-star{{display:inline-block;margin-left:8px;font-size:.72em;color:#744210;background:#fefcbf;border-radius:8px;padding:1px 7px;vertical-align:1px;}}
</style>
</head>
<body>

<div class="logo-bar">
  <a class="brand-link" href="../index.html" title="返回课程主页（16 次课总目录）"><img src="../logo.png" alt="Logo"></a>
  <span class="logo-label">{esc(m['label'])}</span>
</div>

<div class="breadcrumb">
  <a href="../index.html">← 返回主页</a>
</div>

<div class="toc-page">
  <h2>🕒 {esc(m['title'])}</h2>
  <div class="toc-subtitle">{total} 张幻灯片 · {len(groups)} 个板块 · 其中 <b>{n_star} 页为 ★课后拓展</b>（供课后复盘与作业）</div>
  <div class="toc-subtitle">{esc(m['sub'])}</div>
  <div class="toc-subtitle">点击任意标题直接跳转（键盘 ← → 或滚轮翻页；任意页按 <b>Esc</b> 可回到本目录）</div>
  <div class="toc-subtitle">👩‍🏫 授课教师：周宇文 ｜ 联系电话/微信：13580390715</div>

  <div class="toc-progress">
    <span>0/{total}</span>
    <div class="progress-bar"><div class="progress-fill" style="width:0%"></div></div>
  </div>

  <div class="toc-list">

{chr(10).join(items)}

  </div>
</div>
</body>
</html>''')
    print(f"lecture{no} built: {total} slides（含 {n_star} 页★课后）+ toc（{len(groups)} 板块）")
    print('  ★课后页:', '、'.join(f'{no}.{i}' for i in sorted(star)))


if __name__ == '__main__':
    for n in (5, 6):
        build_one(n)
