import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment
from openpyxl.cell.rich_text import TextBlock, CellRichText

# 输出路径
output_path = "/root/.openclaw/workspace-xiaobian/output/客厅太空冒险_剧本_v3_官方格式.xlsx"

# --------------------------
# 准备剧本数据 (官方格式)
# --------------------------
script_data = []

# 表头
script_data.append([
    "类型", "配置信息", "剧情描述", "角色名", "编剧台词", 
    "英文台词", "组件配置", "知识点"
])

# --------------------------
# 段落1：太空故事
# --------------------------
script_data += [
["TL", "", "【场景】明亮舒适的 living room，阳光透过窗户洒在地毯上。房间一角放着一盏带着黄色灯罩的 lamp。【角色】User、Dad", "", "", "", "", ""],
["", "", "Dad穿着宽松的家居服，正懒洋洋地往他最喜欢的大 armchair 里钻。User 抱着一本厚厚的图画书跑进客厅。【emoji】Dad😌, User🤩【音效】欢快轻柔的尤克里里背景乐", "", "", "", "", ""],
["", "", "", "User", "Dad, you really don't have to work today?", "", "", ""],
["", "", "", "Dad", "Nope! Not today.", "", "", ""],
["", "", "", "Dad", "We have the whole **afternoon**, dear.", "", "", ""],
["", "", "User高兴地跳了一下，立刻把手里的图画书举到 Dad 面前，书的封面画着一艘彩色的飞船。", "", "", "", "", ""],
["对话互动", "", "", "User", "Can we read the space adventure?", "", "", ""],
["", "", "", "Dad", "Of course! This **afternoon**, we go to space!", "", "", ""],
["", "", "User开心地挨着 armchair 坐在地毯上，双手托腮，认真地听着。【镜头】特写 Dad 手里的书，画面是飞船在宇宙中飞行。", "", "", "", "", ""],
["", "", "", "Dad", "The spaceship is flying fast...", "", "", ""],
["", "", "", "Dad", "It's getting closer and closer to the glowing star!", "", "", ""],
["", "", "Dad兴奋地准备翻下一页揭晓结局，但他来回翻了几下，突然愣住了。后面几页全被撕掉了，只剩下参差不齐的纸边。【音效】背景乐突然暂停，翻书声戛然而止。【emoji】Dad😳", "", "", "", "", ""],
["互动(图片)", "", "画面出现被撕坏的书页特写，出现一个放大镜 icon 提示 User 点击查看。【玩家操作】点击被撕坏的书页边缘。【音效】点击时发出疑惑的音效 `Hmm?`", "", "", "", "", ""],
["", "", "Dad尴尬地挠了挠头，把残缺的书页展示给 User 看。【emoji】Dad😅", "", "", "", "", ""],
["", "", "", "Dad", "What's wrong with the book? The pages are gone!", "", "", ""],
["", "", "User不满地皱起眉头，站了起来，双手叉腰。", "", "", "", "", ""],
["对话互动", "", "", "User", "What's wrong with the book?", "", "", ""],
["", "", "", "Dad", "It's broken. The space trip is over.", "", "", ""],
["", "", "User显然不满足于故事就这么断了，在 living room 里来回踱步。就在这时，角落里那盏带黄色灯罩的 lamp 恰好闪烁了一下。【音效】灯泡闪烁的“滋滋”声。【镜头】给闪烁的 lamp 一个特写。", "", "", "", "", ""],
["对话互动", "", "", "User", "Look! That **<red>lamp</red>**!", "", "", ""],
["", "", "Dad顺着 User 手指的方向看过去，一脸疑惑。【emoji】Dad🤨", "", "", "", "", ""],
["", "", "", "Dad", "The lamp?", "", "", ""],
["", "", "User跑过去站在 lamp 旁边，双手比划了一个大大的圆形。【emoji】User😎【音乐】背景乐变成充满冒险感的欢快节奏。", "", "", "", "", ""],
["", "", "", "User", "That's the glowing star from the story!", "", "", ""],
["", "", "Dad瞬间明白了 User 的意思，他笑着把手里的破书直接扔到了沙发后面。【音效】书本落地的闷响。【emoji】Dad😁", "", "", "", "", ""],
["", "", "", "Dad", "You're right! We don't need the book!", "", "", ""],
["", "", "", "Dad", "We can BE the story!", "", "", ""],
]

# --------------------------
# 段落2：太空船起飞
# --------------------------
script_data += [
["TL", "", "仍是 living room，但背景音乐变得充满动感，像科幻电影的配乐。【角色】User、Dad", "", "", "", "", ""],
["", "", "随着 Dad 响亮地打了一个响指，整个 living room 开始发生奇妙的变化！【音效】响亮的“啪”声，紧接着是魔法般“叮叮叮”的音效。", "", "", "", "", ""],
["", "", "【场景转换】普通的窗帘变成了银色的星际防辐射板，茶几变成了布满仪表的控制台，地上的地毯图案看起来像星图。【镜头】环视变形后的 living room。【音乐】宏大、充满史诗感的太空探险乐起！", "", "", "", "", ""],
["对话互动", "", "", "User", "Our <red>**living room**</red> is a spaceship!", "", "", ""],
["", "", "User飞快地跑向刚才 Dad 坐过的那把大 armchair，一下子爬上去坐好。【音效】User 爬上椅子的“咚咚”声。", "", "", "", "", ""],
["对话互动", "", "", "User", "This <red>**armchair**</red> is my seat!", "", "", ""],
["", "", "", "User", "I'm the captain!", "", "", ""],
["", "", "Dad笑着走过来，假装很想坐那把舒服的椅子，伸手摸了摸椅背。【emoji】Dad🥺", "", "", "", "", ""],
["", "", "", "Dad", "Captain? Can I sit in the **armchair** too?", "", "", ""],
["", "", "User摇了摇头，果断拒绝，指着 armchair 旁边一张不起眼的小矮凳。【镜头】特写 User 坚定摇头的表情，然后镜头平移到旁边的小凳子上。", "", "", "", "", ""],
["", "", "", "User", "No way! You are my helper.", "", "", ""],
["", "", "", "User", "Helpers sit there.", "", "", ""],
["", "", "Dad委屈地瘪瘪嘴，夸张地叹了口气，然后乖乖走到小凳子旁坐下，还向 User 敬了个不标准的礼。【emoji】Dad🫡【音效】Dad 坐下时小凳子发出的“吱呀”声。", "", "", "", "", ""],
["", "", "", "Dad", "Yes, Captain! What's next?", "", "", ""],
["", "", "User坐直身子，目光盯住了桌上的风扇遥控器，遥控器上有红、蓝、绿三个显眼的按钮。User 伸出手，指向遥控器。【镜头】给桌上的遥控器特写。", "", "", "", "", ""],
["", "", "", "Dad", "We need to fly! <strong>Press the</strong> red button?", "", "", ""],
["对话互动", "", "", "User", "Yes! <red>**Press the**</red> red button!", "", "", ""],
["", "", "Dad狠狠按下了遥控器上的红色按键。【音效】按键“滴”的一声。", "", "", "", "", ""],
["", "", "突然，living room 里的电视屏幕亮了，画面正好是科幻电影里刺耳的警报声和巨大怪兽的吼叫，把两人都吓了一跳。【音效】电视里的警报声和吼叫声！【emoji】User😲, Dad😲", "", "", "", "", ""],
["", "", "User捂住耳朵，咯咯笑着朝 Dad 摇头大喊。【音效】电视声音太大，User 不得不大声说话。", "", "", "", "", ""],
["", "", "", "User", "Wrong button, helper!", "", "", ""],
["互动(图片-单选)", "", "画面给出遥控器特写（红、蓝、绿三个按钮），提示 User 操作。【玩家操作】在遥控器上点击蓝色的按钮。【音效】正确的“叮”声，电视关闭。", "", "", "", "", ""],
["对话互动", "", "", "User", "<red>**Press the**</red> blue button!", "", "", ""],
["", "", "Dad赶紧擦了把汗，这次小心翼翼地按下了蓝色按钮。【音效】按键声，紧接着是风扇启动时叶片转动的“嗡嗡”声。", "", "", "", "", ""],
["", "", "", "Dad", "Blue button! Here we go!", "", "", ""],
["", "", "这次按对了！头顶的吊扇呼呼地转了起来，强风吹过，User 的头发在风中飞舞。【镜头】特写风扇旋转，再切到 User 被风吹乱的头发和兴奋的脸庞。【emoji】User🤩", "", "", "", "", ""],
["", "", "强风把 living room 的窗帘也吹得像船帆一样高高鼓起。【音效】呼啸的风声，背景乐变得激昂。", "", "", "", "", ""],
["", "", "", "Dad", "It's working!", "", "", ""],
["", "", "", "User", "We're flying! The spaceship is moving!", "", "", ""],
]

# --------------------------
# 段落3：小行星带！撞击！
# --------------------------
script_data += [
["TL", "", "变成飞船驾驶舱的 living room 中，风扇呼呼地吹着，背景音乐充满星际旅行的动感节奏。【角色】User、Dad", "", "", "", "", ""],
["", "", "User紧紧抓着 armchair 的扶手，身体随着飞行的想象左右摇晃；Dad 坐在旁边的小凳子上，假装在一块不存在的面板上敲敲打打。【emoji】User🤩", "", "", "", "", ""],
["", "", "", "Dad", "Captain! I see something ahead!", "", "", ""],
["", "", "User突然挺直了背，眯起眼睛指向前方地板上散落的彩色靠垫和玩具积木（在他们的想象中，那些是一颗颗漂浮的陨石）。【镜头】从 User 的主观视角看去，地板上的靠垫闪烁着危险的红光，像极了太空中的小行星带。【音效】雷达警报的“滴滴”声。", "", "", "", "", ""],
["", "", "", "User", "Oh no! Asteroids!", "", "", ""],
["", "", "User把手放在想象中的方向盘上，开始大声指挥旁边的 helper Dad。【音效】飞船紧急转向的音效。【emoji】User😠", "", "", "", "", ""],
["", "", "", "User", "Left! Right!", "", "", ""],
["", "", "Dad夸张地配合着，整个人从小凳子上弹起来，假装拼命转动一个大副驾驶盘。【emoji】Dad😵", "", "", "", "", ""],
["", "", "", "Dad", "Left! Right! Turning!", "", "", ""],
["", "", "就在这时，由于 Dad 动作太大，不小心踢到了其中一个靠垫，靠垫滑过去，刚好卡住了 armchair 的一个轮子。【音效】沉闷的“BOOM”碰撞声，紧接着是一阵刺耳的金属摩擦音效。【镜头】特写靠垫卡进轮子的瞬间，整个画面跟着剧烈震动了一下。", "", "", "", "", ""],
["", "", "User失去平衡，在 armchair 里猛地往前栽了一下，赶紧抓紧扶手。【emoji】User😲", "", "", "", "", ""],
["", "", "", "User", "Whoa!", "", "", ""],
["", "", "头顶的风扇（引擎）似乎也被震到了，“咔哒”响了一声，然后越转越慢，风力减弱了。【音效】风扇减速的“呜...呜...”声，音乐变得紧张。", "", "", "", "", ""],
["", "", "", "Dad", "The engine! It's broken!", "", "", ""],
["对话互动", "", "", "User", "<red>**What's wrong with the**</red> spaceship?", "", "", ""],
["", "", "", "Dad", "We hit a big asteroid! We can't move!", "", "", ""],
["", "", "", "User", "Try the engine again!", "", "", ""],
["", "", "Dad想了想，指着遥控器上最大的那个电源键，向 User 建议。【emoji】Dad🤔", "", "", "", "", ""],
["", "", "", "Dad", "Maybe <strong>Press the</strong> power button? It might restart the engine!", "", "", ""],
["对话互动", "", "", "User", "<red>**Press the**</red> power button!", "", "", ""],
["", "", "", "Dad", "I'm trying! But it's not working, Captain!", "", "", ""],
["", "", "飞船（风扇）还是没有恢复动力。情况变得危急，Dad 摊开双手，看着 User 等待指示。【emoji】Dad😟", "", "", "", "", ""],
["", "", "", "Dad", "We are stuck! What do we do now?", "", "", ""],
["", "", "User摸了摸下巴，突然眼睛一亮，想到了一个绝妙的办法！User 站起来，用手指着 armchair 宽大的椅背，对 Dad 发出指令。【音效】灵光一闪的“叮铃”声。", "", "", "", "", ""],
["", "", "", "User", "Dad! You push the **armchair**!", "", "", ""],
["", "", "", "Dad", "Yes, Captain! Manual override!", "", "", ""],
["互动(图片-拖拽)", "", "画面出现俯视角的 living room（现在是布满靠垫/小行星的太空），提示 User 操作 Dad 推动椅子避开障碍。【玩家操作】拖拽 Dad 推动 armchair 绕开地上的 3 个靠垫（小行星），顺利到达安全区域。【音效】轮子在地板上滚动发出的摩擦声，避开障碍时的“嗖”声。", "", "", "", "", ""],
["", "", "在 User 的拖拽指挥下，Dad 咬着牙，用力推着 armchair 在 living room 里左拐右弯，笨拙但惊险地绕过了一个个靠垫。【镜头】跟随 Dad 推椅子的动作，画面充满颠簸感和速度感。【音乐】紧张刺激的冲刺配乐。", "", "", "", "", ""],
["", "", "终于，随着最后一次漂移转弯，他们成功穿过了这片“小行星带”！【音效】冲过终点线的欢呼音效，紧张的音乐随之舒缓下来。", "", "", "", "", ""],
["", "", "Dad累得一下子松开双手，瘫坐在地上大口喘气，还夸张地拿袖子擦了擦额头上不存在的汗。【emoji】Dad🥵", "", "", "", "", ""],
["", "", "", "Dad", "Phew... that was close!", "", "", ""],
["", "", "User得意地从 armchair 上跳下来，走到 Dad 面前，像个真正的老船长一样，满意地拍了拍 Dad 的肩膀。【emoji】User😎", "", "", "", "", ""],
["", "", "", "User", "Good job, helper! We passed the asteroids!", "", "", ""],
]

# --------------------------
# 段落4：发光的星球
# --------------------------
script_data += [
["TL", "", "仍是变身为太空的 living room。Dad 还瘫坐在地上喘气，User 则灵活地爬回 armchair 上重新坐稳。【角色】User、Dad", "", "", "", "", ""],
["", "", "User站在 armchair 上，手搭凉棚向远处眺望；Dad 在一旁揉着酸痛的胳膊。【emoji】User🤩, Dad🥱", "", "", "", "", ""],
["", "", "突然，角落里那盏有着黄色灯罩的 lamp 再次闪烁起来，发出微弱但诱人的光芒。【镜头】特写远处角落里一闪一闪的 lamp，伴随着神秘的、吸引人的星际音乐。", "", "", "", "", ""],
["对话互动", "", "", "User", "Look! The glowing star!", "", "", ""],
["", "", "User从 armchair 上跳下来，兴奋地指着前方。", "", "", "", "", ""],
["", "", "", "User", "We're close!", "", "", ""],
["", "", "Dad听到声音，精神一振，立刻从地上爬了起来。【emoji】Dad😎", "", "", "", "", ""],
["", "", "", "Dad", "The star! Finally!", "", "", ""],
["", "", "Dad拍了拍胸口，自告奋勇地大声说，想要在船长面前表现一下。", "", "", "", "", ""],
["", "", "", "Dad", "I'll get it! Helper to the rescue!", "", "", ""],
["", "", "可是 Dad 刚自信满满地往前跨出两大步，脚就被地上缠绕的 lamp 电线绊了一下。【音效】滑稽的“嗖——吧唧”绊倒声。", "", "", "", "", ""],
["", "", "Dad失去平衡，手忙脚乱地向前踉跄了几步，差点摔了个大马趴，最后只能尴尬地扶住了墙。【镜头】给 Dad 绊倒的滑稽动作一个特写。【emoji】Dad😵‍💫", "", "", "", "", ""],
["", "", "User看着笨手笨脚的 helper，无奈地摇了摇头，小大人似的叹了口气。【音效】轻松诙谐的配乐。", "", "", "", "", ""],
["", "", "", "User", "Oh, helper...", "", "", ""],
["", "", "User决定亲自出马。他小心翼翼地绕过电线，慢慢走到了那盏闪烁的 lamp 面前。【emoji】User🧐", "", "", "", "", ""],
["", "", "", "User", "I'll go.", "", "", ""],
["", "", "Dad在后面紧张地握紧拳头，小声给 User 加油。【emoji】Dad🤞", "", "", "", "", ""],
["", "", "", "Dad", "Be careful, Captain. You can do it!", "", "", ""],
["", "", "User凑近 lamp，发现它的灯光一闪一闪的，看起来很不稳定。【镜头】主观视角，User 盯着闪烁的灯罩内部。", "", "", "", "", ""],
["对话互动", "", "", "User", "<red>**What's wrong with the**</red> <red>**lamp**</red>?", "", "", ""],
["", "", "Dad扶着墙，尴尬地揉着膝盖，远远地看了一眼。【emoji】Dad🤔", "", "", "", "", ""],
["", "", "", "Dad", "Hmm... maybe the bulb is loose?", "", "", ""],
["", "", "User听到提示，伸出小手，伸进灯罩里，轻轻摸到了那个温热的灯泡，然后小心地拧了一下。【音效】拧灯泡的轻微摩擦声，紧接着是“叮”的一声清脆音效。", "", "", "", "", ""],
["", "", "奇迹发生了！原本闪烁不停的 lamp 瞬间稳定地亮了起来，发出明亮而温暖的黄色光芒，把整个昏暗的角落都照亮了。【镜头】温暖的光芒照在 User 充满成就感的笑脸上。【音乐】代表胜利与发现的华丽交响乐高潮！", "", "", "", "", ""],
["", "", "", "User", "It's working! I fixed the star!", "", "", ""],
["", "", "", "User", "And it's warm!", "", "", ""],
["", "", "任务完成了，但星球不能留在角落里。User 双手小心翼翼地抱住 lamp 的底座。【镜头】特写 User 抱起台灯的动作。", "", "", "", "", ""],
["互动(混合：对话+拖拽)", "【核心互动】", "User抱着这颗珍贵的“星球”，向 Dad 宣布它应该放在哪里。【玩家跟读/选择】 `Let's put the lamp by the armchair! Our living room is bright now!` 【玩家操作】在说完台词后，画面提示 User 将发光的 lamp 从角落拖拽到房间中央的 armchair 旁边。【音效】拖放成功的“咔哒”声和胜利的礼花音效。", "User", "Let's put the <red>**lamp**</red> by the <red>**armchair**</red>! Our <red>**living room**</red> is bright now!", "", "", ""],
["", "", "在 User 的指挥和操作下，lamp 稳稳地放在了 armchair 旁边。温暖的灯光洒满了整个被当作飞船的 living room，照亮了所有的“小行星”（靠垫）和“引擎”（风扇）。【镜头】全景展示被暖光包围的客厅。【emoji】User😎, Dad😁", "", "", "", "", ""],
["", "", "Dad走过来蹲下身，看着被暖光照亮的整个房间，发出由衷的感叹。【emoji】Dad😊", "", "", "", "", ""],
["", "", "", "Dad", "Wow... It's beautiful. The whole living room is glowing!", "", "", ""],
["", "", "User拍了拍手上的灰尘，满意地看着自己的杰作，像一个真正的太空探险家那样骄傲地宣布。【音效】英雄般的定音鼓声。", "", "", "", "", ""],
["", "", "", "User", "Mission accomplished!", "", "", ""],
]

# --------------------------
# 段落5：最棒的下午
# --------------------------
script_data += [
["TL", "", "仍是 living room，但此前的“太空”幻象（银色防辐射板、仪表盘）已经悄然褪去，变回了普通的窗帘和茶几。【镜头】整个房间被旁边那盏修好的黄色 lamp 照亮，光线柔和而温暖。【音乐】舒缓、温馨的家庭钢琴曲渐入。", "", "", "", "", ""],
["", "", "冒险结束了。满头大汗的 User 舒服地靠在巨大的 armchair 柔软的椅背上，长舒了一口气。【emoji】User😌", "", "", "", "", ""],
["", "", "", "User", "Phew... What an <red>**afternoon**</red>!", "", "", ""],
["", "", "Dad并没有回到小板凳上，而是直接盘腿坐在了地毯上，背靠着 User 所在的 armchair 的扶手。他看着散落一地的靠垫（曾经的小行星），笑着摇了摇头。【emoji】Dad😆", "", "", "", "", ""],
["", "", "房间里安静下来，只有头顶的风扇还在发出微弱的“嗡嗡”声。Dad 抬起头，看着坐在高高椅子上的 User，声音里透着真诚的快乐。【镜头】特写 Dad 满足的表情。", "", "", "", "", ""],
["", "", "", "Dad", "This was the best **afternoon**.", "", "", ""],
["", "", "", "Dad", "Even better than the book.", "", "", ""],
["", "", "User环顾四周，看着这个被灯光照亮的、有些凌乱的房间，露出满足的微笑。【emoji】User😊", "", "", "", "", ""],
["", "", "", "User", "I love our <red>**living room**</red>.", "", "", ""],
["对话互动", "", "", "User", "Next <red>**afternoon**</red>, we fly to the moon!", "", "", ""],
["", "", "Dad听完，仰起头哈哈大笑起来。他转过身，向他那永远充满活力的“船长”举起手，认真地敬了一个礼。【emoji】Dad🫡", "", "", "", "", ""],
["", "", "", "Dad", "Yes, Captain! To the moon!", "", "", ""],
["", "", "User也开心地笑了起来。镜头缓缓拉远，定格在这个被 lamp 暖光包裹着的、有些凌乱但充满欢笑的 living room 里。【音乐】温馨的钢琴曲推向高潮后缓缓收尾。【镜头】全景，画面逐渐变暗（Fade out）。", "", "", "", "", ""],
]

# --------------------------
# 生成Excel文件
# --------------------------
wb = Workbook()
ws = wb.active
ws.title = "完整剧本"

# 设置列宽
column_widths = [15, 15, 70, 10, 50, 20, 20, 20]
for i, width in enumerate(column_widths):
    ws.column_dimensions[chr(ord('A') + i)].width = width

# 写入表头
for col, header in enumerate(script_data[0], 1):
    cell = ws.cell(row=1, column=col, value=header)
    cell.font = Font(bold=True, size=11)
    cell.alignment = Alignment(vertical="top", wrap_text=True)
    cell.fill = PatternFill(start_color="EFEFEF", end_color="EFEFEF", fill_type="solid")

# 写入数据
bold_font = Font(bold=True)
red_bold_font = Font(bold=True, color="FF554A")

for row_idx, row_data in enumerate(script_data[1:], 2):  # row 1 is header
    for col_idx, cell_value in enumerate(row_data, 1):
        cell = ws.cell(row=row_idx, column=col_idx, value=cell_value)
        cell.alignment = Alignment(vertical="top", wrap_text=True)
        
        # 处理富文本知识点标注
        if col_idx == 5 and isinstance(cell_value, str) and ("**" in cell_value or "<red>" in cell_value):
            # 解析富文本
            rich_text = CellRichText()
            current_text = cell_value
            current_text = current_text.replace("<red>", "__RED_START__").replace("</red>", "__RED_END__")
            
            parts = []
            while current_text:
                if "**" in current_text:
                    before, rest = current_text.split("**", 1)
                    if before:
                        parts.append(TextBlock(text=before))
                    if "__RED_START__" in rest and rest.split("**")[0].startswith("__RED_START__"):
                        # 红色加粗
                        rest = rest.replace("__RED_START__", "", 1)
                        bold_text, rest = rest.split("**", 1)
                        parts.append(TextBlock(red_bold_font, text=bold_text))
                        if "__RED_END__" in rest:
                            rest = rest.replace("__RED_END__", "", 1)
                    else:
                        # 普通加粗
                        bold_text, rest = rest.split("**", 1)
                        parts.append(TextBlock(bold_font, text=bold_text))
                    current_text = rest
                else:
                    parts.append(TextBlock(text=current_text))
                    current_text = ""
            
            cell.value = rich_text

# 保存文件
wb.save(output_path)
print(f"✅ 按官方格式生成完成！文件路径：{output_path}")
print(f"  总行数：{len(script_data)}")
print(f"  对话行数：{sum(1 for row in script_data if row[3] and row[4])}")
print(f"  互动数量：{sum(1 for row in script_data if '互动' in str(row[0]))}")
