import turtle
import easygui
import os
import sys
smery = []
cm = []
suradnice = [[0, 0]]
rozmery = [0, 0]
screenSize = [0, 0]

while True:
    filePath = easygui.fileopenbox(title='Open a file for drawing...', filetypes=[["*.rtf", "Súbor z robota"]], multiple=False)
    if filePath == None:
        sys.exit(0)
    try:
        file = open(filePath, "r")
        break
    except:
        os.system("cls")
        print("Bad file. Try again.")
        
i = "n"
while not i in ["y", "yes", "Y"]:
    try:
        screenSize[0] = int(input("Set the width of the image: "))
        screenSize[1] = int(input("Set the height of the image: "))
    except:
        os.system("cls")
        print("Write ONLY whole numbers without text!")
        continue
    os.system("cls")
    while True:
        print("Size of the image will be " + str(screenSize[0]) + "x" + str(screenSize[1]))
        i = input("Confirm y/n: ")
        if not i in ["y", "yes", "Y", "n", "N", "no"]:
            os.system("cls")
            print("Bad input. Try again")
        else:
            os.system("cls")
            break

print("Reading file...")
try:
    lines = file.readlines()
    for x in range(0, len(lines), 2):
        smery.append(int(float(lines[x].strip("\n"))))
        cm.append(float(lines[x + 1].strip("\n")))
except:
    print("Unexcepted error! Mabye a wrong file? Please try again.")
    os.system("pause")
    exit()

print("Calculating size...")
curX = 0
curY = 0
xy = []
for a in range(0, len(cm)):
    if smery[a] == 1:
        curX = curX - cm[a]
    elif smery[a] == 2:
        curY = curY + cm[a]
    elif smery[a] == 3:
        curX = curX + cm[a]
    elif smery[a] == 4:
        curY = curY - cm[a]
    xy = [0, 0]
    xy[0] = curX
    xy[1] = curY
    suradnice.append(xy)

for xy in suradnice:
    x = xy[0]
    if x < 0:
        for z in range(0, len(suradnice)):
            suradnice[z][0] = suradnice[z][0] + abs(x)

for xy in suradnice:
    y = xy[1]
    if y < 0:
        for z in range(0, len(suradnice)):
            suradnice[z][1] = suradnice[z][1] + abs(y)

for xy in suradnice:
    x = xy[0]
    y = xy[1]
    if x > rozmery[0]:
        rozmery[0] = x
        
    if y > rozmery[1]:
        rozmery[1] = y

suradnice = [[0, 0]]
del curX
del curY
del xy

print("Calculating scale...")
m1 = rozmery[0] / screenSize[0]
m2 = rozmery[1] / screenSize[1]
if m1 > m2:
    scale = m1
else:
    scale = m2

print("Calculating X and Y...")
curX = 0
curY = 0
xy = []
for a in range(0, len(cm)):
    if smery[a] == 1:
        curX = curX - int(cm[a] / scale)
    elif smery[a] == 2:
        curY = curY + int(cm[a] / scale)
    elif smery[a] == 3:
        curX = curX + int(cm[a] / scale)
    elif smery[a] == 4:
        curY = curY - int(cm[a] / scale)
    xy = [0, 0]
    xy[0] = curX
    xy[1] = curY
    suradnice.append(xy)

print("Fixing the ends...")
fixX = 0
fixY = 0
if suradnice[0][0] < suradnice[-1][0]:
    fixX = suradnice[0][0] - suradnice[-1][0]

if suradnice[0][1] > suradnice[-1][1]:
    fixY = suradnice[-1][1] - suradnice[0][1]
    
suradnice[-1][0] = suradnice[0][0]
suradnice[0][1] = suradnice[-1][1]



print("Shifting...")
for xy in suradnice:
    x = xy[0]
    if x < 0:
        for z in range(0, len(suradnice)):
            suradnice[z][0] = suradnice[z][0] + abs(x)

for xy in suradnice:
    y = xy[1]
    if y < 0:
        for z in range(0, len(suradnice)):
            suradnice[z][1] = suradnice[z][1] + abs(y)

print("Centering...")
shiftX = int((screenSize[0] - fixX - (rozmery[0] / scale)) / 2)
shiftY = int((screenSize[1] + fixY - (rozmery[1] / scale)) / 2)
for a in range(0, len(suradnice)):
    xy = suradnice[a]
    xy[0] = xy[0] + shiftX
    xy[1] = xy[1] + shiftY
    suradnice[a] = xy

print("Drawing...")
t = turtle.Turtle()
t.hideturtle()
t.getscreen().screensize(screenSize[0], screenSize[1])
t.pensize(7)
t.speed(0)
t.penup()
t.goto(suradnice[0][0] - (screenSize[0]/2), suradnice[0][1] - (screenSize[1]/2))
t.pendown()
t.color("#004000", "#EFE4B0")
t.begin_fill()
for x in range(1, len(suradnice)):
    t.goto(suradnice[x][0] - (screenSize[0]/2), suradnice[x][1] - (screenSize[1]/2))
t.end_fill()
turtle.done()
