# This demo was written by Max Sargent and Peter Scarfe

# Import the various libraries that we will be using
import pyglet
from psychopy import visual, event, core
import numpy as np

# Define our colours
black = 0
white = 1
red = [white, black, black]

# Get a list of the screens avaliable
display = pyglet.canvas.get_display()
screens = display.get_screens()

# Get the length of the list of screens
numScreens = len(screens)

# We want to present on the secondary monitor (assuming two monitors connected). Note screens are counted from 0.
screenNum = numScreens - 1;

# Get the size of the screen in pixels. We do this here as PsychoPy has substaintial bugs related to macos displays
# If we poll the screen size of mywin once created it will be incorrect on macos.
# This will result in our positioning in terms of pixels being incorrect.
myScreen = screens[screenNum]
widthPix = myScreen.width
heightPix = myScreen.height

# Create a full screen window on our secondary monitor. We use pyglet as our screen backend. Note the rbg1 colour profile.
# We color our screen black and will use pixel units.
mywin = visual.Window(fullscr = True, units = 'pix', color = [black, black, black], winType = 'pyglet', colorSpace = 'rgb1', screen = screenNum)

# Screen coordinates in PsychoPy are +/- half the relevent dimension, as [0, 0] is defined as the centre of the screen.
# Here we randomly place a dot on the screen
dotx = np.random.rand() * widthPix - widthPix / 2
doty = np.random.rand() * heightPix - heightPix / 2

# Define the dot: we set the color space for the dot and color the face and edge of the dot red. 
# PsychoPy appears not to caary the colorspace of the window to the objects
dotRadiusPix = 5
dot = visual.Circle(
    win = mywin,
    radius = dotRadiusPix,
    color = red,
    colorSpace = 'rgb1',
    units = 'pix',
    pos = [dotx, doty]
)

# Draw the dot
dot.draw()

# Flip to the screen
mywin.flip()

# Wait for key press before continuing. When pressed close the screen and exit PsychoPy.
event.waitKeys()
mywin.close()
core.quit()