##############################################################################
# gamestate.py
##############################################################################
# Classes related to the game play state.
##############################################################################
# 12/12 Flembobs
##############################################################################

import pygame
import random
from weakref import WeakKeyDictionary       
       
from engine.model import *
from engine.events import *
from engine.systemevents import *

from gui import *

##############################################################################
# GAME EVENTS
##############################################################################

class ManMoveRequest(Event):
   """
   Generated by the game state when the player is requested to move.
   """
   
   def __init__(self,move_by):
      """
      move_by - tuple of amount to move by (x_amount,y_amount)
      """
      self.move_by = move_by
      
class ManColourChangeRequest(Event):
   """
   Generated when the colour change button is pressed.
   """
   
   def __init__(self):
      pass
      
##############################################################################
# GAME EVENTS - MANAGER AND LISTENER CLASSES
##############################################################################

class GameEventManager(EventManager):
   listeners = WeakKeyDictionary()

class GameEventListener(Listener):
   
   def __init__(self):
      Listener.__init__(self,GameEventManager)

##############################################################################
# GAME OBJECTS - MAN
##############################################################################

class Man(GameObject,GameEventListener):
   
   def __init__(self,pos):
      GameEventListener.__init__(self)

      self.pos = pos
      self.colour = (0,255,0)
      
   #--------------------------------------------------------------------------
          
   def render(self,surface):
      pygame.draw.rect(surface,self.colour,pygame.Rect(self.pos,(16,16)))
      
   #--------------------------------------------------------------------------
   
   def notify(self,event):
      
      if isinstance(event,ManMoveRequest):
         self.pos = (self.pos[0]+event.move_by[0],\
                     self.pos[1]+event.move_by[1])
                     
      if isinstance(event,ManColourChangeRequest):
         self.colour = (random.randint(50,255),random.randint(50,255),
                        random.randint(50,255))
                       
         

##############################################################################
# GAME STATE CLASS
##############################################################################

class GameState(State,SystemEventListener,GUIEventListener):

   def __init__(self):
   
      SystemEventListener.__init__(self)
      GUIEventListener.__init__(self)
      
      self.game_objects = []
        
      #create the man we can control with buttons
      self.man = Man((240,240))
      self.game_objects.append(self.man)
      
      #create the button that changes the man's colour
      b_rect = pygame.Rect((10,400),(20,20))
      
      b_norm_surf = pygame.Surface((20,20))
      b_norm_surf.fill((255,255,255))
      
      b_mo_surf = pygame.Surface((20,20))
      b_mo_surf.fill((255,0,0))
      
      self.colour_button = Button(b_rect,b_norm_surf,b_mo_surf)
      self.game_objects.append(self.colour_button)
      
      #create the static text displayed at the top left
      static_text = Text("Skeleton Game",(255,255,255),30)
      static_text.rect.left = 10
      static_text.rect.top = 10
      self.game_objects.append(static_text)
      
      #create the text box that can be used to input text
      text_ip_box = TextInputBox((10,80),460,"You can input text here.",
                                  (0,255,0),(0,0,0),20)
      self.game_objects.append(text_ip_box)
      
   #--------------------------------------------------------------------------
   
   def notify(self,event):
   
      if isinstance(event,TickEvent):
         SystemEventManager.post(DrawRequestEvent(self.game_objects))
   
      if isinstance(event,KeyboardEvent):
         if event.key == pygame.K_ESCAPE:
            SystemEventManager.post(QuitEvent()) 
            
      if isinstance(event,KeyboardEvent):
         if event.type == pygame.KEYDOWN:
            
            move_request = None
            
            if event.key == pygame.K_UP:
               move_request = ManMoveRequest((0,-16))
            if event.key == pygame.K_DOWN:
               move_request = ManMoveRequest((0,16))
            if event.key == pygame.K_RIGHT:
               move_request = ManMoveRequest((16,0))
            if event.key == pygame.K_LEFT:
               move_request = ManMoveRequest((-16,0))
               
            GameEventManager.post(move_request)
            
      if isinstance(event,ButtonClickedEvent):
      
         if event.button is self.colour_button:
            GameEventManager.post(ManColourChangeRequest())
            
      #test mouse over with a print
      if isinstance(event,ButtonMouseOverEvent):
         print "Mouse over: "+str(event.button)
               
               
   