# this implements a very basic visual oddball task using PsychoPy
# see https://en.wikipedia.org/wiki/Oddball_paradigm for more details

from psychopy import visual, core
from utilities import wait_for_space
import random
import pylsl

# Prepare the LSL stream info
info = pylsl.StreamInfo(
    name='Stimulus computer',
    type='Markers',
    channel_count=1,
    nominal_srate=0, # should be zero for markers
    channel_format='string',
    source_id=None,
)

# Create a new outlet
outlet = pylsl.StreamOutlet(info)

# Print the stream info
print("Now publishing stream:", info.name(), info.type(), info.channel_count(), "channels at", info.nominal_srate(), "Hz")

screen = visual.Window([800, 600], color='black', units='pix')
fixation = visual.TextStim(screen, text='+', color='white', height=50)
standard = visual.TextStim(screen, text='STANDARD', color='green', height=50)
deviant  = visual.TextStim(screen, text='DEVIANT', color='red', height=100)

wait_for_space(screen, 'Press SPACE to start')
fixation.draw()
screen.flip()
core.wait(2)

# present 10 trials of the oddball task
# where 80% of the stimuli are standard and 20% are deviant
for trial in range(150):
    # Randomly choose a stimulus for each trial
    condition = random.choice(['s', 's', 's', 's', 'd'])  # 80% standard, 20% deviant

    if condition == 's':
        print('standard')
        stimulus = standard
    else:
        print('deviant')
        stimulus = deviant

    stimulus.draw()  # present the randomly chosen stimulus
    screen.flip()
    outlet.push_sample([condition])  # send marker to LSL
    core.wait(0.2)

    fixation.draw()
    screen.flip()
    jitter = random.uniform(0, 0.3)
    core.wait(0.85 + jitter)

wait_for_space(screen, 'Done, press SPACE to finish')

screen.close()
core.quit()
