import pygame
from pygame.locals import *
from sys import exit

def standardInput(event, screen):
    if event.type == QUIT:
        quitGame()
    if event.type == pygame.KEYDOWN:
        if event.key == K_q:
            quitGame()

def quitGame():
    pygame.quit()
    exit()

def chooseLevel(level_buttons, mouse_pos):
    for item in level_buttons:
        level = item[0]
        rect = item[1]
        if rect.collidepoint(mouse_pos):
            return level
    
def inputEvent(event, word):

    # Sets the maximum length of input
    maxLength = 30

    if event.type == pygame.KEYDOWN:

        # Set the letter of input and append it to the userInput list
        # if the list is not over the maximum length
        if event.key >= pygame.K_a and event.key <= pygame.K_z:
            if event.mod & pygame.KMOD_SHIFT:
                letter = chr(ord('A') + event.key - pygame.K_a)
            else:
                letter = chr(ord('a') + event.key - pygame.K_a)

                
        # Set the letter as a number and append it to the userInput
        # list if the list does not exceed max length
        elif event.key >= pygame.K_0 and event.key <= pygame.K_9:
            
            if event.mod & pygame.KMOD_SHIFT:
                if event.key == K_1:
                    letter = '!'
                if event.key == K_2:
                    letter = '@'
                if event.key == K_3:
                    letter = '#'
                if event.key == K_4:
                    letter = '$'
                if event.key == K_5:
                    letter = '%'
                if event.key == K_6:
                    letter = '^'
                if event.key == K_7:
                    letter = '&'
                if event.key == K_8:
                    letter = '*'
                if event.key == K_9:
                    letter = '('
                if event.key == K_0:
                    letter = ')'
            else:
                letter = chr(ord('0') + event.key - pygame.K_0)
        
        elif event.key == K_PERIOD:
            letter = '.'
        elif event.key == K_COMMA:
            letter = ','
        else:
            letter = None

        if letter and len(word) < maxLength:
            word = word + letter


        # Add a space to the userInput list if list does not
        # exceed maxLength
        elif event.key == pygame.K_SPACE:
            if len(word) < maxLength:
                word = word + ' '

        # Pop off the last element in the list if the user
        # hits backspace (and the list is not empty)
        elif event.key == pygame.K_BACKSPACE:
            if len(word) > 0:
                word = word[0:-1]
                
    return word



def drawText(screen, word, font, blink, start_loc):
    color = (0,0,0)
    text = ''.join(word)
    text_surface = font.render(text,True,color)
    screen.blit(text_surface, start_loc)
    if 0 <= blink % 20 <= 8:
        line_x = start_loc[0] + text_surface.get_width()
        pygame.draw.line(screen, color,
                         (line_x, start_loc[1]),
                         (line_x, start_loc[1]+text_surface.get_height()))
