# The following are textual title and chapter breaks for parts 1 and
# 2. 
part1 = """0 Titles
2:38 Wookies At Home
7:35 Pointless Dancing
10:45 Luke
14:45 Wookie Planet C
18:20 Cooking Show
23:30 Martial Law
27:45 Wookie Porn
35:10 Leia
37:00 Stormtroopers
42:40 Jefferson Starship"""

part2 = """0 Take the Upper Area!
1:15 Cartoon Part 1
8:43 Cartoon Part 2
11:15 Slowdown
18:15 Life on Tatooine
31:25 Chewbacca's Back
38:30 Life Day
44:56 Toy Commercial"""


def chapters(s):
    """
    Splits the string s into two lists, one of chapters and one of titles
    for each chapter.
    """
    
    lines = s.split("\n")
    chapters = []
    titles = []
    for l in lines:
        l = l.rstrip()
        ch, ti = l.split(' ', 1)

        chapters.append(ch)
        titles.append(ti)

    return chapters, titles

ch1, ti1 = chapters(part1)
ch2, ti2 = chapters(part2)

def part_menu(tn, titles):
    """
    Given a title and a list of chapters, make a menu for that title.
    
    @param tn: The number of the title we're making a menu for.
    @param titles: A list of strings, the chapter titles.
    """

    newmenu()
    background("xine_snapshot-2.png")
    moveto(75, 100)

    for i, ti in enumerate(titles):
        label(ti, action="jump title %d chapter %d;" % (tn, i + 1))

    rmoveto(500, 10)

    label("Back", action="jump menu 5;")


def keypad(message, choice, right, wrong):
    """
    Make a keypad on the screen. 
    
    @param message: A message displayed in the message area on the
    screen. 
    @param choice: The correct choice.
    @param right: The menu to jump to if the user gets the choice
    right.
    @param wrong: The menu to jump to if the user gets the choice
    wrong. 
    """

    xposes = (302, 352, 402)
    yposes = (200, 250, 300, 350)

    keypad = [
        ( 1, 2, 3 ),
        ( 4, 5, 6 ),
        ( 7, 8, 9 ),
        ( None, 0, None ),
        ]

    moveto(352, 125)
    text(message, halign="center", valign="center")

    for row, ypos in zip(keypad, yposes):
        for key, xpos in zip(row, xposes):
            if key is None:
                continue

            moveto(xpos, ypos)

            if key == choice:
                action = "jump menu %d;" % right
            else:
                action = "jump menu %d;" % wrong
                
            label("%d" % key,
                  action=action,
                  halign="center", valign="center")


font("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 24)

# Menu 1
keypad("Enter Passcode", 1, 2, 1)

# Menu 2
newmenu()
keypad("1", 9, 3, 1)

# Menu 3
newmenu()
keypad("19", 7, 4, 1)

# Menu 4
newmenu()
keypad("197", 8, 5, 1)
            
# Menu 5
newmenu()

moveto(75, 300)
background("xine_snapshot-2.png")

title("SWHS-1.dvd.mpg", chapters=",".join(ch1), post="jump titleset 2 title 1;")
title("SWHS-2.dvd.mpg", chapters=",".join(ch2), post="call vmgm menu 5;")
 
label("Play", action="jump title 1;")
label("Chapter List (Part 1)", action="jump menu 6;")
label("Chapter List (Part 2)", action="jump menu 7;")

# Menu 6
part_menu(1, ti1)

# Menu 7
part_menu(2, ti2)