from .XmlTag import XmlTag

class WebControl (XmlTag):

	def __init__(self, tag_name):
		XmlTag.__init__(self, tag_name)

	def create_from_json(self, json):
		if "components" in json:
			for component in json["components"]:
				wc = WebControl.from_json(component)
				self.add(wc)
	
	@staticmethod
	def from_json(json):
		ctl = None

		if json["type"] == "box":
			orientation = "vertical"
			if "orientation" in json:
				orientation = json["orientation"]
			
			from .controls import Box
			ctl = Box(orientation)
		if json["type"] == "grid":
			from .controls import Grid, GridRow, GridCell
			ctl = Grid()

			if "rows" in json:
				for row in json["rows"]:
					row_ctl = GridRow()
					for cell in row["cells"]:
						ctl1 = WebControl.from_json(cell)
						if not ctl1 is None:
							td = GridCell()
							td.add(ctl1)
							row_ctl.add(td)
					ctl.add(row_ctl)

		elif json["type"] == "label":
			from .controls import Label
			text = ""
			if "text" in json:
				text = json["text"]
				
			ctl = Label(text)
		elif json["type"] == "text":
			from .controls import TextBox
			ctl = TextBox()
		elif json["type"] == "password":
			from .controls import TextBox
			ctl = TextBox("password")

		if not ctl is None:
			if "components" in json:
				for component in json["components"]:
					wc = WebControl.from_json(component)
					ctl.add(wc)

		print(json["type"] + " is " + str(ctl))
		return ctl

	def render_json(self):
		return None