from LUIFrame import LUIFrame
from LUILabel import LUILabel
from LUILayouts import LUIHorizontalLayout
from LUIVerticalLayout import LUIVerticalLayout
from LUIObject import LUIObject
from LUIButton import LUIButton


class UI_WindowTitleBar(LUIObject):
	def __init__(self, title="No title", **kwargs):
		LUIObject.__init__(self, solid=True, **kwargs)

		LUILabel(title, parent=self, top=1, center_horizontal=True)
		self.closeButton = LUIButton(text="X", parent=self, margin=0, top=1, right=16)
		self.closeButton._label.margin = 0, 12, 0, 12

		self.width = self.parent.parent.parent.width

	def _drag(self, event):
		if base.mouseWatcherNode.hasMouse():
			self.parent.parent.parent.set_pos(self.mousePosOffset())

	def on_mousedown(self, event=None):
		self.bind('mousemove', self._drag)

	def on_mouseup(self, event=None):
		self.unbind('mousemove')

	def mousePosOffset(self):
		left, top = self.mousePos()
		left -= self.parent.parent.parent.width / 2
		top -= 20
		return (left, top)

	def mousePos(self):
		if base.mouseWatcherNode.hasMouse():
			mpos = base.mouseWatcherNode.getMouse()
			mousePos = (mpos.getX() * base.getAspectRatio(), mpos.getY())

			screenWidth = base.win.getProperties().getXSize()
			screenHeight = base.win.getProperties().getYSize()
			ratio = base.getAspectRatio()

			# https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio
			left = (((mousePos[0] - -ratio) * (screenWidth - 0)) / (ratio - -ratio)) + 0
			top = (((mousePos[1] - 1) * (screenHeight - 0)) / (-1 - 1)) + 0

			return (left, top)
		return(0,0)

class UI_Window(LUIFrame):
	def __init__(self, title="No title", **kwargs):
		LUIFrame.__init__(self, inner_padding=0, style=LUIFrame.FS_raised, **kwargs)

		self.verticalLayout = LUIVerticalLayout(parent=self)
		self.contentWidget = None
		self.titleBar = UI_WindowTitleBar(title=title, parent=self.verticalLayout.cell(), margin=0)

		self.titleBar.closeButton.bind("click", self.close)

	def setContentWidget(self, widget):
		if self.contentWidget:
			self.contentWidget.parent = None
		self.contentWidget = widget
		self.contentWidget.parent = self.verticalLayout.cell()
		self.width = (self.contentWidget.width
					+ self.get_margin_right()
					+ self.get_margin_left()
					+ self.get_padding_right()
					+ self.get_padding_left())
		self.titleBar.width = self.width

	def close(self, event=None):
		self.parent = None

class UI_CollapsableGroupBox(LUIObject):
	def __init__(self, title="No title", orientation="vertical", **kwargs):
		LUIObject.__init__(self, solid=True, **kwargs)
		if orientation == "vertical":
			self._mainLayout = LUIVerticalLayout(parent=self)
		else:
			self._mainLayout = LUIHorizontalLayout(parent=self)

		self._collapsed = False
		self._widgets = []

		# Header
		self._headerLayout = LUIHorizontalLayout(parent=self._mainLayout.cell())
		self._collapseButton = LUILabel("^", margin=(0,5,0,0), font_size=24, parent=self._headerLayout.cell())
		LUILabel(title, font_size=20, parent=self._headerLayout.cell())

		self.bind("click", self.toggleState)

	def toggleState(self, event=None):
		self._collapsed = not self._collapsed
		if self._collapsed:
			self._collapseButton.set_text(">")
			for widget in self._widgets:
				widget.hide()
			self.height = 20
		else:
			self._collapseButton.set_text("^")
			for widget in self._widgets:
				widget.show()
			self.height = None

	def addWidget(self, widget):
		self._widgets.append(widget)
		widget.parent = self._mainLayout.cell()

class UI_QuestLogWindow(UI_Window):
	def __init__(self, **kwargs):
		UI_Window.__init__(self, title="Quest log", **kwargs)
		self.setContentWidget(UI_QuestLogWidget())

class UI_QuestLogEntry(LUIObject):
	def __init__(self, title="No title", **kwargs):
		LUIObject.__init__(self, solid=True, **kwargs)
		self.layout = LUIHorizontalLayout(parent=self)

		LUILabel(title, margin=(0,10,0,0), parent=self.layout.cell(), font_size=18)
		LUILabel("0/1 complete", parent=self.layout.cell(), font_size=14)

	def on_mouseover(self, event):
		self.color = (1,0,0,1)

	def on_mouseout(self, event):
		self.color = (1,1,1,1)

class UI_QuestsListWidget(LUIFrame):
	def __init__(self, **kwargs):
		LUIFrame.__init__(self, style=LUIFrame.FS_sunken, inner_padding=0, scrollable=True, **kwargs)
		self.layout = LUIVerticalLayout(parent=self)

		groupBox = UI_CollapsableGroupBox(title="In progress", parent=self.layout.cell())
		for i in range(1,20):
			groupBox.addWidget(UI_QuestLogEntry(title="     Very magical quest {}".format(i)))

		groupBox2 = UI_CollapsableGroupBox(title="Completed", parent=self.layout.cell())
		for i in range(1,20):
			groupBox2.addWidget(UI_QuestLogEntry(title="     Yeah quest {}".format(i)))

class UI_QuestWidget(LUIFrame):
	def __init__(self, **kwargs):
		LUIFrame.__init__(self, style=LUIFrame.FS_sunken, inner_padding=0, scrollable=True, **kwargs)
		self.layout = LUIVerticalLayout(parent=self)

		txt = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nibh elit, volutpat at scelerisque at, blandit eget tellus. Vestibulum viverra libero ante. Morbi pharetra justo eget cursus mattis. Curabitur vitae efficitur ligula, vel molestie tellus. Nunc tempus blandit lectus, in malesuada enim tristique non. Etiam sed magna sodales, egestas ligula quis, tristique velit. Aliquam quam ex, tristique at ex molestie, ullamcorper tristique nulla. Phasellus consequat orci ac diam congue mollis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque mauris nunc, ultrices ut aliquam nec, elementum at mi. Nulla nec dapibus odio. Proin nec ultricies odio, et eleifend neque. Cras sapien enim, dictum eu erat nec, molestie sodales turpis. Integer eget mauris felis. Aenean eleifend ultricies nulla, eget tempor libero lacinia vel.

Cras sed dui enim. Mauris in eros efficitur, elementum enim a, venenatis mi. Quisque id odio eget sem rhoncus lacinia nec ac eros. Etiam porta mi ac lacinia tristique. Cras lectus leo, dignissim ac metus tincidunt, suscipit mattis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec suscipit sollicitudin sagittis. Aliquam luctus ac est non dapibus. Nullam ultricies, turpis in aliquam egestas, est mauris vehicula erat, sed tristique nisl lorem ut sem. Donec efficitur varius turpis non rutrum. Curabitur lobortis, lorem a posuere egestas, tellus neque gravida arcu, vitae mattis enim sem at enim. Nam ac dui risus.

Vivamus quis gravida quam. Morbi accumsan vestibulum tellus sed luctus. Cras et justo ac lacus feugiat condimentum at vel ex. Proin scelerisque rutrum quam et iaculis. Nunc dolor nibh, ullamcorper ac ante sit amet, dignissim consectetur enim. Cras placerat leo quam, non hendrerit mi pulvinar sed. Nulla mattis euismod sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut porta arcu mauris, et porttitor eros mattis a.

Duis bibendum facilisis sodales. Cras aliquam nibh ultrices pulvinar malesuada. Nullam eu mollis augue. Sed semper turpis purus. Proin sit amet felis condimentum, ornare massa vitae, blandit velit. Nulla sed convallis ante, at blandit est. Quisque dapibus porttitor cursus. Phasellus eu consequat nulla. Vestibulum dui dolor, gravida sit amet sapien quis, rutrum scelerisque tortor. Donec sed nisl nulla. Donec sed leo urna.

Cras ut dapibus velit, nec hendrerit lectus. Nulla faucibus dapibus posuere. Aenean rhoncus mi sollicitudin justo congue pharetra. Proin blandit urna et urna porta pulvinar. Donec bibendum nunc diam, quis fringilla tortor efficitur in. In ante sem, condimentum nec nunc in, venenatis molestie mi. Suspendisse lectus lectus, pellentesque sit amet commodo quis, pellentesque at mi. Mauris ornare dignissim justo in convallis. Sed quis urna eget dolor dictum malesuada dictum efficitur dolor. Suspendisse erat felis, hendrerit non ornare vel, tempor sed ex. Suspendisse viverra mi eu semper ullamcorper. In viverra tellus est, cursus bibendum purus vulputate sit amet. Donec faucibus diam sit amet rhoncus varius. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris a vulputate diam. """

		width = (self.width
				- self.get_margin_left()
				- self.get_margin_right()
				- self.get_padding_left()
				- self.get_padding_right())

		LUILabel("Quest title", margin=(0,10,0,0), parent=self.layout.cell(), font_size=24)
		LUILabel(txt, wordwrap=True, width=width, parent=self.layout.cell(), font_size=18)

class UI_QuestLogWidget(LUIFrame):
	def __init__(self, **kwargs):
		LUIFrame.__init__(self, style=LUIFrame.FS_sunken, inner_padding=0, margin=(0,0,0,0), padding=(0,0,0,0), width=800, height=550, scrollable=False, **kwargs)
		self.layout = LUIHorizontalLayout(parent=self)

		UI_QuestsListWidget(parent=self.layout.cell(), width=self.width/2, height=self.height)
		UI_QuestWidget(parent=self.layout.cell(), width=self.width/2, height=self.height)
