from .WebControl import WebControl
from .WebStyleSheet import WebStyleSheet
from .XmlTag import XmlTag

class WebPage (WebControl):

	def __init__(self):
		WebControl.__init__(self, 'html')
		self.__title = None
		self.__body = XmlTag('body')
		self.__head = XmlTag('head')
		self.__style_sheet = WebStyleSheet()
		
		self.add(self.__head)
		self.__head.add(self.__style_sheet)

		self.add(self.__body)

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

		if "title" in json:
			self.set_title(json["title"])

	def get_style_sheet(self):
		return self.__style_sheet
	def set_style_sheet(self, style_sheet):
		self.__style_sheet = style_sheet

	def get_head(self):
		return self.__head
	def get_body(self):
		return self.__body

	def get_title(self):
		return self.__title

	def set_title(self, value):
		self.__title = value

	def render_json(self):
		return {
			"title": "Not Found",
			"components": None
		}

	def render_before_html(self):

		title = self.get_title()
		if title is None:
			title = ""

		html = """<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">"""
			
		return html

	def render_after_html(self):
		html = """
</html>"""
		return html