import random
import os
from palettes import randomPalette
from util import getTurtleAndScreen
# Fibonacci tree.
# CAC, 4/24/2023

def fibTree(turtle,length,angle,level,colors,typeA):
    realAngle=angle/2
    if level >0: # If level <=0, draw nothing.
        if typeA:
            turtle.color(random.choice(colors))
            turtle.forward(length)
            fibTree(turtle,length,angle,level-1,colors,False)
        else:
            x,y=turtle.pos()
            heading = turtle.heading()
            turtle.left(realAngle)
            turtle.color(random.choice(colors))
            turtle.forward(length)
            fibTree(turtle,length,angle,level-1,colors,True)
            turtle.penup()
            turtle.goto(x,y)
            turtle.setheading(heading-realAngle)
            turtle.pendown()
            turtle.color(random.choice(colors))
            turtle.forward(length)
            fibTree(turtle,length,angle,level-1,colors,False)

def Draw():
    level = random.randint(4,15)
    angle = random.randint(45,90)
    title = "Fibonacci Tree "+str(level)+" "+str(angle)
    width = 800
    height = 600
    filename = os.path.basename(__file__)[0:-3]
    turtle, screen = getTurtleAndScreen(title,width,height,filename,moveWorld=True)

    colors = randomPalette()
    bg = random.choice(colors)
    colors.remove(bg)
    screen.bgcolor(bg)
    turtle.pensize(2)

    length = height/(1.5*level)

    turtle.penup()
    turtle.goto(width/2,height/4)
    turtle.pendown()

    turtle.setheading(90)
    fibTree(turtle,length,angle,level,colors,True)

    turtle.penup()

    screen.mainloop()
# ----------------------------------------------------------------
if __name__ == '__main__':
   Draw()
