from .XmlAttribute import XmlAttribute
from .XmlTagStyles import XmlTagStyles

class XmlTag:

	def __init__(self, tag_name : str):
		self.__tag_name = tag_name
		self.__style = XmlTagStyles()
		self.__attributes = []
		self.__controls = []
		self.__content = None

	def add_attribute(self, name, value):
		self.__attributes.append(XmlAttribute(name, value))

	def add(self, ctl):
		self.__controls.append(ctl)

	def get_controls(self):
		return self.__controls

	def get_tag_name(self):
		return self.__tag_name

	def get_content(self):
		return self.__content
	def set_content(self, value):
		self.__content = value

	def get_style(self) -> XmlTagStyles:
		return self.__style

	def has_content_or_controls(self):
		return not (self.get_content() is None and len(self.__controls) == 0)

	def render_before_html(self):
		html = "<" + self.get_tag_name()

		if self.get_style().has_classes():
			classname = self.get_style().get_class()
			if not (classname is None or len(classname) == 0):
				html += " class=\"" + classname + "\""

		if self.get_style().has_rules():
			rulestr = self.get_style().get_rules_css()
			if not (rulestr is None or len(rulestr) == 0):
				html += " style=\"" + rulestr + "\""

		if (len(self.__attributes) > 0):
			html += " "
			
		for i in range(len(self.__attributes)):
			attr = self.__attributes[i].name
			html += attr + "=\"" + self.__attributes[i].value + "\""
			if i < len(self.__attributes) - 1:
				html += " "

		if self.has_content_or_controls():
			html += ">"

		return html
	def render_after_html(self):
		if self.has_content_or_controls():
			return "</" + self.get_tag_name() + ">"
		else:
			return " />"

	def render_html(self):
		html = ""
		html += self.render_before_html()
		if self.get_content() is None:
			for ctl in self.get_controls():
				if not ctl is None:
					html += ctl.render_html()
		else:
			html += self.get_content()
			
		html += self.render_after_html()

		return html