#!/usr/bin/env python3
"""
3D Charts Showcase — column3d / bar3d / pie3d / line3d / area3d with view3d, gapdepth, shape.

Generates: charts-3d.pptx

  Slide 1  3D families            column3d / bar3d / pie3d / line3d
  Slide 2  area3d & stacked 3D    area3d / stackedColumn3d / percentStackedColumn3d / line3d stacked
  Slide 3  view3d                 different rotX,rotY,perspective angles
  Slide 4  gapdepth               0 / 50 / 150 / 300 (3D bar/column/line/area only)
  Slide 5  bar shapes             box / cylinder / cone / pyramid (bar3d / column3d)
  Slide 6  Title & legend
  Slide 7  Series styling         colors, gradient, transparency, outline, shadow
  Slide 8  Presets

SDK twin of charts-3d.sh (officecli CLI). Both produce an equivalent
charts-3d.pptx. This one drives the **officecli Python SDK**
(`pip install officecli-sdk`): one resident is started and every slide, title
shape and chart is shipped over the named pipe in a single `doc.batch(...)`
round-trip. Each item is the same `{"command","parent","type","props"}` dict
you'd put in an `officecli batch` list.

Usage:
  pip install officecli-sdk          # plus the `officecli` binary on PATH
  python3 charts-3d.py
"""

import os
import sys

# --- locate the SDK: prefer an installed `officecli-sdk`, else the in-repo copy
try:
    import officecli  # pip install officecli-sdk
except ImportError:
    sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "..", "..", "..", "sdk", "python"))
    import officecli

FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "charts-3d.pptx")

# ------------------------------------------------------------------ data + layout
CATS = "Q1,Q2,Q3,Q4"
D2 = "East:120,135,148,162;West:95,108,115,128"
D3 = "East:120,135,148,162;South:95,108,115,128;West:80,90,98,110"
PIE_CATS = "North,South,East,West"
PIE_D = "Share:30,25,28,17"

TL = {"x": "0.3in", "y": "1.05in", "width": "6.1in", "height": "3in"}
TR = {"x": "6.95in", "y": "1.05in", "width": "6.1in", "height": "3in"}
BL = {"x": "0.3in", "y": "4.25in", "width": "6.1in", "height": "3in"}
BR = {"x": "6.95in", "y": "4.25in", "width": "6.1in", "height": "3in"}

# slide cursor — bumped by slide(); chart()/title() target /slide[<_slide>].
_slide = 0


def slide(title):
    """One `add slide` item + its title `add shape` item, in batch-shape."""
    global _slide
    _slide += 1
    return [
        {"command": "add", "parent": "/", "type": "slide", "props": {}},
        {"command": "add", "parent": f"/slide[{_slide}]", "type": "shape",
         "props": {"text": title, "size": 24, "bold": "true", "autoFit": "normal",
                   "x": "0.5in", "y": "0.3in", "width": "12.3in", "height": "0.6in"}},
    ]


def chart(box, props):
    """One `add chart` item (box geometry + chart props), in batch-shape."""
    return {"command": "add", "parent": f"/slide[{_slide}]", "type": "chart",
            "props": {**box, **props}}


print(f"Building {FILE} ...")

with officecli.create(FILE, "--force") as doc:
    items = []

    # ---- Slide 1: 3D families ----
    items += slide("3D families — column3d / bar3d / pie3d / line3d")
    items += [
        chart(TL, {"chartType": "column3d", "title": "column3d", "legend": "bottom",
                   "categories": CATS, "data": D2}),
        chart(TR, {"chartType": "bar3d", "title": "bar3d", "legend": "bottom",
                   "categories": CATS, "data": D2}),
        chart(BL, {"chartType": "pie3d", "title": "pie3d", "legend": "right",
                   "categories": PIE_CATS, "data": PIE_D}),
        chart(BR, {"chartType": "line3d", "title": "line3d", "legend": "bottom",
                   "categories": CATS, "data": D2}),
    ]

    # ---- Slide 2: area3d & stacked 3D ----
    items += slide("area3d & stacked 3D")
    items += [
        chart(TL, {"chartType": "area3d", "title": "area3d", "legend": "bottom",
                   "categories": CATS, "data": D2}),
        chart(TR, {"chartType": "stackedColumn3d", "title": "stackedColumn3d",
                   "legend": "bottom", "categories": CATS, "data": D3}),
        chart(BL, {"chartType": "percentStackedColumn3d", "title": "percentStackedColumn3d",
                   "legend": "bottom", "categories": CATS, "data": D3}),
        chart(BR, {"chartType": "stackedBar3d", "title": "stackedBar3d",
                   "legend": "bottom", "categories": CATS, "data": D3}),
    ]

    # ---- Slide 3: view3d — rotX,rotY,perspective angles ----
    items += slide("view3d — rotX,rotY,perspective angles")
    items += [
        chart(TL, {"chartType": "column3d", "title": "view3d=15,20,30", "view3d": "15,20,30",
                   "legend": "none", "categories": CATS, "data": D2}),
        chart(TR, {"chartType": "column3d", "title": "view3d=30,40,15", "view3d": "30,40,15",
                   "legend": "none", "categories": CATS, "data": D2}),
        chart(BL, {"chartType": "column3d", "title": "view3d=20", "view3d": "20",
                   "legend": "none", "categories": CATS, "data": D2}),
        chart(BR, {"chartType": "pie3d", "title": "pie3d view3d=40,30,30", "view3d": "40,30,30",
                   "legend": "right", "categories": PIE_CATS, "data": PIE_D}),
    ]

    # ---- Slide 4: gapdepth — 0 / 50 / 150 / 300 ----
    items += slide("gapdepth — 0 / 50 / 150 / 300")
    for box, g in zip([TL, TR, BL, BR], [0, 50, 150, 300]):
        items.append(chart(box, {"chartType": "column3d", "title": f"gapdepth={g}",
                                  "gapdepth": str(g), "legend": "none",
                                  "categories": CATS, "data": D2}))

    # ---- Slide 5: 3D bar shapes — box / cylinder / cone / pyramid ----
    items += slide("3D bar shapes — box / cylinder / cone / pyramid")
    for box, s in zip([TL, TR, BL, BR], ["box", "cylinder", "cone", "pyramid"]):
        items.append(chart(box, {"chartType": "bar3d", "shape": s, "title": f"shape={s}",
                                  "legend": "none", "categories": CATS, "data": D2}))

    # ---- Slide 6: Title & legend ----
    items += slide("Title & legend")
    items += [
        chart(TL, {"chartType": "column3d", "title": "Styled title", "title.font": "Georgia",
                   "title.size": "20", "title.color": "4472C4", "title.bold": "true",
                   "legend": "bottom", "categories": CATS, "data": D2}),
        chart(TR, {"chartType": "column3d", "title": "legend=top + legendFont", "legend": "top",
                   "legendFont": "10:333333:Calibri", "categories": CATS, "data": D2}),
        chart(BL, {"chartType": "column3d", "title": "legend.overlay=true", "legend": "topRight",
                   "legend.overlay": "true", "categories": CATS, "data": D2}),
        chart(BR, {"chartType": "column3d", "autotitledeleted": "true", "legend": "none",
                   "categories": CATS, "data": D2}),
    ]

    # ---- Slide 7: Series styling — colors, gradient, transparency, outline, shadow ----
    items += slide("Series styling — colors, gradient, transparency, outline, shadow")
    items += [
        chart(TL, {"chartType": "column3d", "title": "colors + seriesoutline",
                   "colors": "4472C4,ED7D31", "seriesoutline": "000000:0.5",
                   "legend": "bottom", "categories": CATS, "data": D2}),
        chart(TR, {"chartType": "column3d", "title": "gradient + seriesshadow",
                   "gradient": "FF6600-FFCC00", "seriesshadow": "000000-5-45-3-50",
                   "legend": "none", "categories": CATS, "data": D2}),
        chart(BL, {"chartType": "column3d", "title": "transparency=30", "transparency": "30",
                   "legend": "bottom", "categories": CATS, "data": D2}),
        chart(BR, {"chartType": "column3d", "title": "per-series gradients",
                   "gradients": "FF0000-0000FF;00FF00-FFFF00",
                   "legend": "bottom", "categories": CATS, "data": D2}),
    ]

    # ---- Slide 8: Presets — preset bundles on 3D charts ----
    items += slide("Presets — preset bundles on 3D charts")
    for box, p in zip([TL, TR, BL, BR], ["minimal", "dark", "corporate", "colorful"]):
        items.append(chart(box, {"chartType": "column3d", "preset": p, "title": f"preset={p}",
                                  "view3d": "15,20,30", "legend": "bottom",
                                  "categories": CATS, "data": D2}))

    doc.batch(items)
    print(f"  added {_slide} slides, {len(items)} items")

print(f"Generated: {FILE}  ({_slide} slides)")
