import turtle
import tkinter
from tkinter import filedialog # file dialog
from tkinter.colorchooser import askcolor # color dialog
from time import sleep
from colour import Color as color # color module for gradient as color() function



def setColor(mode, value):
    if mode == 0:
        rgb = round(value * 2.55)
        t.color((rgb, rgb, rgb))
    elif mode == 1:
        t.color(gradient[dataValue].hex)


# set up tkinter
root = tkinter.Tk()
root.withdraw()

    
# get input
print("Welcome to")
print()
print("IIIIIIII    III           IIII     III                III")     
print("II    II    III           IIII      III              III")
print("II    II    III           IIII       III            III")
print("IIIIIIII    III           IIII        III          III")
print("IIII        III           IIII         III        III")
print("II II       III           IIII          III      III")
print("II  II      III           IIII           III    III")
print("II   II     IIIIIIIII     IIII            III  III")
print("II    II    IIIIIIIII     IIII             IIIIII")
print()
print(" Radial       Light     Intensity          Viewer")
print()
print("Made by Viktor from FLL team Legolas")
print("robot.vdp.sk")
print()
print("send your bug reports, questions and requests to: viktor.zavodsky@gmail.com")
print()
print()
print("Let's start!")
print()
while True:
    print("Choose mode:")
    mode = input("black-white / color / circles: ")
    if mode == "black" or mode == "black-white" or mode == "b" or mode == "bw":
        print("Mode: black-white")
        print()
        mode = 0
        break
    elif mode == "color" or mode == "col" or mode == "c":
        print("Mode: color")
        print()
        mode = 1
        # color gradient selection
        print("Now, you have to select 2 colors to form a gradient.")
        sleep(1)
        print()
        print("Select first color for the gradient:")
        gradientCol1 = askcolor()
        print("Select second color for the gradient:")
        gradientCol2 = askcolor()
        print("Colors selected: ",gradientCol1[1]," and ",gradientCol2[1])
        break
    elif mode == "circles":
        mode = 2
        print("Sorry, this mode is not finished. Please select another mode.")
        print()#break then
    else:
        print("Wrong input. Try again.")
        print()


print()
while True:
    penThickness = input("Select thickness of line: ")
    try:
        penThickness = int(penThickness)
        break
    except:
        print("Wrong value. Try again.")
        print()


print()
print("Select background color:")
root.withdraw()
bgColor = askcolor()

print()
print("Now, please select a file.")
sleep(1)

# ask for file & test it
while True:
    try:
        # open file dialog window
        fileName = filedialog.askopenfilename()
        # try to open the file
        file = open(fileName)
        # get number of lines & close the file
        lineCount = len(file.readlines())
        file.close()
        break
    except:
        print()
        print("Something went wrong. Please try again.")
        continue

print()
print("File ", fileName, " loaded successfully.")


# create color gradient (101 colors for each value from 0 to 101)
if mode == 1:
    gradient = list(color(gradientCol1[1]).range_to(color(gradientCol2[1]), 101))


# prepare turtle
t = turtle.Turtle()
t.hideturtle()
turtle.bgcolor(bgColor[1])
turtle.colormode(255)
turtle.tracer(0,0)
t.speed(0)
t.penup()
t.goto(0,0)
t.pensize(penThickness)

t.forward(300)
t.left(90)
t.pendown()

print()
print("Drawing...")

# open the file again
file = open(fileName)

# decide draw settings
steps = 360 / lineCount

# read & draw
for i in range(lineCount):
    try:
        line = file.readline()
        dataValue = int(line)
        setColor(mode, dataValue)
        t.forward(5 * steps)
        t.left(1 * steps)
    except Exception as error:
        print("Error: ", error,"i = ",i," dataValue = ",dataValue)
        
# show the picture
turtle.update()

print()
print("Picture completed.")

# close after 5 secs
print()
end = input("Do you want to leave?     Press enter to leave...")
raise SystemExit
