#! /usr/bin/python
# -*- coding: utf-8 -*-
# In which we pickle!

import pygame, os.path, time, pickle, sys
from pygame.locals import *

clock = pygame.time.Clock()
saved = None
BLACK = [0,0,0]
WHITE = [255,255,255]
RED = [255, 0, 0]
W_HEIGHT = 30
W_WIDTH = 40

class Widgit(pygame.sprite.Sprite):
  filled = None

  def __init__(self, size, position, filled):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface(size)
    self.image.set_alpha(90)
    self.image.fill(WHITE)
    self.rect = self.image.get_rect()
    self.rect.topleft = position
    self.filled = filled
    if filled:
      pygame.draw.line(self.image, RED, (0, W_HEIGHT), (W_WIDTH, 0), 8)
      #self.image.fill(RED)
    else:
      self.image.fill(WHITE)

  def handle_mouse(self):
      self.filled = not self.filled
      #It should have been a function, but the
      #repetition doesn't really bug me until the
      #third time.
      if self.filled:
	pygame.draw.line(self.image, RED, (0, W_HEIGHT), (W_WIDTH, 0), 8)
	#self.image.fill(RED)
      else:
	self.image.fill(WHITE)
	

def main(filename):
  pygame.init()
   
  pygame.display.set_caption(filename)
  screen = pygame.display.set_mode([6*W_WIDTH, 5*W_HEIGHT])
  #screen.fill(WHITE)
  background = pygame.Surface([6*W_WIDTH, 5*W_HEIGHT])
  background.fill(WHITE)

  default_font = pygame.font.get_default_font()
  font = pygame.font.SysFont(default_font, 32, False)

  for i in range(6):
    for j in range(5):
      message = font.render(str(30 - ((6*j)+i)), True, BLACK)
      background.blit(message, [i*W_WIDTH + 10, j*W_HEIGHT + 6])
 
  background.convert()
  screen.blit(background, (0,0))
  pygame.display.update()

  widgits = pygame.sprite.OrderedUpdates()
  saved = []
  
  if os.path.exists("/home/USERNAME/bin/countdown/" + filename):
    saved = pickle.load(open("/home/USERNAME/bin/countdown/" + filename))
  else:
    for i in range(6):
      for j in range(5):
	saved.append([i*W_WIDTH, j*W_HEIGHT, False])

  for i in range(30):
    widgits.add(Widgit([W_WIDTH, W_HEIGHT], [saved[i][0], saved[i][1]],
saved[i][2]))

  done=0
  
  while not done:
                                 
      for e in pygame.event.get():
       if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
        saved=[]  
        for widgit in widgits:
          saved.append([widgit.rect.left, widgit.rect.top, widgit.filled])
          pickle.dump(saved, open("/home/USERNAME/bin/countdown/" + filename,"w"))
        done = 1
        break

       if e.type == MOUSEBUTTONDOWN:
        #making a throwaway spite to check collisions with 
        click = pygame.sprite.Sprite()
        click.rect = Rect(e.pos[0],e.pos[1],1,1)
        saved=[]
	#I realized later that I didn't want to save state when the program
	#exited, but when the state changed (maybe someone rebooted) 
        for widgit in widgits:

	  #It is amazing the extent to which
	  #your own sloppiness stands out when
	  #you can get some distance between what you
	  #meant and actaully did.

	  #I shouldn't have generated a new sprite for
	  #each click either; a new rect would have been
	  #sufficient
          saved.append([widgit.rect.left, widgit.rect.top, widgit.filled])
          pickle.dump(saved, open("/home/USERNAME/bin/countdown/" + filename, "w"))
	#print click.rect.topright
        for doohicky in pygame.sprite.spritecollide(click, widgits, False):
          doohicky.handle_mouse()
      
      widgits.clear(screen, background)
      pygame.display.update(widgits.draw(screen))
      clock.tick(500)
  pygame.quit()
  exit()
    
if __name__ == "__main__":
  #print sys.argv[1]
  main(sys.argv[1])