###############################################################################
# ---------------- 15-112 Recitation #10: Midterm 1 Review Part 1 ----------- #

# 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 *
import copy
###############################################################################
# Simple Animation
###############################################################################
# LEARNING OBJECTIVES
# Review animations, especially exam-style problems

'''
Write an animation that utilizes the run() function to do the following.
Initially, there is a 10x10 grid that scales with the size of the canvas. 
    There is also a 20 pixel margin at the top of the canvas. 
    Also, the bottom left cell is highlighted red. 
Every half second the highlighted cell changes in the following way:
    Starting in the bottom left cell, moves diagonally until it hits the top right cell, 
    where it proceeds to move straight down to the bottom right cell 
Each time it hits the bottom-right corner, it immediately starts over, 
jumping back to the bottom-left corner and sweeping up to the right again.
Display a score in the middle at the top of the window (in the margin). 
    The score is initially 5 (not 0).
Each time the user clicks the mouse, if the user clicks in the same cell
    which is highlighted red, the score increases by 1, 
    otherwise the score decreases by 1.
If the score gets to 10, the game is over, and the screen displays “You win!”;
If the score gets to 0, the game is over, and the screen displays “You lose!”
When the game is over, the red box is not seen, and mouse presses are ignored.
When the game is over, and only then, the user can press ‘n’ to start a new game, 
    and the game starts over in its initial state.

'''


def init(data):
	pass

def keyPressed(event, data):
	pass

def mousePressed(event, data):
	pass

def timerFired(data):
	pass

def redrawAll(canvas, data):
	pass

####################################
# 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)

    def timerFiredWrapper(canvas, data):
        timerFired(data)
        redrawAllWrapper(canvas, data)
        # pause, then call timerFired again
        canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
    # Set up data and call init
    class Struct(object): pass
    data = Struct()
    data.width = width
    data.height = height
    data.timerDelay = 100 # milliseconds
    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))
    timerFiredWrapper(canvas, data)
    # and launch the app
    root.mainloop()  # blocks until window is closed
    print("bye!")

run(500, 500)