from pptx import Presentation
from pptx.util import Inches, Pt, Emu
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.enum.shapes import MSO_SHAPE
import os

prs = Presentation()
prs.slide_width = Inches(13.333)
prs.slide_height = Inches(7.5)

# Color scheme - scientific/technical
DARK_BG = RGBColor(0x0F, 0x17, 0x2A)      # Dark navy
ACCENT_BLUE = RGBColor(0x38, 0xBD, 0xF8)   # Bright blue
ACCENT_TEAL = RGBColor(0x14, 0xB8, 0xA6)   # Teal
WHITE = RGBColor(0xFF, 0xFF, 0xFF)
LIGHT_GRAY = RGBColor(0xCB, 0xD5, 0xE1)
MID_GRAY = RGBColor(0x94, 0xA3, 0xB8)
CARD_BG = RGBColor(0x1E, 0x29, 0x3B)
HIGHLIGHT = RGBColor(0xF5, 0x9E, 0x0B)     # Amber accent

def add_bg(slide, color=DARK_BG):
    bg = slide.background
    fill = bg.fill
    fill.solid()
    fill.fore_color.rgb = color

def add_shape(slide, left, top, width, height, color, alpha=None):
    shape = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)
    shape.fill.solid()
    shape.fill.fore_color.rgb = color
    shape.line.fill.background()
    return shape

def add_text_box(slide, left, top, width, height, text, font_size=18, color=WHITE, bold=False, align=PP_ALIGN.LEFT, font_name="Microsoft JhengHei"):
    txBox = slide.shapes.add_textbox(left, top, width, height)
    tf = txBox.text_frame
    tf.word_wrap = True
    p = tf.paragraphs[0]
    p.text = text
    p.font.size = Pt(font_size)
    p.font.color.rgb = color
    p.font.bold = bold
    p.font.name = font_name
    p.alignment = align
    return txBox

def add_para(tf, text, font_size=16, color=LIGHT_GRAY, bold=False, space_before=Pt(8), font_name="Microsoft JhengHei"):
    p = tf.add_paragraph()
    p.text = text
    p.font.size = Pt(font_size)
    p.font.color.rgb = color
    p.font.bold = bold
    p.font.name = font_name
    p.space_before = space_before
    return p

# ===== SLIDE 1: Title =====
slide = prs.slides.add_slide(prs.slide_layouts[6])  # Blank
add_bg(slide)

# Accent bar at top
add_shape(slide, Inches(0), Inches(0), prs.slide_width, Inches(0.08), ACCENT_BLUE)

# Title
add_text_box(slide, Inches(1.5), Inches(1.8), Inches(10), Inches(1.2),
             "聚苯胺簡介", font_size=48, color=WHITE, bold=True, align=PP_ALIGN.CENTER)

# Subtitle
add_text_box(slide, Inches(1.5), Inches(3.0), Inches(10), Inches(0.8),
             "Polyaniline (PANI) — 導電高分子材料", font_size=24, color=ACCENT_TEAL, align=PP_ALIGN.CENTER)

# Divider line
add_shape(slide, Inches(5), Inches(4.0), Inches(3.333), Inches(0.04), ACCENT_BLUE)

# Key feature card
card = add_shape(slide, Inches(3), Inches(4.5), Inches(7.333), Inches(2.2), CARD_BG)
card.shadow.inherit = False

add_text_box(slide, Inches(3.5), Inches(4.7), Inches(6.333), Inches(0.5),
             "🔬 氣體感測應用", font_size=20, color=ACCENT_BLUE, bold=True)

tb = add_text_box(slide, Inches(3.5), Inches(5.3), Inches(6.333), Inches(1.2),
             "偵測靈敏度：1 PPM（可達 0.01 PPM）", font_size=18, color=LIGHT_GRAY)
tf = tb.text_frame
add_para(tf, "圖中曲線向下部分即為偵測到氣體時的訊號變化", font_size=14, color=MID_GRAY)

# ===== SLIDE 2: Product Specs =====
slide = prs.slides.add_slide(prs.slide_layouts[6])
add_bg(slide)
add_shape(slide, Inches(0), Inches(0), prs.slide_width, Inches(0.08), ACCENT_TEAL)

add_text_box(slide, Inches(0.8), Inches(0.4), Inches(8), Inches(0.7),
             "導電聚苯胺成分表", font_size=32, color=WHITE, bold=True)

# Specs in card layout
specs = [
    ("中文名稱", "聚苯胺（導電態聚苯胺、聚乙烯苯胺）"),
    ("英文名稱", "Polyaniline"),
    ("CAS", "5612-44-2"),
    ("純度", "≥ 98%（Purity ≧ 98%）"),
    ("分子量", "50,000 ~ 60,000"),
    ("電導率", "7.5 S/cm"),
    ("粒徑", "< 30 μm（鐳射細微性儀）"),
    ("儲存", "室溫（RT）"),
    ("外觀", "深綠色或淺綠色粉末"),
    ("摻雜率", "> 30%（摩爾比）"),
    ("分散性", "可分散於二甲苯、丁醇等溶劑"),
    ("濃度限制", "> 10 wt% 可分散"),
]

# Two columns of spec cards
col1_x = Inches(0.8)
col2_x = Inches(6.8)
start_y = Inches(1.4)
card_h = Inches(0.75)
gap = Inches(0.12)

for i, (label, value) in enumerate(specs):
    col = 0 if i < 6 else 1
    row = i if i < 6 else i - 6
    x = col1_x if col == 0 else col2_x
    y = start_y + row * (card_h + gap)
    
    # Card background
    card = add_shape(slide, x, y, Inches(5.5), card_h, CARD_BG)
    
    # Label
    add_text_box(slide, x + Inches(0.3), y + Inches(0.1), Inches(1.8), Inches(0.35),
                 label, font_size=13, color=ACCENT_TEAL, bold=True)
    
    # Value
    add_text_box(slide, x + Inches(0.3), y + Inches(0.38), Inches(5), Inches(0.35),
                 value, font_size=14, color=LIGHT_GRAY)

# ===== SLIDE 3: Conductivity Test =====
slide = prs.slides.add_slide(prs.slide_layouts[6])
add_bg(slide)
add_shape(slide, Inches(0), Inches(0), prs.slide_width, Inches(0.08), HIGHLIGHT)

add_text_box(slide, Inches(0.8), Inches(0.4), Inches(10), Inches(0.7),
             "實際導電測試", font_size=32, color=WHITE, bold=True)

add_text_box(slide, Inches(0.8), Inches(1.2), Inches(10), Inches(0.5),
             "壓成塊狀後進行導電性測量", font_size=20, color=MID_GRAY)

# Large feature cards
features = [
    ("⚡", "導電性能", "電導率達 7.5 S/cm\n適用於柔性電子、感測器等應用"),
    ("🧪", "材料特性", "粉末狀，深綠/淺綠色\n可分散於多種有機溶劑"),
    ("📏", "粒徑控制", "< 30 μm 超細粒徑\n確保均勻分散與高效能"),
]

for i, (icon, title, desc) in enumerate(features):
    x = Inches(0.8) + i * Inches(4.1)
    y = Inches(2.2)
    
    card = add_shape(slide, x, y, Inches(3.8), Inches(4.5), CARD_BG)
    
    add_text_box(slide, x + Inches(0.3), y + Inches(0.3), Inches(3.2), Inches(0.8),
                 icon, font_size=40, align=PP_ALIGN.CENTER)
    
    add_text_box(slide, x + Inches(0.3), y + Inches(1.2), Inches(3.2), Inches(0.5),
                 title, font_size=20, color=WHITE, bold=True, align=PP_ALIGN.CENTER)
    
    add_text_box(slide, x + Inches(0.3), y + Inches(1.9), Inches(3.2), Inches(2.2),
                 desc, font_size=15, color=LIGHT_GRAY, align=PP_ALIGN.CENTER)

# ===== SLIDE 4: Pricing =====
slide = prs.slides.add_slide(prs.slide_layouts[6])
add_bg(slide)
add_shape(slide, Inches(0), Inches(0), prs.slide_width, Inches(0.08), ACCENT_BLUE)

add_text_box(slide, Inches(0.8), Inches(0.4), Inches(10), Inches(0.7),
             "簡易報價", font_size=32, color=WHITE, bold=True)

add_text_box(slide, Inches(0.8), Inches(1.2), Inches(10), Inches(0.5),
             "以上價格均為未稅價", font_size=16, color=MID_GRAY)

# Price cards
prices = [
    ("100g", "$3,500", "少量試用"),
    ("500g", "$9,500", "量產測試"),
]

for i, (qty, price, note) in enumerate(prices):
    x = Inches(2) + i * Inches(5)
    y = Inches(2.5)
    
    # Card
    card = add_shape(slide, x, y, Inches(4), Inches(3.5), CARD_BG)
    
    # Accent top bar
    add_shape(slide, x, y, Inches(4), Inches(0.06), ACCENT_BLUE if i == 0 else ACCENT_TEAL)
    
    # Quantity
    add_text_box(slide, x + Inches(0.3), y + Inches(0.4), Inches(3.4), Inches(0.6),
                 qty, font_size=24, color=MID_GRAY, align=PP_ALIGN.CENTER)
    
    # Price
    add_text_box(slide, x + Inches(0.3), y + Inches(1.2), Inches(3.4), Inches(1),
                 price, font_size=44, color=WHITE, bold=True, align=PP_ALIGN.CENTER)
    
    # Note
    add_text_box(slide, x + Inches(0.3), y + Inches(2.5), Inches(3.4), Inches(0.5),
                 note, font_size=16, color=ACCENT_TEAL, align=PP_ALIGN.CENTER)

# Save
out = os.path.expanduser("~/Desktop/聚苯胺簡介_美編版.pptx")
prs.save(out)
print(f"OK → {out}")
