###############################################################################
# ---------------- 15-112 Recitation #8: Event-Based Animation ---------------- #

# This is a starter file of the problems we did in recitation. A good way to
# use this file is to try to re-write problems you saw in recitation from
# scratch. This way, you can test your understanding and ask on Piazza or
# office hours if you have questions :)

# --------------------------------------------------------------------------- #
from tkinter import *
###############################################################################
# Simple Animation
###############################################################################
# LEARNING OBJECTIVES
# What is MVC?
# How do we use data to track variables in an animation?
# What do keyPressed and mousePressed do?

def init(data):
	pass

def mousePressed(event, data):
	pass

def keyPressed(event, data):
	pass

def redrawAll(canvas, data):
	pass


###############################################################################
# isLatinSquare
###############################################################################
# LEARNING OBJECTIVES
# Review traversing 2D lists
# Review loops

'''
A latin square can be represented as a N x N 2D list with N different elements,
where each element occurs exactly once in each column and each row. Write a 
function isLatinSquare(a) that takes in a possibly ragged 2D-list that returns
true if a is a latin square.
'''

def isLatinSquare(L):
	return 42


####################################
# use the run function as-is
####################################

def run(width=300, height=300):
    def redrawAllWrapper(canvas, data):
        canvas.delete(ALL)
        canvas.create_rectangle(0, 0, data.width, data.height,
                                fill='white', width=0)
        redrawAll(canvas, data)
        canvas.update()

    def mousePressedWrapper(event, canvas, data):
        mousePressed(event, data)
        redrawAllWrapper(canvas, data)

    def keyPressedWrapper(event, canvas, data):
        keyPressed(event, data)
        redrawAllWrapper(canvas, data)

    # Set up data and call init
    class Struct(object): pass
    data = Struct()
    data.width = width
    data.height = height
    root = Tk()
    init(data)
    # create the root and the canvas
    canvas = Canvas(root, width=data.width, height=data.height)
    canvas.pack()
    # set up events
    root.bind("<Button-1>", lambda event:
                            mousePressedWrapper(event, canvas, data))
    root.bind("<Key>", lambda event:
                            keyPressedWrapper(event, canvas, data))
    redrawAll(canvas, data)
    # and launch the app
    root.mainloop()  # blocks until window is closed
    print("bye!")

run(500, 500)