import display
import random
import buttons
import mch22
import time
from machine import Pin
from neopixel import NeoPixel

font = "roboto_blackitalic24"

m_subst_sing = ["mankind", "misuse", "money", "memory", "merchandise", "mayhem"]
m_subst_plur = ["may", "must", "might", "misfits", "mondays", "meteors", "machines", "miracles"]

c_verb_sing = ["causes", "challenges", "creates", "cripples", "contains", "cheats", "crashes"]
c_verb_plur = ["cause", "challenge", "create", "cripple", "contain", "cheat", "crash"]

h_object = ["hazard", "hope", "heaven", "hashes", "help", "heap", "hexdumps", "hell", "hopelessness", "haircuts", "helpers", "hunger", "hyperspace", "himalaya", "hypotheses", "hurricanes", "hypocrisy", "happiness"]

c_adverb = ["casually", "constantly", "continuously", "carefully", "carelessly", "creatively", "correctly", "conveniently"]
h_verb_sing = ["helps", "hides", "hacks", "happens", "haunts", "hugs", "hypothesizes"]
h_verb_plur = ["help", "hide", "hack", "happen", "haunt", "hug", "hypothesize"]

m_word = ""
c_word = ""
h_word = ""

color = [0,0,0]

def reboot(pressed):
  if pressed:
    mch22.set_brightness(255)
    mch22.exit_python()

buttons.attach(buttons.BTN_A,reboot)

def writeCentered(text, y, font, color):
  width = display.getTextWidth(text, font)
  x = int(160 - width/2)
  display.drawText(x, y, text, color, font)

def genColor():
  global color
  segment = random.randint(0,5)
  if segment == 0: # red - yellow
    color[0] = 255
    color[1] = random.randint(0,255)
    color[2] = 0
  if segment == 1: # yellow - green
    color[0] = random.randint(0,255)
    color[1] = 255
    color[2] = 0
  if segment == 2: # green - cyan
    color[0] = 0
    color[1] = 255
    color[2] = random.randint(0,255)
  if segment == 3: # cyan - blue
    color[0] = 0
    color[1] = random.randint(0,255)
    color[2] = 255
  if segment == 4: # blue - purple
    color[0] = random.randint(0,255)
    color[1] = 0
    color[2] = 255
  if segment == 5: # purple - red
    color[0] = 255
    color[1] = 0
    color[2] = random.randint(0,255)

def genAcronym():
  global m_word, c_word, h_word
  m_idx = random.randint(0,len(m_subst_sing)+len(m_subst_plur)-1)
  plural = m_idx >= len(m_subst_sing)
  if plural:
    m_word = m_subst_plur[m_idx - len(m_subst_sing)]
  else:
    m_word = m_subst_sing[m_idx]
  verb_object = (random.randint(0,100) < 50)
  if verb_object:
    if plural:
      c_word = c_verb_plur[random.randint(0,len(c_verb_plur)-1)]
    else: 
      c_word = c_verb_sing[random.randint(0,len(c_verb_sing)-1)]
    h_word = h_object[random.randint(0,len(h_object)-1)]
  else:
    c_word = c_adverb[random.randint(0,len(c_adverb)-1)]
    if plural:
      h_word = h_verb_plur[random.randint(0,len(h_verb_plur)-1)]
    else: 
      h_word = h_verb_sing[random.randint(0,len(h_verb_sing)-1)]

def writeAcronym():
  global font
  global color
  display.drawFill(0x00000000)
  col = (color[0]<<16) | (color[1]<<8) | color[2]
  writeCentered(m_word, 60, font, col)
  writeCentered(c_word, 100, font, col)
  writeCentered(h_word, 140, font, col)
  display.flush()

def fadeIn():
  for i in range(0,255,5):
    brightness(i)
    time.sleep(0.02)

def fadeOut():
  for i in range(255,0,-5):
    brightness(i)
    time.sleep(0.02)

def brightness(brightness):
  global np
  global color
  r = int(color[0] * pow(brightness/255.0, 1.7))
  g = int(color[1] * pow(brightness/255.0, 1.7))
  b = int(color[2] * pow(brightness/255.0, 1.7))
  for i in range(0,5):
    np[i] = (r,g,b)
  np.write()
  mch22.set_brightness(brightness)

# send colors out to LEDs

Pin(19, Pin.OUT).on()
np = NeoPixel(Pin(5, Pin.OUT), 5)
while True:
  brightness(0)
  genAcronym()
  genColor()
  writeAcronym()
  fadeIn()
  time.sleep(3)
  fadeOut()