# 3rd party python modules
import pygame
pygame.init()

# Own modules
from game.classes.Text import Text
from game.classes.Preferences import *
from game.components import helpers

class Button( pygame.sprite.Sprite ):
	def __init__( self, ret_value, text_inactive, centerxy, mouse_over_sound, mouse_click_sound, unique_name=None, text_active=None, size=20, font="SysFont", color_inactive=BLACK, color_active=YELLOW, antialias=True, bold_inactive=False, bold_active=True, italic_inactive=False, italic_active=True, background_inactive=None, background_active=None, unique_id = None ):
		pygame.sprite.Sprite.__init__( self )

		self._ret_value = ret_value # What to return if the button has been clicked.
		self._text_inactive = text_inactive
		if text_active == None:
			self._text_active = self._text_inactive
		else:
			self._text_active = text_active
		self._centerxy = centerxy

		# Sounds.
		self._mouse_over_sound = helpers.load_sound( mouse_over_sound )
		self._mouse_over_sound_played = False

		self._mouse_click_sound = helpers.load_sound( mouse_click_sound )
		
		self._unique_name = unique_name
		self._size = size
		self._font = font
		self._color_inactive = color_inactive
		self._color_active = color_active
		self._antialias = antialias
		self._bold_inactive = bold_inactive
		self._bold_active = bold_active
		self._italic_inactive = italic_inactive
		self._italic_active = italic_active
		self._background_inactive = background_inactive
		self._background_active = background_active

		# The mouse button has to be released before it can be pressed again.
		self._mouse_already_down = False

		# Draw both states of the button onto two unique surfaces.
		self.draw_button_states()

		# Show the inactive state first.
		self.image = self._button_inactive.image
		self.rect = self._button_inactive.rect

		# Additional properties.
		self.unique_id = unique_id
		self.selected = False

	def draw_button_states( self ):
		# Draw both states of the button onto two unique surfaces.
		self._button_inactive = Text( self._text_inactive, self._centerxy, self._unique_name, self._size, self._font, self._color_inactive, self._antialias, self._bold_inactive, self._italic_inactive, self._background_inactive )
		self._button_active = Text( self._text_active, self._centerxy, self._unique_name, self._size, self._font, self._color_active, self._antialias, self._bold_active, self._italic_active, self._background_active )


	def update( self, events ):
		if self.selected:
			self.image = self._button_active.image
			self.rect = self._button_active.rect
			if not self._mouse_over_sound_played:
				sound_channel.play( self._mouse_over_sound )
				self._mouse_over_sound_played = True
				
			for e in events:
				if e.type == pygame.KEYDOWN:
					if e.key == pygame.K_RETURN:
						sound_channel.play( self._mouse_click_sound )
						return self._ret_value			
		else:
			self.image = self._button_inactive.image
			self.rect = self._button_inactive.rect
			self._mouse_over_sound_played = False			


	def set_text( self, new_text ):
		"""
		Changes the text.
		"""

		self._button_inactive.set_text( new_text )
		self._button_active.set_text( new_text )

