"""Functions for establishing a test stream that sends out continuous artificial stimulus data"""
import pylsl
import time
import random
import threading
import uuid


def repopulate_list(template_list):
    """Creates a shuffled deep copy of a template list."""
    new_list = []
    for item in template_list:
        new_list.append(item)
    random.shuffle(new_list)
    epoch_id = uuid.uuid4()
    return new_list, epoch_id


def marker_publish(signal, outlet, events, log=False):
    """Sends randomly generated markers (row/column number) through the lsl stream outlet."""
    trial_num = len(events)
    generator, epoch_id = repopulate_list(events)
    count = 0
    start_time = pylsl.local_clock()
    if log:
        print('Markers sending ...\n\n\n\n\n')
    while not signal.is_set():
        if generator:
            # generate random marker data
            # status = random.randint(0, 1)
            tmp = generator.pop()
            # chooses the target in the identifiers list
            if tmp == 1:
                event = 1
                if log:
                    print("""
                                       AAA               
                                      A:::A              
                                     A:::::A             
                                    A:::::::A            
                                   A:::::::::A           
                                  A:::::A:::::A          
                                 A:::::A A:::::A         
                                A:::::A   A:::::A        
                               A:::::A     A:::::A       
                              A:::::AAAAAAAAA:::::A      
                             A:::::::::::::::::::::A     
                            A:::::AAAAAAAAAAAAA:::::A    
                           A:::::A             A:::::A   
                          A:::::A               A:::::A  
                         A:::::A                 A:::::A 
                        AAAAAAA                   AAAAAAA
                    """)
                target = 1
            else:
                event = 0
                if log:
                    print("""
                        BBBBBBBBBBBBBBBBB   
                        B::::::::::::::::B  
                        B::::::BBBBBB:::::B 
                        BB:::::B     B:::::B
                          B::::B     B:::::B
                          B::::B     B:::::B
                          B::::BBBBBB:::::B 
                          B:::::::::::::BB  
                          B::::BBBBBB:::::B 
                          B::::B     B:::::B
                          B::::B     B:::::B
                          B::::B     B:::::B
                        BB:::::BBBBBB::::::B
                        B:::::::::::::::::B 
                        B::::::::::::::::B  
                        BBBBBBBBBBBBBBBBB   
                    """)
                target = 0

            # increment marker count
            count = count + 1

            t = pylsl.local_clock()
            package = [
                str(t),
                str(event),
                str(target),
                str(trial_num),
                str(epoch_id)
            ]

            # data pushed in form [[identity, target, trial_num], timestamp]
            outlet.push_sample(package)
            # print(' {}' .format(data))
            # print('\n{}\n'.format(letters[tmp])) # print for command line training

            # down time in between sending markers
            time.sleep(0.4)
            # if count % trial_num == 0:
            #     # after trial_num flashes, 1 set of trials complete
            #     end_time = pylsl.local_clock()
            #     if log:
            #         print('\n\n\n\n\ntrial was {} seconds long\n\n\n\n\n'.format(end_time - start_time))
        else:
            generator, epoch_id = repopulate_list(events)
            count = 0
            time.sleep(4)
            start_time = pylsl.local_clock()
    if log:
        print('Markers no longer sending.')


def test_marker_stream():
    """Create and return marker lsl outlet."""
    # create
    info = pylsl.StreamInfo('Markers', 'Markers', 5, 0, 'string', 'mywid32')
    # next make an outlet
    outlet = pylsl.StreamOutlet(info)
    return outlet


def start_marker_stream(outlet, events, log=True):
    """Starts publishing marker data on the lsl stream in a new thread."""
    kill_signal = threading.Event()
    marker_thread = threading.Thread(target=marker_publish, name='marker-generator', args=(kill_signal, outlet, events, log))
    marker_thread.daemon = True
    marker_thread.start()


if __name__ == '__main__':
    test_outlet = test_marker_stream()
    test_events = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    start_marker_stream(test_outlet, test_events, log=True)

    while True:
        time.sleep(5)


@@ -1119,357 +1119,142 @@
     
-                    AAA               %0A                                      A:::A              %0A                                     A:::::A             %0A                                    A:::::::A            %0A                                   A:::::::::A           %0A                                  A:::::A:::::A          %0A                    
+AAA%0A                  A:::A%0A                 A:::::A%0A                A:::::::A%0A               A:::::::::A%0A              A:::::A:::::A%0A
     
@@ -1281,27 +1281,9 @@
 :::A
-         %0A         
+%0A
     
@@ -1286,35 +1286,24 @@
             
-           
 A:::::A   A:
@@ -1307,24 +1307,16 @@
  A:::::A
-        
 %0A       
@@ -1323,75 +1323,28 @@
     
-                    A:::::A     A:::::A       %0A                    
+A:::::A     A:::::A%0A
     
@@ -1374,35 +1374,9 @@
 :::A
-      
 %0A
-                    
     
@@ -1407,34 +1407,9 @@
 :::A
-     
 %0A
-                    
     
@@ -1441,33 +1441,9 @@
 :::A
-    %0A                    
+%0A
     
@@ -1476,32 +1476,9 @@
 :::A
-   
 %0A
-                    
     
@@ -1508,39 +1508,17 @@
  A:::::A
-  
 %0A
-                    
      A::
@@ -1545,38 +1545,17 @@
  A:::::A
- 
 %0A
-                    
     AAAA
@@ -1738,727 +1738,23 @@
 %22%22%22%0A
-                        BBBBBBBBBBBBBBBBB   %0A                        B::::::::::::::::B  %0A                        B::::::BBBBBB:::::B %0A                        BB:::::B     B:::::B%0A                          B::::B     B:::::B%0A                          B::::B     B:::::B%0A                          B::::BBBBBB:::::B %0A                          B:::::::::::::BB  %0A                          B::::BBBBBB:::::B %0A                          B::::B     B:::::B%0A                          B::::B     B:::::B%0A                          B::::B     B:::::B%0A                        BB:::::BBBBBB::::::B%0A                        B:::::::::::::::::B %0A                        B::::::::::::::::B  %0A                        BBBBBBBBBBBBBBBBB   
+%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A
 %0A   
@@ -2822,17 +2822,17 @@
 e.sleep(
-4
+2
 )%0A      
@@ -3743,9 +3743,8 @@
 leep(5)%0A
-%0A

Replace B with whitespace