import os
import easygui
import math
import pygame

pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((500, 500))
# win2 = Window("2nd window", size=(256, 256), always_on_top=True)
screen.fill((200, 200, 200))
font = pygame.font.SysFont("arial bold", 20)

end = False
while not end:
    filePath = easygui.fileopenbox(title='Open a file for drawing...',
                                   filetypes=[["*.rtf", "Súbor z robota"]],
                                   multiple=False)
    print(f"filePath = {filePath}")
    if filePath == None:
        # This is, when you close window
        # print(f"filePath == None")

        # print("Please select a file")
        # continue
        quit()
    try:
        full_file = eval(open(filePath, "r", encoding="utf8").read())   # This is the full file
        print(f"full_file type: {str(type(full_file))}")
        if str(type(full_file)) == "<class 'dict'>":
            file = full_file
            if_height = False
        elif str(type(full_file)) == "<class 'list'>":
            file = full_file[0]   # These are only the points
            if_height = True
        end = True
    except Exception as e:
        print(f"Error {e} occured")
        os.system("cls")
        print("Bad file(s). Try again.")

backslash = "\\"
pygame.display.set_caption(f"Scanned shape from file {filePath.split(backslash)[-1]}")

try:
    print(f"\nfile: ")
    data = {}
    for k in file.keys():
        print(f"\t{k}:")
        # if str(abs(k)) == "8280":
        #     kk = 23
        # else:
        #     kk = k
        if abs(k) > 359:
            kk = k // 360
        else:
            kk = k
        data[abs(kk)] = {}
        for k2 in file[k].keys():
            print(f"\t\t{file[k][k2]}")
            if abs(k2) not in data[abs(kk)].keys():
                data[abs(kk)][abs(k2)] = list(set(file[k][k2]))
            else:
                data[abs(kk)][abs(k2)] = list(set(data[abs(kk)][abs(k2)] + file[k][k2]))
    print(f"type of file = {type(file)}")
except Exception as e:
    print(f"Error {e} occured")

print("\n\ndata = {")
for k in data.keys():
    print(f"\t{k}: ", end="")
    print("{")
    for k2 in data[k].keys():
        print(f"\t\t{k2}: {data[k][k2]}")
    print("\t}, ")
print("}\n")

pi = math.pi
x1 = 250
y1 = 250

dist_from_center = 90
radius = (max(list(data[list(data.keys())[-1]].keys())) + dist_from_center) // 3
# print(f"radius = {radius}")
print(f"round(pi * 200) = {round((pi * 200))}")

if if_height:
    text = font.render(f"height: {full_file[1]['height']} cm", True, (0, 0, 0))
    screen.blit(text, text.get_rect(center=(55, 15)))

# One third of the distance
lines1 = []
dots1 = []
for i in range(0, round(pi * 200), round(pi * 200 / (len(list(data.keys()))-1))):
    x2 = x1 + math.cos((i + round(pi * 100)) / 100) * radius
    y2 = y1 + math.sin((i + round(pi * 100)) / 100) * radius

    if i != 0:
        lines1.append(pygame.draw.line(screen, (0, 0, 0), (x1, y1), (x2, y2)))
    else:
        lines1.append(pygame.draw.line(screen, (255, 0, 0), (x1, y1), (x2, y2)))

    pygame.display.flip()

    angle = i // round(pi * 200 / len(list(data.keys())))
    try:
        for r in data[angle].keys():
            radius2 = r // 3
            for a in data[angle][r]:
                one_deg = round(pi * 200) / 360
                j = one_deg * a
                x3 = x2 + math.cos((j + i) / 100) * radius2
                y3 = y2 + math.sin((j + i) / 100) * radius2
                dots1.append(pygame.draw.rect(screen, (0, 0, 255), pygame.rect.Rect((x3-1, y3-1), (2, 2))))
    except:
        pass

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
