# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 9th of April, 2015

# gets every filename from starting path (I call them 'names')
def getFSNames(_path = "/Users"):
	for _ in os.listdir(_path):
		p = _path + "/" + _
		if os.path.isdir(p):
			getFSNames(p)
		else:
			main.NAMES.append(p)

# gets 'main.NAMES', 'main.LETTERS' and 'main.MAXLETTER'
def nameInit():
	main.NAMES = []
	main.LETTERS = {"/":0}
	main.MAXLETTER = 0
	
	# get names
	getFSNames()
	
	# count letters
	for _ in main.NAMES:
		for __ in _:
			if __ in main.LETTERS:
				main.LETTERS[__] += 1
			else:
				main.LETTERS[__] = 1
	
	# get the maximum letter count (used for size and color calculation)
	for _ in main.LETTERS:
		if main.LETTERS[_] > main.MAXLETTER:
			main.MAXLETTER = main.LETTERS[_]
	
	# debug
	#print main.LETTERS
	#print main.MAXLETTER

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# regenerates image
def regenImg():
	main.SURF.fill([0, 0, 0])
	for _ in main.LETTERS:
		font = pygame.font.Font(None, int(200. / main.MAXLETTER * main.LETTERS[_]) + 20)
		text = font.render(_, 1, [
			int(255. / main.MAXLETTER * main.LETTERS[_]), 0, 0
		])
		rect = text.get_rect()
		pos = [random.randint(0, main.WIDTH - rect.right), random.randint(0, main.HEIGHT - rect.bottom)]
		main.SURF.blit(text, pos)

# render function
def render():
	main.SCREEN.fill([0, 0, 0])
	
	if main.DOGETFS:
		if main.TICKSTOGETFS > 10:
			main.SURF.fill([colorValid(main.TICKSTOGETFS), 0, 0])
		else:
			main.SURF.fill([0, 0, 0])
	
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	pygame.display.flip()

# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				sys.exit()
		
		# key
		if event.type == pygame.KEYDOWN:
			if event.key == keys.regen:
				main.DOGETFS = True
				main.TICKSTOGETFS = 200
	
	if main.DOGETFS and main.TICKSTOGETFS <= 0:
		# generate both
		nameInit()
		regenImg()
		main.DOGETFS = False
	
	if main.TICKSTOGETFS > 0:
		main.TICKSTOGETFS -= 1

# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			#print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# dummy class for global variables
class dummy():
	pass

# importing needed modules
import pygame, sys, time, math, os, random
pygame.font.init()

# main dummy
main = dummy()
main.TICKS = 0

main.WIDTH, main.HEIGHT = 1080, 720
main.SCALE = 1
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)] 
main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("FS Letter")
main.SURF = pygame.Surface(main.SIZE)

main.TICKSTOGETFS = 0
main.DOGETFS = False

# get names (disabled)
#nameInit()

# keys dummy
keys = dummy()
keys.regen = pygame.K_SPACE

# run program
run()
