from lxml import etree

class Data:
	def __init__(self):
		self._title = ""
		self._prev = ""
		self._next = ""
		self._body = []

	def get_prev(self):
		return self._prev.split(".html")[0] + ".protoxml"

	def get_next(self):
		return self._next.split(".html")[0] + ".protoxml"


class Image:
	def __init__(self, src, width=-1, height=-1):
		self._src = src
		self._height = height
		self._width = width

	def show(self):
		print "image src: " + self._src + "(" + str(self._width) + " x " + str(self._height) + ")"

	def to_xml(self):
		img = etree.Element("image")
		img.attrib["name"] = self._src
		if self._width != -1:
			img.attrib["width"] = self._width
		if self._height != -1:
			img.attrib["height"] = self._height
		return img

	def to_element(self, tag_name="element"):
		el = etree.Element(tag_name)

		layout = etree.Element("layout")
		layout.attrib["secondary_alignment"] = "CENTER"
		layout.attrib["gap"] = "0"
		layout.attrib["fit_content_height"] = "true"
		top_mg = etree.Element("top_margin")
		top_mg.text = "10"
		layout.append(top_mg)
		el.append(layout)
		el.append(self.to_xml())
		return el


class Paragraph:
	def __init__(self, list_ident=0):
		self._content = []
		self._list_indent = list_ident

	def show(self):
		content = ""
		for c in self._content:
			content += " " + c.to_str()
		print "paragraph (" + str(self._list_indent) + "): " + content + "\n"

	def to_xml(self):
		text = etree.Element("text")
		for c in self._content:
			c_xml = c.to_xml()
			text.append(c_xml)
		return text

	def to_element(self, tag_name="element"):
		el = etree.Element(tag_name)

		if self._list_indent != 0:
			layout = etree.Element("layout")
			el.append(layout)

			left_mg = etree.Element("left_margin")
			left_mg.text = str(10 * self._list_indent)
			layout.append(left_mg)

		el.append(self.to_xml())
		return el


class ParagraphContent:
	pass


class TextContent(ParagraphContent):
	def __init__(self, text="", href="", new_line = False):
		self._text = text
		self._href = href
		self._new_line = new_line

	def get_string(self, clean = True):
		text = self._text
		if not text:
			text = ""
		if clean:
			text = text.strip()
			if text:
				text += " "
		return text

	def get_href(self, clean = True):
		href = self._href
		if clean:
			href = href.split(".html")[0] + ".protoxml"
		return href

	def to_str(self):
		return self.get_string() + (( "->" + self.get_href()) if self._href != "" else "") + "\n"

	def to_xml(self):
		p = etree.Element("p")
		p.attrib["string"] = self.get_string()
		if self._href != "":
			p.attrib["href"] = self.get_href()
		if self._new_line:
			p.attrib["new_line"] = "true"
		return p


class NewLineContent(ParagraphContent):
	def to_str(self):
		return "new line\n"

	def to_xml(self):
		p = etree.Element("p")
		p.attrib["new_line"] = "true"
		return p


class Table:
	def __init__(self):
		self._lines = []
		self._columns_width = []

	def get_column_count(self):
		count = 0
		for line in self._lines:
			line_count = len(line)
			if line_count > count:
				count = line_count
		return count

	def show(self):
		print "table"
		print "width " + " ".join(str(x) for x in self._columns_width)
		for line in self._lines:
			print "new line"
			for cell in line:
				print "new cell"
				cell.show()

	def to_xml(self):
		col_count = self.get_column_count()

		table = etree.Element("table")
		table.attrib["gap"] = "2"
		table.attrib["columnCount"] = str(col_count)
		assert(len(self._columns_width) == col_count)

		# column_count
		for col_index in range(col_count):
			col_attr = etree.Element("columnAttribute")
			col_attr.attrib["index"] = str(col_index)
			if self._columns_width[col_index] < 0:
				col_attr.attrib["useMaxAvailableSpace"] = "true"
			else:
				col_attr.attrib["useMaxAvailableSpace"] = "false"
				col_attr.attrib["fixedColumnWidth"] = str(self._columns_width[col_index])
			table.append(col_attr)

		# cell
		for line in self._lines:
			for col_index in range(col_count):
				if len(line) <= col_index:
					table.append(etree.Element("cell"))
				else:
					cell = line[col_index]
					if isinstance(cell, Image):
						cell_xml = etree.Element("cell")
						table.append(cell_xml)
						cell_xml.append(cell.to_element("element"))
					else:
						table.append(cell.to_element("cell"))

		return table
				
	def to_element(self):
		element = etree.Element("element")
		element.append(self.to_xml())
		return element