#################################################
# 15-112-m18 hw14
# Your Name:
# Your Andrew ID:
# Your Section:
#################################################

from tkinter import *
import math

####################################
# Crop, AppleTree, and Corn Classes
####################################

class Crop(object):
    pass

class AppleTree(Crop):
    pass

class Corn(Crop):
    pass

####################################
# Animation Core Components
####################################

def init(data):
    data.crops = []
    data.selectedCrop = "corn"
    data.money = 5
    data.timerDelay = 300

def mousePressed(event, data):
    newCrop = getNewCrop(data,event.x, event.y)
    if canBuyCrop(data, newCrop):
        plantCrop(data, newCrop)

def timerFired(data):
    growCrops(data)
    checkForCropIntersections(data)

def keyPressed(event, data):
    if event.keysym == "c":
        data.selectedCrop = "corn"
    if event.keysym == "a":
        data.selectedCrop = "apple"
    if event.keysym == "h":
        harvestCrop(data)

def redrawAll(canvas, data):
    drawBackground(canvas,data)
    drawMoney(canvas,data)
    drawAllCrops(canvas,data)

####################################
# Drawing Helper Functions
####################################

def drawBackground(canvas,data):
    canvas.create_rectangle(0,0,data.width, data.height,
        fill="saddle brown", width=0)

def drawMoney(canvas, data):
    canvas.create_text(20,20, text=str(data.money))

def drawAllCrops(canvas,data):
    for crop in data.crops:
        crop.draw(canvas)

####################################
# TimerFired Helper Functions
####################################

def growCrops(data):
    for crop in data.crops:
        if crop.isDead():
            continue
        if crop.isFullyGrown():
            continue
        crop.grow()

def checkForCropIntersections(data):
    for crop1 in data.crops:
        for crop2 in data.crops:
            if crop1 == crop2:
                continue
            if crop1.intersects(crop2):
                crop1.dead = True
                crop2.dead = True

####################################
# KeyPressed Helper Functions
####################################

def harvestCrop(data):
    for crop in data.crops:
        if crop.isFullyGrown() and not crop.isDead():
            data.crops.remove(crop)
            data.money += crop.getSellPrice()
            return

####################################
# MousePressed Helper Functions
####################################

def canBuyCrop(data, newCrop):
    return data.money - newCrop.getPurchasePrice() >= 0

def getNewCrop(data,x,y):
    if data.selectedCrop== "corn":
        newCrop = Corn(x,y)
    elif data.selectedCrop == "apple":
        newCrop = AppleTree(x,y)
    return newCrop

def plantCrop(data, newCrop):
    data.crops.append(newCrop)
    data.money -= newCrop.getPurchasePrice()

####################################
# 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.configure(bd=0, highlightthickness=0)
    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,200)