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
import os

img_dir = "/Users/lingochen/Desktop/pani_images"

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

# Colors
DARK = RGBColor(0x0F, 0x17, 0x2A)
BLUE = RGBColor(0x38, 0xBD, 0xF8)
TEAL = RGBColor(0x14, 0xB8, 0xA6)
WHITE = RGBColor(0xFF, 0xFF, 0xFF)
LGRAY = RGBColor(0xCB, 0xD5, 0xE1)
MGRAY = RGBColor(0x94, 0xA3, 0xB8)
CARD = RGBColor(0x1E, 0x29, 0x3B)
AMBER = RGBColor(0xF5, 0x9E, 0x0B)

def bg(s):
    s.background.fill.solid(); s.background.fill.fore_color.rgb = DARK

def bar(s, c):
    sh = s.shapes.add_shape(MSO_SHAPE.RECTANGLE, 0, 0, prs.slide_width, Inches(0.08))
    sh.fill.solid(); sh.fill.fore_color.rgb = c; sh.line.fill.background()

def card(s, x, y, w, h):
    sh = s.shapes.add_shape(MSO_SHAPE.RECTANGLE, x, y, w, h)
    sh.fill.solid(); sh.fill.fore_color.rgb = CARD; sh.line.fill.background()
    return sh

def txt(s, x, y, w, h, text, sz=18, c=WHITE, b=False, align=PP_ALIGN.LEFT):
    tb = s.shapes.add_textbox(x, y, w, h)
    tf = tb.text_frame; tf.word_wrap = True
    p = tf.paragraphs[0]; p.text = text
    p.font.size = Pt(sz); p.font.color.rgb = c; p.font.bold = b
    p.font.name = "Microsoft JhengHei"; p.alignment = align
    return tf

def add_p(tf, text, sz=16, c=LGRAY, b=False, sp=Pt(6)):
    p = tf.add_paragraph(); p.text = text
    p.font.size = Pt(sz); p.font.color.rgb = c; p.font.bold = b
    p.font.name = "Microsoft JhengHei"; p.space_before = sp
    return p

def img(s, path, x, y, w=None, h=None):
    if os.path.exists(path):
        kwargs = {}
        if w: kwargs["width"] = w
        if h: kwargs["height"] = h
        s.shapes.add_picture(path, x, y, **kwargs)

# ===== SLIDE 1: Title + Gas Sensing + Images =====
sl = prs.slides.add_slide(prs.slide_layouts[6]); bg(sl); bar(sl, BLUE)

txt(sl, Inches(1), Inches(0.3), Inches(11), Inches(0.8),
    "聚苯胺簡介", sz=40, c=WHITE, b=True, align=PP_ALIGN.CENTER)

# Place slide1 images
img(sl, f"{img_dir}/slide1_img0.png", Inches(0.5), Inches(1.3), Inches(5.5), Inches(5.5))
img(sl, f"{img_dir}/slide1_img2.jpg", Inches(6.5), Inches(1.3), Inches(6.3), Inches(4))
img(sl, f"{img_dir}/slide1_img1.png", Inches(6.5), Inches(5.5), Inches(3), Inches(1.5))

# Text description overlay
card(sl, Inches(0.5), Inches(5.8), Inches(5.5), Inches(1.3))
tf = txt(sl, Inches(0.8), Inches(5.9), Inches(5), Inches(1.2),
         "🔬 氣體感測應用", sz=16, c=BLUE, b=True)
add_p(tf, "濃度 1PPM，靈敏度可達 0.01PPM", sz=13, c=LGRAY)
add_p(tf, "曲線向下部分即為偵測到氣體時的訊號", sz=13, c=MGRAY)

# ===== SLIDE 2: Specs =====
sl = prs.slides.add_slide(prs.slide_layouts[6]); bg(sl); bar(sl, TEAL)
txt(sl, Inches(0.8), Inches(0.3), Inches(10), Inches(0.6),
    "導電聚苯胺成分表", sz=30, c=WHITE, b=True)

specs = [
    ("中文名稱", "聚苯胺（導電態聚苯胺、聚乙烯苯胺）"),
    ("英文名稱", "Polyaniline"),
    ("CAS", "5612-44-2"),
    ("純度", "Purity ≧ 98%；99.0 wt% 以上"),
    ("儲存", "室溫（RT）"),
    ("外觀", "粉末狀，深綠色或淺綠色粉末"),
    ("分子量", "50,000 ~ 60,000"),
    ("電導率", "7.5 S/cm"),
    ("粒徑", "< 30 μm（鐳射細微性儀）"),
    ("摻雜率", "> 30%（摩爾比）"),
    ("分散性", "可分散於二甲苯、丁醇等溶劑，濃度 > 10 wt%"),
    ("可加工溫度", "低於 150°C"),
    ("分解溫度", "空氣中超過 300°C"),
    ("表觀密度", "0.3 ~ 0.5 g/cm³"),
]

for i, (label, value) in enumerate(specs):
    col = 0 if i < 7 else 1
    row = i if i < 7 else i - 7
    x = Inches(0.8) if col == 0 else Inches(6.8)
    y = Inches(1.2) + row * Inches(0.85)
    card(sl, x, y, Inches(5.5), Inches(0.75))
    txt(sl, x + Inches(0.3), y + Inches(0.05), Inches(1.8), Inches(0.3),
        label, sz=12, c=TEAL, b=True)
    txt(sl, x + Inches(0.3), y + Inches(0.35), Inches(5), Inches(0.35),
        value, sz=13, c=LGRAY)

# ===== SLIDE 3: Conductivity Test + 3 Images =====
sl = prs.slides.add_slide(prs.slide_layouts[6]); bg(sl); bar(sl, AMBER)
txt(sl, Inches(0.8), Inches(0.3), Inches(10), Inches(0.6),
    "實際導電測試（壓成塊狀）", sz=30, c=WHITE, b=True)

# 3 images from slide3
img(sl, f"{img_dir}/slide3_img4.png", Inches(0.3), Inches(1.2), Inches(4.3), Inches(5.8))
img(sl, f"{img_dir}/slide3_img3.png", Inches(4.8), Inches(1.2), Inches(4.3), Inches(5.8))
img(sl, f"{img_dir}/slide3_img5.jpg", Inches(9.3), Inches(1.2), Inches(3.8), Inches(5.8))

# ===== SLIDE 4: Pricing =====
sl = prs.slides.add_slide(prs.slide_layouts[6]); bg(sl); bar(sl, BLUE)
txt(sl, Inches(0.8), Inches(0.3), Inches(10), Inches(0.6),
    "簡易報價", sz=30, c=WHITE, b=True)
txt(sl, Inches(0.8), Inches(1.0), Inches(10), Inches(0.4),
    "以上價格均為未稅價", sz=16, c=MGRAY)

for i, (qty, price, note, accent) in enumerate([
    ("100g", "$3,500", "少量試用", BLUE),
    ("500g", "$9,500", "量產測試", TEAL)
]):
    x = Inches(2) + i * Inches(5); y = Inches(2)
    card(sl, x, y, Inches(4), Inches(4.5))
    sh = sl.shapes.add_shape(MSO_SHAPE.RECTANGLE, x, y, Inches(4), Inches(0.06))
    sh.fill.solid(); sh.fill.fore_color.rgb = accent; sh.line.fill.background()
    txt(sl, x, y + Inches(0.5), Inches(4), Inches(0.6), qty, sz=24, c=MGRAY, align=PP_ALIGN.CENTER)
    txt(sl, x, y + Inches(1.3), Inches(4), Inches(1), price, sz=44, c=WHITE, b=True, align=PP_ALIGN.CENTER)
    txt(sl, x, y + Inches(3), Inches(4), Inches(0.5), note, sz=16, c=accent, align=PP_ALIGN.CENTER)

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