# -*- coding: utf-8 -*-
"""Add 后续工作 & 总结 slides to yh -3D_OpticsFEM.pptx（仅矩阵组装范围）"""
from pathlib import Path
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN

SRC = Path(r"e:\组会\yh -3D_OpticsFEM.pptx")
OUT = Path(r"e:\组会\yh -3D_OpticsFEM_updated.pptx")
LAYOUT = 11  # 5_标题和内容
# 保留前 6 页（封面～散射结果展示），其后全部替换
KEEP_SLIDES = 6


def delete_slide(prs, index):
    sld_id = prs.slides._sldIdLst[index]
    r_id = sld_id.rId
    prs.part.drop_rel(r_id)
    del prs.slides._sldIdLst[index]


def trim_after_keep(prs):
    while len(prs.slides) > KEEP_SLIDES:
        delete_slide(prs, KEEP_SLIDES)


def set_title(slide, text):
    if slide.shapes.title:
        slide.shapes.title.text = text


def add_bullets(slide, left, top, width, height, items, size=16):
    box = slide.shapes.add_textbox(left, top, width, height)
    tf = box.text_frame
    tf.word_wrap = True
    for i, item in enumerate(items):
        p = tf.paragraphs[0] if i == 0 else tf.add_paragraph()
        p.text = item
        p.font.size = Pt(size)
        p.space_after = Pt(8)
        if item.startswith("【"):
            p.font.bold = True


def add_table_slide(prs, title, headers, rows):
    slide = prs.slides.add_slide(prs.slide_layouts[LAYOUT])
    set_title(slide, title)
    nrows, ncols = len(rows) + 1, len(headers)
    tbl = slide.shapes.add_table(
        nrows, ncols, Inches(0.45), Inches(1.45), Inches(12.2), Inches(0.42 * nrows)
    ).table
    for j, h in enumerate(headers):
        c = tbl.cell(0, j)
        c.text = h
        for p in c.text_frame.paragraphs:
            p.font.bold = True
            p.font.size = Pt(12)
    for i, row in enumerate(rows):
        for j, val in enumerate(row):
            c = tbl.cell(i + 1, j)
            c.text = val
            for p in c.text_frame.paragraphs:
                p.font.size = Pt(11)


def build():
    prs = Presentation(str(SRC))
    trim_after_keep(prs)

    # --- 后续工作一：矩阵组装扩展（不含求解/后处理）---
    slide = prs.slides.add_slide(prs.slide_layouts[LAYOUT])
    set_title(slide, "后续工作一：完善三维矩阵组装")
    add_bullets(slide, Inches(0.4), Inches(1.35), Inches(12), Inches(5.8), [
        "工作范围：kernel 矩阵组装与导出；求解器、后处理不在本阶段任务内。",
        "",
        "【散射 FemType=4】",
        "  · 在现有体积分 + SBC + 入射组装基础上，扩展更多边界类型",
        "  · 对齐 MATLAB：assembly_equ / assembly_out / assembly_inc 其余算例变体",
        "",
        "【本征 FemType=5】",
        "  · 完善 A/B 组装：更多边界（PEC/PBC 等）与算例配置",
        "  · 稳定导出 Ai/Aj/Av、Bi/Bj/Bv，便于与参考代码对比",
        "",
        "【单元与算例扩展】",
        "  · 周期边界 PBC、二阶 Nedelec、各向异性材料等（按光学算例需求逐项接入组装）",
        "  · 覆盖课题组典型 3D 光学算例的「可组装、可导出矩阵」",
        "",
        "【验证方式】",
        "  · 与 MATLAB / 参考矩阵逐元或 COO 格式对比，不要求 C++ 端求解场",
    ], size=15)

    # --- 后续工作二：.em 接口（待做）---
    slide = prs.slides.add_slide(prs.slide_layouts[LAYOUT])
    set_title(slide, "后续工作二：改造接口适配 Design.em")
    add_bullets(slide, Inches(0.4), Inches(1.35), Inches(12), Inches(5.8), [
        "目标：LASPCEM/HOFEM 使用 Design.em + Design.em.mesh；内核仍用 JSON。",
        "",
        "【待开发】",
        "  · 适配层：解析 .em → 生成内部 JSON + 网格输入",
        "  · Mesh_3D 读入 Design.em.mesh（与现有 SBCmesh.dat 路径并列）",
        "  · 材料、边界、频率、激励字段映射到 phy / material",
        "  · 仍通过 OpticsFEM_All 入口，组装完成后导出矩阵",
        "",
        "【说明】",
        "  · 本项为后续接口工作，当前阶段尚未完成",
        "  · 组装验证仍以 sbc3d.json / eigen3d.json + SBCmesh.dat 为主",
    ], size=15)

    # --- 路线图 ---
    add_table_slide(
        prs,
        "后续工作路线图",
        ["方向", "内容", "说明"],
        [
            ["矩阵组装", "FemType 4/5 及更多 3D 光学算例", "本阶段核心"],
            ["边界扩展", "PBC、二阶基、各向异性等", "仅组装层"],
            [".em 适配", "Design.em → JSON + mesh", "接口层，待开发"],
            ["验证", "与 MATLAB 矩阵对比", "不依赖 C++ 求解"],
        ],
    )

    # --- 总结 ---
    slide = prs.slides.add_slide(prs.slide_layouts[LAYOUT])
    set_title(slide, "工作总结")
    add_bullets(slide, Inches(0.5), Inches(1.35), Inches(12), Inches(4.0), [
        "【已完成 — 矩阵组装】",
        "  ① 保持 JSON 配置与模块化架构，将 2D 一阶边元扩展为 3D 四面体矢量有限元。",
        "  ② Mesh_3D：读入 SBCmesh.dat；phy / material 从 JSON 加载。",
        "  ③ 散射（FemType=4）：体积分 + SBC 出射 + 入射激励，完成稀疏矩阵 A 与右端 b 组装。",
        "  ④ 本征（FemType=5）：刚度 A、质量 B 组装，COO 导出（Ai/Aj/Av、Bi/Bj/Bv）。",
        "  ⑤ 与 MATLAB 参考代码矩阵对比一致（如 Av.txt 等）。",
        "",
        "【工作边界】",
        "  · 本阶段止于矩阵组装与导出；求解器、后处理由其他模块或 MATLAB 完成。",
    ], size=14)

    add_bullets(slide, Inches(0.5), Inches(5.2), Inches(12), Inches(2.0), [
        "【后续方向】",
        "  · 完善 3D 组装代码，适配更多光学算例类型",
        "  · 开发 .em 文件适配接口（尚未完成）",
    ], size=15)

    slide = prs.slides.add_slide(prs.slide_layouts[LAYOUT])
    set_title(slide, "谢谢！")
    box = slide.shapes.add_textbox(Inches(2), Inches(2.8), Inches(9), Inches(1.5))
    tf = box.text_frame
    tf.text = "欢迎批评指正"
    p = tf.paragraphs[0]
    p.font.size = Pt(32)
    p.alignment = PP_ALIGN.CENTER

    prs.save(str(OUT))
    print(f"Saved {len(prs.slides)} slides -> {OUT}")


if __name__ == "__main__":
    build()
