# -*- coding: UTF-8 -*-
# -----------------------------------------------------------------------------
#    xierpa server
#    Copyright (c) 2014+  buro@petr.com, www.petr.com, www.xierpa.com
#    
#    X I E R P A  3
#    Distribution by the MIT License.
#
# -----------------------------------------------------------------------------
#
#    make.py
#
import webbrowser
from xierpa3.components import Theme, Page, Column 
from xierpa3.builders.cssbuilder import CssBuilder
from xierpa3.builders.htmlbuilder import HtmlBuilder
from xierpa3.attributes import Em, Margin, Color, Perc 

BODYFAMILY = 'Georgia, serif'

class BaseHelloWorldText(Column):

    # Get Constants->Config as class variable, so inheriting classes can redefine values.
    C = Column.C

    def buildBlock(self, b):
        u"""Build a content base class. Inherited by specific HelloWorld... classes
        that define the method **self.helloWorldText(b). In this example the CSS parameter
        are still defined inside the block. Different from the real usage of BluePrint API parameter,
        that allow modification from including components or inheriting components."""
        b.div(class_=self.getClassName(), margin=Margin(0, self.C.AUTO, 0, self.C.AUTO), 
              width='70%', maxwidth=700, minwidth=300, backgroundcolor='#222', 
              padding=Em(0.5), fontfamily=BODYFAMILY, fontsize=Em(4), textalign=self.C.CENTER,
              lineheight=Em(1.4))
        self._helloWorldText(b)
        b._div()
        b.div(class_=self.C.CLASS_CAPTION, color=Color('#888'), 
              margin=Margin(0, self.C.AUTO, 0, self.C.AUTO), 
              width=Perc(70), maxwidth=700, minwidth=300,
              paddingleft=Em(0.5), fontfamily=BODYFAMILY, fontsize=Em(0.8), 
              textalign=self.C.CENTER, lineheight=Em(1.4), fontstyle=self.C.ITALIC)
        b.text('Intentionally non-responsive page example. Generated by Xierpa3.')
        b._div()

class HelloWorldHome(BaseHelloWorldText):
    u"""Private method. Inheriting from *BaseHelloWorldText* component, the class name generated by
    @self.getClassName()@ results in @HelloWorldHome@. Color is different per page."""
    def _helloWorldText(self, b):
        b.div(color=Color('yellow')) # Color name will be translate to more reliable hex code.
        b.text('Hello, world!')
        b._div()

    # TODO: Needs to clean up. JSON example goes to separate example site.
    def buildAjaxDict(self, site, d):
        myD = d['HelloWorldHome'] = {}
        myD['name'] = 'Petr'
        myD['message'] = 'Hello'
        myD['fontName'] = site.e.form['font'] or 'notdefined'
        return d

class HelloWorldOther1(BaseHelloWorldText):
    u"""Private method. Inheriting from *BaseHelloWorldText* component, the class name generated by
    @self.getClassName()@ results in @HelloWorldHome@. Color is different per page."""
    def _helloWorldText(self, b):
        b.div(color=Color('#00FF00')) # Show the text on this page in another color, to visualize the difference.
        # TODO: Needs to clean up. JSON example goes to separate example site.
        b.a(href='/ajax/font-MyFont')
        b.text('Hello, world on another page using MyFont')
        b._a()
        b._div()

    # TODO: Needs to clean up. JSON example goes to separate example site.
    def buildAjaxDict(self, site, d):
        myD = d['HelloWorldOther1'] = {}
        myD['message'] = 'Hello ' * 10
        return d

class HelloWorldOther2(BaseHelloWorldText):
    u"""Private method. Inheriting from *BaseHelloWorldText* component, the class name generated by
    @self.getClassName()@ results in @HelloWorldHome@. Color is different per page."""
    def _helloWorldText(self, b):
        b.div(color=Color('#00FFFF')) # Show the text on this page in another color, to visualize the difference.
        b.text('And yet another world on this page.')
        b._div()
        
class Navigation(Column):

    # Get Constants->Config as class variable, so inheriting classes can redefine values.
    C = Column.C

    def buildBlock(self, b):
        b.div(class_=self.getClassName(), margin=Margin(0, self.C.AUTO, 0, self.C.AUTO), 
              width=Perc(40), maxwidth=700, minwidth=300, backgroundcolor=Color('#DDD'), 
              padding=Em(0.5), fontfamily=BODYFAMILY, textalign=self.C.CENTER) 
        # Still doing content and page identifiers directly, without adapter, in this example.     
        b.text(' | ')
        for pageId in HelloWorldPages.PAGES:
            # Making a reference to the page class is enough to build the url.
            b.a(href='/'+pageId, fontsize=Em(1), color=Color('#444'))
            b.text(pageId.capitalize()) # Make simple label in the menu from page id..
            b._a()
            b.text(' | ')
        b._div()
            
class HelloWorldPages(Theme):
    u"""The **HelloWorldLayout** class implements a basic "Hello, world!" page, running as
    batch process, saving the result as an HTML file. Also it is available in the example webserver,
    e.g. through the Xierpa3App."""
    # Get Constants->Config as class variable, so inheriting classes can redefine values.
    C = Theme.C

    TITLE = u'The layout version of "Hello, world!" page.' # Use as title of window.

    TEMPLATE_OTHER1 = 'other'
    TEMPLATE_OTHER2 = 'otherworld'
    PAGES = (C.TEMPLATE_INDEX, TEMPLATE_OTHER1, TEMPLATE_OTHER2)

    def baseComponents(self):
        u"""Create a theme site with just one single template home page. Answer a list
        of page instances that are used as templates for this site."""
        # Create an instance (=object) of the text component to be placed on the page.
        navigation = Navigation()
        home = HelloWorldHome() # Example text component for the home page.
        other1 = HelloWorldOther1() # Example text component for the other1Page
        other2 = HelloWorldOther2() # Example text component for the other2Page
        # Create an instance (=object) of the page, containing the "hw" component.
        # The class is also the page name in the url. The navigation simply refers
        # to the url by class nane.
        homePage = Page(class_=self.C.TEMPLATE_INDEX, components=(navigation, home), 
            title=self.TITLE + '|' + self.C.TEMPLATE_INDEX)
        other1Page = Page(class_=self.TEMPLATE_OTHER1, components=(navigation, other1), 
            title=self.TITLE + '|' + self.TEMPLATE_OTHER1)
        other2Page = Page(class_=self.TEMPLATE_OTHER2, components=(navigation, other2), 
            title=self.TITLE + '|' + self.TEMPLATE_OTHER2)
        # Answer a list of types of pages for this site.
        return [homePage, other1Page, other2Page]

    def make(self, root=None):
        u"""The instance of this class builds CSS and HTML files at the optional path **root**.
        If not defined, then the default ~/Desktop/Xierpa3Examples/[component.name] is used as export path,
        as set by Builder.DEFAULT_ROOTPATH"""
        # Create an "instance" (=object) of type "HelloWorldLayout". The type (=class) defines
        # the behavior of the object that is made by calling the class.

        # C S S
        # Create the main CSS builder instance to build the SASS/CSS part of the site.
        cssBuilder = CssBuilder()
        # Compile (=build) the SCSS to CSS and save the file in "css/style.css".
        self.build(cssBuilder) # Build from entire site theme, not just from template. Result is stream in builder.
        cssBuilder.save(self, root) 
    
        # H T M L
        # Create the main HTML builder instance to build the HTML part of the site.
        htmlBuilder = HtmlBuilder()
        # Compile the HTML and save the resulting HTML file in "helloWorld.html".
        self.build(htmlBuilder) # Build from entire site theme, not just from template. Result is stream in builder.
        # Answer the path, so we can directly open the file with a browser.
        return htmlBuilder.save(self, root)  
    
if __name__ == '__main__':
    # TODO: Why is the browser loading the CSS for every page?
    # This construction "__name__ == '__main__'" makes this Python file only 
    # be executed when called in direct mode, such as "python make.py" in the terminal.         
    # Since no rootPath is added to make(), the file export is in ~/Desktop/Xierpa3Examples/HelloWorldLayout/   
    path = HelloWorldPages().make()
    webbrowser.open(path)
