#################################################################
# 15-112-n18 hw15
# Your Name:
# Your Andrew ID:
# Your Section:

#################################################################

# Copy-paste your Asteroid classes here!


#################################################################

# Starter Code begins here. Read and understand it!

import random, math

# Helper function for drawing the Rocket
def drawTriangle(canvas, cx, cy, angle, size, fill="black"):
    angleChange = 2*math.pi/3
    p1x, p1y = (cx + size*math.cos(angle), 
                    cy - size*math.sin(angle))
    p2x, p2y = (cx + size*math.cos(angle + angleChange), 
                    cy - size*math.sin(angle + angleChange))
    p3x, p3y = (cx, cy)
    p4x, p4y = (cx + size*math.cos(angle + 2*angleChange),
                    cy - size*math.sin(angle + 2*angleChange))
    
    canvas.create_polygon((p1x, p1y), (p2x, p2y), (p3x, p3y), (p4x, p4y),
                                                                 fill=fill)

# Read this class carefully! You'll need to call the methods!
class Rocket(object):
    def __init__(self, cx, cy):
        self.cx = cx
        self.cy = cy
        self.angle = 90

    def rotate(self, numDegrees):
        self.angle += numDegrees

    def makeBullet(self):
        offset = 10
        dx, dy = (offset*math.cos(math.radians(self.angle)), 
                            offset*math.sin(math.radians(self.angle)))
        speedLow, speedHigh = 20, 40

        return Bullet(self.cx+dx, self.cy-dy,
                self.angle,random.randint(speedLow, speedHigh))

    def draw(self, canvas):
        size = 30
        drawTriangle(canvas, self.cx, self.cy, 
            math.radians(self.angle), size, fill="green2")

# Read this class carefully! You'll need to call the methods!
class Bullet(object):
    def __init__(self, cx, cy, angle, speed=20):
        self.cx = cx
        self.cy = cy
        self.r = 5
        self.angle = angle
        self.speed = speed

    def moveBullet(self):
        dx = math.cos(math.radians(self.angle))*self.speed
        dy = math.sin(math.radians(self.angle))*self.speed
        self.cx, self.cy = self.cx + dx, self.cy - dy

    def isCollisionWithAsteroid(self, other):
        # in this case, other must be an asteroid
        if(not isinstance(other, Asteroid)):
            return False
        else:
            return (math.sqrt((other.cx - self.cx)**2 + 
                                (other.cy - self.cy)**2)
                                        < self.r + other.r) 

    def draw(self, canvas):
        cx, cy, r = self.cx, self.cy, self.r
        canvas.create_oval(cx - r, cy - r, cx + r, cy + r, 
            fill="white", outline=None)

    def onTimerFired(self, data):
        self.moveBullet()

#################################################################

from tkinter import *

####################################
# customize these functions
####################################

def init(data):
    # load data.xyz as appropriate
    data.rocket = Rocket(data.width//2, data.height//2)
    # what else do you need to store here? 
    

def mousePressed(event, data):
    # use event.x and event.y
    pass

def keyPressed(event, data):
    # use event.char and event.keysym
    # make the rocket rotate and fire!
    pass

def timerFired(data):
    # it might be a good idea to define onTimerFired methods in your classes...
    pass

def redrawAll(canvas, data):
    # draws the rocket and background
    canvas.create_rectangle(0, 0, data.width, data.height, fill="gray3")
    data.rocket.draw(canvas)
    # don't forget to draw asteroids and bullets!

#################################################################
# 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(600, 600)