import app
import constInfo
import ui
import uiToolTip
import item
import localeInfo
import player
import grp
import time
import event

class TombolaWindow(ui.BoardWithTitleBar):
	
	def __init__(self):
		ui.BoardWithTitleBar.__init__(self)
		self.__BuildWindow()
		
	def __del__(self):
		ui.BoardWithTitleBar.__del__(self)
	
	def __BuildWindow(self):
		
		self.xOpen = 0
		self.yOpen = 0
		
		self.speedDown = 5
		self.timeDiff = 0.1
		
		self.tooltipItem = None
		
		self.animate = FALSE
		self.blink = FALSE
		
		self.blinkTimes = 6
		self.blinkLast = 0
		
		self.position = 1
		self.toPosition = 16
		self.lastClock = 0
		
		self.SetSize(341, 364)
		self.AddFlag("float")
		self.AddFlag("movable")
		self.SetTitleName("Metin2Neleus - Roata Destinului")
		self.SetCloseEvent(ui.__mem_func__(self.OnClose))
		
		image = ui.ImageBox()
		image.SetParent(self)
		image.LoadImage("locale/ro/ui/tombola/background.tga")
		image.SetPosition(6, 29)
		image.Show()
		self.background = image
		
		select = ui.ImageBox()
		select.SetParent(self.background)
		select.SetPosition(0, 0)
		select.LoadImage("locale/ro/ui/tombola/1.tga")
		select.Hide()
		self.select = select
		
		button = ui.Button()
		button.SetParent(self.background)
		button.SetPosition(125, 124)
		button.SetUpVisual("locale/ro/ui/tombola/button.tga")
		button.SetOverVisual("locale/ro/ui/tombola/button_hover.tga")
		button.SetDownVisual("locale/ro/ui/tombola/button_down.tga")
		button.SetEvent(ui.__mem_func__(self.OnButtonClick))
		button.Show()
		self.run = button
		
		self.slots = [
			self.__CreateSlot(1, 161 - 5, 53 - 4),
			self.__CreateSlot(2, 207 - 5, 63 - 4),
			self.__CreateSlot(3, 248 - 5, 93 - 4),
			self.__CreateSlot(4, 277 - 5, 135 - 4),
			self.__CreateSlot(5, 290 - 5, 182 - 4),
			self.__CreateSlot(6, 277 - 5, 228 - 4),
			self.__CreateSlot(7, 248 - 5, 271 - 4),
			self.__CreateSlot(8, 207 - 5, 298 - 4),
			self.__CreateSlot(9, 160 - 5, 307 - 4),
			self.__CreateSlot(10, 114 - 5, 296 - 4),
			self.__CreateSlot(11, 72 - 5, 269 - 4),
			self.__CreateSlot(12, 44 - 5, 229 - 4),
			self.__CreateSlot(13, 30 - 5, 183 - 4),
			self.__CreateSlot(14, 41 - 5, 133 - 4),
			self.__CreateSlot(15, 70 - 5, 92 - 4),
			self.__CreateSlot(16, 112 - 5, 63 - 4)
		]
		
		self.items = {}
		
	def __CreateSlot(self, number, x, y):
		grid = ui.GridSlotWindow()
		grid.SetParent(self)
		grid.SetPosition(x, y)
		grid.ArrangeSlot(number, 1, 1, 32, 32, 0, 0)
		grid.SetOverInItemEvent(ui.__mem_func__(self.OverInItem))
		grid.SetOverOutItemEvent(ui.__mem_func__(self.OverOutItem))
		grid.RefreshSlot()
		grid.Show()
		
		return grid
	
	def OverInItem(self, slotIndex):
		if slotIndex < 1 or slotIndex > 16:
			return
		
		self.tooltipItem = uiToolTip.ItemToolTip()
		self.tooltipItem.AddItemData(self.items[slotIndex - 1], 0, 0)
		self.tooltipItem.Show()

	def OverOutItem(self):
		if self.tooltipItem:
			self.tooltipItem.HideToolTip()
			
	def OnButtonClick(self):
		self.run.Disable()
		self.run.Down()
		constInfo.VID = "tombola|begin"
		event.QuestButtonClick(constInfo.TOMBOLA)
	
	def OnPrepare(self, items):
		items = items.split("|")
		
		j = 0
		for i in range(0, 32, 2):
			self.slots[j].SetItemSlot(j + 1, int(items[i]), int(items[i + 1]))
			self.items[j] = int(items[i])
			j += 1
		
	def OnRun(self, data):
		data = data.split("|")
		
		self.toPosition = int(data[0])
		self.speedDown = int(data[1])
		
		self.blinkTimes = 6
		self.blinkLast = 0
		
		self.position = 1
		self.animate = TRUE
		self.blink = FALSE
		self.timeDiff = 0.1
		
		self.lastClock = app.GetTime()
		
	def OnUpdate(self):
		if self.animate == TRUE:
			if (app.GetTime() - self.lastClock) >= self.timeDiff:
				self.lastClock = app.GetTime()
				
				self.toPosition -= 1
				
				if self.toPosition <= self.speedDown:
					self.timeDiff += 0.1
				
				self.position += 1
				if self.position == 17:
					self.position = 1
				
				if not self.select.IsShow():
					self.select.Show()
				
				self.select.LoadImage("locale/ro/ui/tombola/" + str(self.position) + ".tga")
				
				if self.toPosition == 0:
					self.animate = FALSE
					self.blink = TRUE
					
					self.run.Enable()
					self.run.SetUp()
					
					constInfo.VID = "tombola|end"
					event.QuestButtonClick(constInfo.TOMBOLA)
					
		if self.blink == TRUE and (app.GetTime() - self.blinkLast) >= 0.3:
			if self.blinkTimes > 0:
				if self.blinkTimes % 2:
					self.select.Hide()
				else:
					self.select.Show()
				self.blinkTimes -= 1
			else:	
				self.blink = FALSE
				self.blinkTimes = 6
				self.blinkLast = 0
				self.select.Hide()
				self.select.LoadImage("locale/ro/ui/tombola/1.tga")
				
			self.blinkLast = app.GetTime()
			
		(x, y, z) = player.GetMainCharacterPosition()
		if abs(x - self.xOpen) > 1000 or abs(y - self.yOpen) > 1000:
			self.Close()
			
	def Open(self):
		if not self.IsShow():
			self.Show()
			(self.xOpen, self.yOpen, z) = player.GetMainCharacterPosition()
		else:
			self.Close()
			
	def Close(self):
		if self.IsShow():
			self.Hide()
			
	def OnClose(self):
		self.Close()
		
	def OnPressEscapeKey(self):
		self.Close()
		return TRUE