#! /usr/bin/env python

import os, sys, datetime, time
import pygame
from pygame.locals import *

def load_image(name, colorkey=None):
    fullname = os.path.join('data', 'images')
    fullname = os.path.join(fullname, name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

def load_sound(name):
	class NoneSound:
		def play(self): pass
	if not pygame.mixer:
		return NoneSound()
	fullname = os.path.join('data','sounds')
	fullname = os.path.join(fullname, name)
	try:
		sound = pygame.mixer.Sound(fullname)
	except pygame.error, message:
		print 'Cannot load sound:', fullname
		raise SystemExit, message
	return sound

def find_files(directory):
	file_list = []
	for root, dirs, files in os.walk(os.path.join(directory)):
		for file in files:
			if file.find("thumbs.db") == -1 and file.find("Thumbs.db") == -1:
				file_list.append(file)
	return file_list

def display_static_image(image_name,seconds_pause,screen,background,area):
	start_time = datetime.datetime.today()
        end_time = start_time + datetime.timedelta(0,seconds_pause)

	while datetime.datetime.today() < end_time:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
		
			# Draw everything to the screen
			screen.blit(background, (0, 0))

			image, imagerect = load_image(image_name)
			imagerect.centerx = area.centerx
			imagerect.centery = area.centery
			screen.blit(image,imagerect)

			pygame.display.flip()

def get_sprite_choice(screen, background, windowwidth, windowheight):
# get_sprite_choice displays a message to the user to pick which image they want
# to use during gameplay.  It displays all the choices available and returns
# the image that the user has chosen.

    class SpriteChoice(pygame.sprite.Sprite):
        def __init__(self, image_file, centerPoint):
            pygame.sprite.Sprite.__init__(self)
            self.image_file = "choices/" + image_file
            self.image,self.rect = load_image("choices/" + image_file,-1)
            self.rect.center = centerPoint

    # Set up the sprite choices
    # We are creating a group of all the choices the user has for their game piece
    # width and maxheight are used for centering all the pieces on the screen
    # Put all the possible images in the data\images\choices folder
    choices = pygame.sprite.RenderUpdates()
    width = 0
    maxheight = 0
    for image in find_files("data/images/choices"):
        sprite_choice = SpriteChoice(image, (0, 0))
        choices.add(sprite_choice)
        width = width + sprite_choice.rect.width
        
        # We need to determine the tallest image in order to center the pieces in the "y" direction
        if sprite_choice.rect.height > maxheight:
            maxheight = sprite_choice.rect.height

    # Blit everything to the screen
    # Choose your own font, wording, font color and font size
    screen.blit(background,(0,0))
    font = pygame.font.Font(None, 50)
    text = font.render("Choose your character!", 1, (255,255,255))
    textrect = text.get_rect()
    textrect.center = (windowwidth/2, 100)
    screen.blit(text, textrect)
        
    # Blit the choices to the screen.  If you want the images to line up higher or lower on
    # the screen, change (windowheight/2) to whatever start point you want
    startx, starty = (windowwidth - width)/2, (windowheight/2) + (maxheight/2)
    for choice in choices:
        choice.rect.left = startx
        choice.rect.centery = starty
        screen.blit(choice.image, choice.rect)
        startx = startx + choice.rect.width

    pygame.display.flip()

    # Wait to start the game until the user selects which game piece they want to use
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN: # if the user clicked the mouse
                for choice in choices:
                    if choice.rect.collidepoint(event.pos[0], event.pos[1]):
                        # The user has clicked the piece they want to use.
                        return choice.image_file
