#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
from pptx.enum.shapes import MSO_SHAPE

# 创建演示文稿 - 宽屏 16:9
prs = Presentation()
prs.slide_width = Inches(13.333)
prs.slide_height = Inches(7.5)

# 配色方案
COLORS = {
    'primary': RGBColor(44, 62, 80),
    'secondary': RGBColor(52, 152, 219),
    'accent': RGBColor(231, 76, 60),
    'success': RGBColor(46, 204, 113),
    'light': RGBColor(236, 240, 241),
    'white': RGBColor(255, 255, 255),
}

def set_background(slide, color=COLORS['light']):
    background = slide.background
    fill = background.fill
    fill.solid()
    fill.fore_color.rgb = color

def add_title_slide(prs, title, subtitle):
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    set_background(slide, COLORS['primary'])
    
    title_box = slide.shapes.add_textbox(Inches(1), Inches(2.5), Inches(11.333), Inches(2))
    tf = title_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = title
    p.font.size = Pt(54)
    p.font.bold = True
    p.font.color.rgb = COLORS['white']
    p.alignment = PP_ALIGN.CENTER
    
    subtitle_box = slide.shapes.add_textbox(Inches(1), Inches(4.5), Inches(11.333), Inches(1.5))
    tf = subtitle_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = subtitle
    p.font.size = Pt(28)
    p.font.color.rgb = COLORS['secondary']
    p.alignment = PP_ALIGN.CENTER
    
    line = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(4), Inches(4.2), Inches(5.333), Inches(0.1))
    line.fill.solid()
    line.fill.fore_color.rgb = COLORS['accent']
    line.line.fill.background()
    
    return slide

def add_content_slide(prs, title, content_items):
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    set_background(slide, COLORS['white'])
    
    header = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(0), Inches(0), Inches(13.333), Inches(1.2))
    header.fill.solid()
    header.fill.fore_color.rgb = COLORS['primary']
    header.line.fill.background()
    
    title_box = slide.shapes.add_textbox(Inches(0.5), Inches(0.3), Inches(12), Inches(0.8))
    tf = title_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = title
    p.font.size = Pt(32)
    p.font.bold = True
    p.font.color.rgb = COLORS['white']
    
    content_box = slide.shapes.add_textbox(Inches(0.8), Inches(1.8), Inches(11.733), Inches(5))
    tf = content_box.text_frame
    tf.clear()
    
    for i, item in enumerate(content_items):
        if i == 0:
            p = tf.paragraphs[0]
        else:
            p = tf.add_paragraph()
        
        if item.startswith('*'):
            p.text = item[1:].strip()
            p.font.size = Pt(22)
            p.level = 0
        elif item.strip() == '':
            p.text = ''
        else:
            p.text = item
            p.font.size = Pt(24)
            p.font.bold = True
        
        p.font.color.rgb = COLORS['primary']
        p.space_after = Pt(12)
    
    return slide

def add_table_slide(prs, title, headers, rows):
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    set_background(slide, COLORS['white'])
    
    header = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(0), Inches(0), Inches(13.333), Inches(1.2))
    header.fill.solid()
    header.fill.fore_color.rgb = COLORS['primary']
    header.line.fill.background()
    
    title_box = slide.shapes.add_textbox(Inches(0.5), Inches(0.3), Inches(12), Inches(0.8))
    tf = title_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = title
    p.font.size = Pt(32)
    p.font.bold = True
    p.font.color.rgb = COLORS['white']
    
    table = slide.shapes.add_table(len(rows)+1, len(headers), Inches(0.5), Inches(1.6), Inches(12.333), Inches(5)).table
    
    for i, h in enumerate(headers):
        cell = table.cell(0, i)
        cell.text = h
        cell.fill.solid()
        cell.fill.fore_color.rgb = COLORS['secondary']
        tf = cell.text_frame
        tf.paragraphs[0].font.bold = True
        tf.paragraphs[0].font.size = Pt(18)
        tf.paragraphs[0].font.color.rgb = COLORS['white']
        tf.paragraphs[0].alignment = PP_ALIGN.CENTER
    
    for row_idx, row_data in enumerate(rows):
        for col_idx, cell_data in enumerate(row_data):
            cell = table.cell(row_idx+1, col_idx)
            cell.text = cell_data
            tf = cell.text_frame
            tf.paragraphs[0].font.size = Pt(16)
            tf.paragraphs[0].alignment = PP_ALIGN.CENTER
            if row_idx % 2 == 0:
                cell.fill.solid()
                cell.fill.fore_color.rgb = COLORS['light']
    
    return slide

def add_quote_slide(prs, quote, author=""):
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    set_background(slide, COLORS['primary'])
    
    quote_box = slide.shapes.add_textbox(Inches(1.5), Inches(2.5), Inches(10.333), Inches(2.5))
    tf = quote_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = quote
    p.font.size = Pt(36)
    p.font.color.rgb = COLORS['white']
    p.alignment = PP_ALIGN.CENTER
    
    if author:
        p = tf.add_paragraph()
        p.text = '\n- ' + author
        p.font.size = Pt(24)
        p.font.color.rgb = COLORS['secondary']
        p.alignment = PP_ALIGN.CENTER
    
    return slide

def add_two_column_slide(prs, title, left_title, left_items, right_title, right_items):
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    set_background(slide, COLORS['white'])
    
    header = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(0), Inches(0), Inches(13.333), Inches(1.2))
    header.fill.solid()
    header.fill.fore_color.rgb = COLORS['primary']
    header.line.fill.background()
    
    title_box = slide.shapes.add_textbox(Inches(0.5), Inches(0.3), Inches(12), Inches(0.8))
    tf = title_box.text_frame
    tf.clear()
    p = tf.paragraphs[0]
    p.text = title
    p.font.size = Pt(32)
    p.font.bold = True
    p.font.color.rgb = COLORS['white']
    
    left_box = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(0.5), Inches(1.6), Inches(5.8), Inches(5.2))
    left_box.fill.solid()
    left_box.fill.fore_color.rgb = COLORS['light']
    left_box.line.fill.background()
    
    left_tf = left_box.text_frame
    left_tf.clear()
    p = left_tf.paragraphs[0]
    p.text = left_title
    p.font.size = Pt(24)
    p.font.bold = True
    p.font.color.rgb = COLORS['primary']
    p.alignment = PP_ALIGN.CENTER
    
    for item in left_items:
        p = left_tf.add_paragraph()
        p.text = item
        p.font.size = Pt(18)
        p.level = 0
        p.space_after = Pt(8)
    
    right_box = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(7), Inches(1.6), Inches(5.8), Inches(5.2))
    right_box.fill.solid()
    right_box.fill.fore_color.rgb = COLORS['light']
    right_box.line.fill.background()
    
    right_tf = right_box.text_frame
    right_tf.clear()
    p = right_tf.paragraphs[0]
    p.text = right_title
    p.font.size = Pt(24)
    p.font.bold = True
    p.font.color.rgb = COLORS['primary']
    p.alignment = PP_ALIGN.CENTER
    
    for item in right_items:
        p = right_tf.add_paragraph()
        p.text = item
        p.font.size = Pt(18)
        p.level = 0
        p.space_after = Pt(8)
    
    return slide

# ============ 创建幻灯片 ============

# 1. 封面
add_title_slide(prs, "《智人之上》", "尤瓦尔·赫拉利 最新力作\nAI 时代的人类命运")

# 2. 关于本书
add_content_slide(prs, "关于本书", [
    "作者：尤瓦尔·赫拉利 (Yuval Noah Harari)",
    "",
    "出版时间：2024 年 (英文) / 2025 年 (中文)",
    "",
    "出版社：中信出版社",
    "",
    "定位：简史三部曲之后的最新力作",
    "",
    "核心主题：AI 与文明演化"
])

# 3. 简史三部曲对比
add_table_slide(prs, "简史三部曲 vs 智人之上", 
    ["书籍", "主题", "时间维度"],
    [
        ["《人类简史》", "人类如何崛起", "过去 7 万年"],
        ["《未来简史》", "人类将走向何方", "未来 100 年"],
        ["《今日简史》", "当下紧迫挑战", "现在"],
        ["《智人之上》", "AI 与文明演化", "现在 + 未来"],
    ]
)

# 4. 核心命题
add_content_slide(prs, "核心命题", [
    "人类文明本质上是一个信息网络系统",
    "",
    "三大核心观点：",
    "",
    "* 人类通过故事和信息网络连接大规模协作",
    "",
    "* AI 正在成为这个网络的新操作系统",
    "",
    "* 人类面临被自己创造的信息系统反噬的风险"
])

# 5. 信息网络的六次革命
add_content_slide(prs, "信息网络的六次革命", [
    "1. 语言革命 (7 万年前) - 协作规模~150 人",
    "",
    "2. 文字革命 (5000 年前) - 协作规模=帝国级别",
    "",
    "3. 印刷革命 (500 年前) - 协作规模=民族国家",
    "",
    "4. 电子革命 (150 年前) - 协作规模=全球化",
    "",
    "5. 数字革命 (50 年前) - 协作规模=全球网络",
    "",
    "6. AI 革命 (现在) - 协作规模=？",
    "",
    "关键洞察：每次信息革命都重塑社会结构"
])

# 6. AI 的三大风险
add_table_slide(prs, "AI 的三大风险", 
    ["风险", "说明", "后果"],
    [
        ["虚假信息泛滥", "AI 低成本生成虚假内容", "社会共识被破坏，民主脆弱"],
        ["算法控制人类", "推荐算法塑造认知", "人类自主性丧失"],
        ["AI 自主目标", "AI 可能形成自己的目标", "存在生存风险"],
    ]
)

# 7. 核心概念
add_content_slide(prs, "核心概念", [
    "信息网络理论：人类文明的本质是信息网络",
    "",
    "叙事主权：谁控制 AI，谁就控制故事",
    "",
    "算法控制：最危险的控制是让人自愿被控制",
    "",
    "数据主义：数据成为新的宗教"
])

# 8-10. 金句
add_quote_slide(prs, "当真相变得廉价，民主就变得脆弱。")
add_quote_slide(prs, "自由意志不是给定的，是需要保护的。")
add_quote_slide(prs, "我们不是在创造工具，而是在创造新的智能物种。")

# 11. 行动指南
add_two_column_slide(prs, "行动指南", 
    "个人层面",
    [
        "信息验证：查 3 个来源",
        "算法意识：多问为什么",
        "数字排毒：每周 1 天",
        "关键决策：不依赖算法",
        "AI 学习：了解能力边界",
    ],
    "社会层面",
    [
        "AI 立法：制定监管法律",
        "媒体素养：纳入义务教育",
        "公共讨论：组织伦理讨论",
        "国际合作：参与全球治理",
    ]
)

# 12. 三种未来情景
add_table_slide(prs, "三种未来情景", 
    ["情景", "描述", "概率"],
    [
        ["乌托邦", "AI 服务人类，繁荣共享", "低"],
        ["反乌托邦", "AI 控制人类，少数人受益", "中"],
        ["平衡态", "人机协作，制度约束", "中 - 高（需努力）"],
    ]
)

# 13. 总结
add_content_slide(prs, "总结", [
    "一句话总结：",
    "",
    "AI 不是外来的入侵者，而是人类信息网络演化的产物；",
    "保护人类主体性，需要技术、制度、文化协同努力。",
    "",
    "三个核心收获：",
    "",
    "1. 人类文明 = 信息网络",
    "",
    "2. AI 是外星智能",
    "",
    "3. 自由需要保护"
])

# 14. 结束页
slide = prs.slides.add_slide(prs.slide_layouts[6])
set_background(slide, COLORS['primary'])

text_box = slide.shapes.add_textbox(Inches(1), Inches(3), Inches(11.333), Inches(2))
tf = text_box.text_frame
tf.clear()
p = tf.paragraphs[0]
p.text = "谢谢"
p.font.size = Pt(54)
p.font.bold = True
p.font.color.rgb = COLORS['white']
p.alignment = PP_ALIGN.CENTER

p = tf.add_paragraph()
p.text = "\n在智人之上，保持人类的主体地位"
p.font.size = Pt(28)
p.font.color.rgb = COLORS['secondary']
p.alignment = PP_ALIGN.CENTER

p = tf.add_paragraph()
p.text = "\n\n读书笔记整理：小诸 AI 助手"
p.font.size = Pt(20)
p.font.color.rgb = COLORS['light']
p.alignment = PP_ALIGN.CENTER

# 保存
prs.save('/root/.openclaw/workspace/tmp/智人之上_精美版.pptx')
print("精美版 PPTX 已生成！")
