#!/usr/bin/env python3

"""Build a presentation to test the classroom's library with.

Representative test material, NOT the pastor's own files. The shape is
copied from the real Bible study decks and nothing else about them is:

    slide 1   a generic opening, the same on every session
    slide 2   the one that names the session
    slide 3+  the session's content

That first pair is the whole reason Quick Look lets a teacher choose
which slide becomes the library thumbnail. A library that assumed slide
one would show every session under the same picture.

Writes a flat ODF presentation, which LibreOffice then converts to
whatever the test needs:

    tools/lab/make-presentation --slides 12 --session 7 out.fodp
    soffice --headless --convert-to pptx out.fodp
    soffice --headless --convert-to pdf  out.fodp
"""

import argparse
import html
import sys

HEADER = """<?xml version="1.0" encoding="UTF-8"?>
<office:document
 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
 xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
 office:version="1.3"
 office:mimetype="application/vnd.oasis.opendocument.presentation">
 <office:automatic-styles>
  <style:style style:name="title" style:family="presentation">
   <style:graphic-properties draw:fill="none" draw:stroke="none"/>
   <style:paragraph-properties fo:text-align="center"/>
   <style:text-properties fo:font-size="40pt" fo:font-weight="bold"
    style:font-name-complex="Liberation Sans"/>
  </style:style>
  <style:style style:name="body" style:family="presentation">
   <style:graphic-properties draw:fill="none" draw:stroke="none"/>
   <style:text-properties fo:font-size="24pt"/>
  </style:style>
 </office:automatic-styles>
 <office:body>
  <office:presentation>
"""

FOOTER = """  </office:presentation>
 </office:body>
</office:document>
"""

PAGE = """   <draw:page draw:name="{name}" draw:master-page-name="Default">
    <draw:frame draw:style-name="title" draw:layer="layout"
     svg:width="24cm" svg:height="4cm" svg:x="1.4cm" svg:y="3cm">
     <draw:text-box>{title}</draw:text-box>
    </draw:frame>
    <draw:frame draw:style-name="body" draw:layer="layout"
     svg:width="24cm" svg:height="8cm" svg:x="1.4cm" svg:y="8cm">
     <draw:text-box>{body}</draw:text-box>
    </draw:frame>
   </draw:page>
"""

# Deliberately ordinary words. Nothing here is anybody's lesson.
POINTS = (
    "Read the passage aloud together",
    "What does this tell us about God?",
    "Where do you see this in your own week?",
    "One thing to carry into Sunday",
)


def paragraphs(lines):
    return "".join(
        f"<text:p>{html.escape(line)}</text:p>" for line in lines
    )


def build(slides, session):
    pages = []

    # Slide 1 — the generic opening every session shares.
    pages.append(PAGE.format(
        name="page1",
        title=paragraphs(["Wednesday Bible Study"]),
        body=paragraphs(["River of Glory", "Welcome"]),
    ))

    # Slide 2 — the one a teacher has to be able to pick as the thumbnail.
    pages.append(PAGE.format(
        name="page2",
        title=paragraphs([f"Session {session}"]),
        body=paragraphs([f"Session {session} of the autumn study",
                         "Tonight's passage and questions"]),
    ))

    for number in range(3, slides + 1):
        pages.append(PAGE.format(
            name=f"page{number}",
            title=paragraphs([f"Point {number - 2}"]),
            body=paragraphs([
                POINTS[(number - 3) % len(POINTS)],
                f"(slide {number} of {slides})",
            ]),
        ))

    return HEADER + "".join(pages) + FOOTER


def main():
    parser = argparse.ArgumentParser(
        description="Build representative Bible study test material.")
    parser.add_argument("output", help="where to write the .fodp")
    parser.add_argument("--slides", type=int, default=10,
                        help="how many slides in total (minimum 2)")
    parser.add_argument("--session", type=int, default=7,
                        help="the session number slide 2 announces")
    arguments = parser.parse_args()

    if arguments.slides < 2:
        print("a test deck needs at least the opening and the session slide",
              file=sys.stderr)
        return 2

    with open(arguments.output, "w", encoding="utf-8") as handle:
        handle.write(build(arguments.slides, arguments.session))

    print(f"{arguments.output}: {arguments.slides} slides, session "
          f"{arguments.session}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
