from PodSix.Resource import *
from PodSix.Concurrent import Concurrent
from Widget import Widget

class Button(Widget):
	def __init__(self, toggle=False):
		Widget.__init__(self)
		self.down = False
		self.rect = None
		self.toggle = toggle
	
	def MouseDown(self, e):
		if self.rect and self.InRect(e.pos):
			if self.toggle:
				self.down = not self.down
			else:
				self.down = True
			self.triggered = True
	
	def MouseUp(self, e):
		if self.down or self.toggle:
			if not self.toggle:
				self.down = False
			if self.InRect(e.pos):
				self.triggered = True
				self.InternalPressed()
				self.Pressed()
	
	def InternalPressed(self):
		pass
	
	def Pressed(self):
		pass

class TextButton(Button):
	def __init__(self, text, pos = {}, colors=[[0, 0, 0], [255, 255, 255]], font="default", toggle=False):
		self.text = text
		self.font = font
		self.pos = pos
		self.colors = colors
		Button.__init__(self, toggle)
	
	def Draw(self):
		if self.down and self.rect:
			gfx.DrawRect([self.rect.left - 3, self.rect.top - 3, self.rect.width + 7, self.rect.height + 7], self.colors[(not self.down) * 1], 0)
		self.rect = gfx.DrawText(self.text, self.pos, self.colors[self.down * 1])
		gfx.DrawLines([(self.rect.left - 3, self.rect.top - 4), (self.rect.right + 4, self.rect.top - 4)], self.colors[0], False)
		gfx.DrawLines([(self.rect.left - 3, self.rect.bottom + 4), (self.rect.right + 4, self.rect.bottom + 4)], self.colors[0], False)
		gfx.DrawLines([(self.rect.left - 4, self.rect.top - 3), (self.rect.left - 4, self.rect.bottom + 4)], self.colors[0], False)
		gfx.DrawLines([(self.rect.right + 4, self.rect.top - 3), (self.rect.right + 4, self.rect.bottom + 4)], self.colors[0], False)

class ImageButton(Button):
	"""
		Images should be an array of two or three images
			* On image
			* Off image
			* Rollover image (optional)
	"""
	def __init__(self, images, pos, toggle=False):
		Button.__init__(self, toggle)
		self.images = images
		self.pos = pos
		self.rect = None
	
	def Draw(self):
		if len(self.images) > 2 and self.rect and self.InRect(gfx.GetMouse()) and not self.down:
			self.rect = gfx.BlitImage(self.images[2], center=self.pos)
		else:
			self.rect = gfx.BlitImage(self.images[(self.down * 1)], center=self.pos)

class ImageRadioButtonGroup(Widget):
	def __init__(self):
		self.selected = None
		Widget.__init__(self)

class ImageRadioButton(ImageButton):
	"""
		Radio buttons are toggle buttons which know what the others in their group are doing
	"""
	def __init__(self, images, pos, name, group):
		self.group = group
		self.name = name
		ImageButton.__init__(self, images, pos, True)
		self.group.Add(self)
	
	def MouseDown(self, e):
		ImageButton.MouseDown(self, e)
		if self.triggered:
			self.group.triggered = True
	
	def MouseUp(self, e):
		ImageButton.MouseUp(self, e)
		if self.triggered:
			self.group.triggered = True
	
	def InternalPressed(self):
		self.group.selected = self
		for b in self.group.objects:
			b.down = False
		self.down = True


