def send_to_bob(message):
    clearUI()
    a=(100, 200) 
    b=(650, 200)
    path = get_path(a,b)
    print("path: ", path)

    createTextLeft(0, display_height*4.5/6 + 10, "Message: '" + " ".join(message) + "'             (=" + "".join([chr(int(x,2)) for x in message]) + ")", 20)

    createTextLeft(0, display_height*5/6, "Sending: ", 18)

    createTextLeft(0, display_height*5.5/6, "Status: ", 18) 

    for j, letter in enumerate(message):
        #col = random.choice([green, purple, yellow, blue, ])
        col = [random.random() * 255, random.random() * 150, random.random() * 255]
        while sum([abs(background[k] - col[k]) for k in range(3)]) < 30: 
            col = [random.random() * 255, random.random() * 150, random.random() * 255]

        createTextLeft(80 + j * 80, display_height*5/6, letter, 16, colour=col)
        #createTextLeft(55 + j * 50, display_height*5.5/6, "success", 16, colour=green)
        for i in range(len(path)-1):
            a = path[i][0] + 50, path[i][1] + 20
            b = path[i+1][0], path[i][1] + 20
            loss = LOSS * get_dist(a, b)
            red = int(loss * len(letter) + random.random())
            draw_lines(red, len(letter)-red, a, b, block_size=10, green_col=col)

            pygame.time.wait(500)
        createTextLeft(80 + j * 80, display_height*5.5/6, "success", 16, colour=green)
    pygame.display.update()
    pygame.time.wait(1000)
    pop_up.popUpWindow("Success", "You successfully sent the message '" + "".join([chr(int(x,2)) for x in message]) + "' to Bob")
    
    sent_messages[("a","b")] = message
        #print("here", j==len(message) -1 and 1/0)

    main_loop()
def show_help():
    pop_up.popUpWindow('Help?', 'In this level you must first create a secure connection between yourself and Bob.\n Since the distance is too great for a qubit to be sent without loss, you must set up a chain of repeaters which take in a qubit, and output it with greater signal.\n\nOnce a repeater has been placed, the bb84 protocol which you saw in the introduction is run, with green lines representing the successfully transmitted qubits and the red lines representing the lost ones. \nRemember that the further apart your repeaters are, the greater the loss.\n\nOnce you have a complete chain of repeaters, you will be able to send messages completely securely across any number of repeaters, only needing faith in the laws of quantum mechanics to trust its security.')
def create_key(a, b):
    t = 0
    run = True
    q = None

    added_a = False
    added_b = False
    while True:
        global n
        # print("n=", n)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                quitgame()

        clearUI()
        smallText = pygame.font.Font("freesansbold.ttf", 16)
        textSurf, textRect = text_objects("How many qubits do you want to send to the repeater to create the key?",
                                          smallText)
        textRect.center = (display_width * 1 / 2, display_height * 4.5 / 6 + 25)
        gameDisplay.blit(textSurf, textRect)

        textinput.update(events)
        # gameDisplay.blit(textinput.get_surface(), (display_width*1/2, display_width * 10/12))
        gameDisplay.blit(textinput.get_surface(), (textRect.left, 500))

        add_button = button("OK", textRect.left + 50, 500, 50, 50, green, dark_green, action=update_n)

        try:
            n = int(n)

        except:
            if len(n) > 0:
                textSurf, textRect = text_objects("Invalid input", smallText, colour=red)
                textRect.center = (textRect.left + 50, display_height * 5.5 / 6)
                gameDisplay.blit(textSurf, textRect)

        if run and type(n) == int and n > 0:
            q = qkd.bb84(n, p=LOSS)
            dist = get_dist(a, b)
            q.run_protocol(dist)
            draw_lines(q.n_sent - q.n_received, q.n_received, (a[0] + 50, a[1]), b)

            draw_grid()
            t = time.time()


        if run and q and (not q.get_key() or q.get_key() == "error" or q.get_key() in connections):
            textSurf, textRect = text_objects("An empty or identical key has been generated, try sending more qubits",
                                              smallText, colour=red)
            textRect.topleft = (textRect.left + 150, display_height * 5.75 / 6)
            gameDisplay.blit(textSurf, textRect)
            update_n(val="")


        elif q:
            run = False
            if not added_a:
                createTextCenter(a[0], a[1] + 100, "My bases were: " + " ".join(q.alice_bases[:5]) + "..." * int(len(q.alice_bases) > 5), 16)
                added_a = True

            if 0.9 < time.time() - t < 1:
                b_filt = [z for z in q.bob_bases if z != -1]
                if not added_b:
                    createTextCenter(b[0], b[1] - 50, "My bases were: " + " ".join(b_filt[:5]) + "..." * int(len(b_filt) > 5), 16)
                    added_b = True

            if 3.9 < time.time() - t < 4:

                clearUI()

                print("great success")

                repeaters[b] = green
                connections[q.get_key()] = (a, b)
                if complete_path():
                    draw_grid()
                    pygame.display.update()

                    pop_up.popUpWindow("Path complete", "Congratulations! You have created a completely secure path between you and Bob, you may now send and encrypt messages to each other")
                n = ""
                main_loop()

        pygame.display.update()
        clock.tick(15)
def show_future():
    pop_up.popUpWindow("future levels", "In the future, as qubit loss decreases and error correction improves, more complicated networks may be developed. \nOnce qubits can sent deterministically, people may securely send large amounts of data to be computed elsewhere and returned to be measured.\n Eventually a quantum internet may be built which promises to revolutionize the spread of information on a scale akin to the development of the classical internet.")
import pygame
import pygame_textinput
import time
import random
import qkd
import numpy as np
import math
import binascii

import pop_up
pop_up.popUpWindow("Warning!", "If this isn't here everything crashes")

#import TextBox
#Colors
background = (173,216,230)
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,200)
dark_green = (0,255,0)
dark_red = (255,0,0)
grey = (220,220,220)
purple = (128,0,128)
light_red = (255, 102, 102)
yellow = (255,255,0)

#QC initials
nt = 0
qlist = []
aliceBases = []