import display, keypad, sndmixer, virtualtimers as vt, random, machine
from time import time

# Hopefully change the PRNG state a bit
random.seed( int( 1000000 * time() ) % 1000000)

volume = machine.nvs_getint('system', 'volume') or 255

def draw(state):
	sound(state)
	a = 0x00FF00 if state == 1 else 0x002000
	b = 0xFF0000 if state == 2 else 0x200000
	d = 0xFFFF00 if state == 3 else 0x202000
	c = 0x0000FF if state == 4 else 0x000020
	if state == 5:
		a = 0xFF0000
		b = 0xFF0000
		c = 0xFF0000
		d = 0xFF0000
	if state == 6:
		a = 0x00FF00
		b = 0x00FF00
		c = 0x00FF00
		d = 0x00FF00
	display.drawPixel(0, 0, a)
	display.drawPixel(1, 0, a)
	display.drawPixel(0, 1, a)
	display.drawPixel(1, 1, a)
	display.drawPixel(2, 0, b)
	display.drawPixel(3, 0, b)
	display.drawPixel(2, 1, b)
	display.drawPixel(3, 1, b)
	display.drawPixel(2, 2, c)
	display.drawPixel(3, 2, c)
	display.drawPixel(2, 3, c)
	display.drawPixel(3, 3, c)
	display.drawPixel(0, 2, d)
	display.drawPixel(1, 2, d)
	display.drawPixel(0, 3, d)
	display.drawPixel(1, 3, d)
	display.flush()

def sound(state):
	global synth
	f = 0
	if state == 1:
		f = 600
	if state == 2:
		f = 800
	if state == 3:
		f = 1000
	if state == 4:
		f = 1200
	if state == 5:
		f = 400
	if state == 6:
		f = 1400
	sndmixer.freq(synth, f)

def on_key(key_index, pressed):
	global userInput, userTimeout, resultTimeout, showTimeout, showInput, resultPositive, userPressed
	x, y = key_index % 4, int(key_index / 4)
	if pressed:
		if resultTimeout <= 0:
			userTimeout = 30
			if (x == 0 or x == 1) and (y == 0 or y == 1):
				draw(1)
				userInput.append(1)
			elif(x == 2 or x == 3) and (y == 0 or y == 1):
				draw(2)
				userInput.append(2)
			elif(x == 0 or x == 1) and (y == 2 or y == 3):
				draw(3)
				userInput.append(3)
			elif(x == 2 or x == 3) and (y == 2 or y == 3):
				draw(4)
				userInput.append(4)
		userPressed = True
		gotFail = False
		for i in range(len(userInput)):
			if showInput[i] != userInput[i]:
				userInput = []
				showInput = [random.randint(1,4)]
				userTimeout = -1
				showTimeout = 5
				resultTimeout = 5
				resultPositive = False
				gotFail = True
				break
		if (not gotFail) and (len(userInput) >= len(showInput)):
				userInput = []
				userTimeout = -1
				showTimeout = 5
				showInput.append(random.randint(1,4))
				resultTimeout = 5
				resultPositive = True
	else:
		if resultTimeout <= 0:
			draw(0)
		userPressed = False

def gameTask():
	global userTimeout, userInput, showTimeout, showStep, showInput, resultTimeout, userPressed, resultPositive
	if userPressed:
		return 50
	if resultTimeout > 0:
		resultTimeout -= 1
		print("Result wait")
		if resultTimeout % 2:
			if resultPositive:
				draw(6)
			else:
				draw(5)
		else:
			draw(0)
	else:
		if resultTimeout == 0:
			resultTimeout -= 1
			draw(0)
		if userTimeout < 0:
			userTimeout = -1
			userInput = []
			if showTimeout < 0:
				showTimeout = 5
				if (showStep < len(showInput)):
					draw(showInput[showStep])
					showStep += 1
					print("Show step")
				else:
					showStep = 0
					showTimeout = 30
					print("Show end")
			else:
				print("Show wait", showTimeout)
				draw(0)
				showTimeout -= 1
		else:
			userTimeout -= 1
			showTimeout = 0
			showStep = 0
			print("User wait")
	return 50

sndmixer.begin(1, True)
synth = sndmixer.synth()
sndmixer.volume(synth, volume)
sndmixer.play(synth)
userInput = []
showInput = [random.randint(1,4)]
userTimeout = 0
showTimeout = 50
showStep = 0
showActive = False
userPressed = False
resultPositive = False
resultTimeout = 0
vt.begin()
vt.new(50, gameTask)
draw(0)
keypad.add_handler(on_key)