from os.path import dirname, join
from rst import Rest, Paragraph, Strong, OrderedListItem, ListItem, Title, Link, Transition
from rst import Directive, Em, Quote, Text
from tiramisu.option import *
from tiramisu.config import *
#from makerestdoc import *

docdir = join(dirname(__file__), 'build')

def make_rst_file(filename, rstcontent):
    fh = file(filename, 'w')
    fh.write(rstcontent.text())
    fh.close()

def descr_content(path, prefix, descr, root=False):
    content = Rest()
    title = Title(abovechar="", belowchar="=")
    if root:
        title.join(Text("Configuration's overview for: "), Quote(descr._name))
    else:
        title.join(Text("Group's overview for: "), Quote(descr._name))
    content.add(title)
    content.add(ListItem().join(Strong("name:"), Text(descr._name)))
    if not root:
        content.add(ListItem().join(Strong("path:"), Text(path)))
    content.add(ListItem().join(Strong("description:"), Text(descr.doc)))
    if not root:
        content.add(ListItem().join(Strong("container:"), Text(prefix)))
    if not root:
        content.add(ListItem().join(Strong("type:"), Text(descr.group_type)))
    if not root:
        content.add(ListItem().join(Strong("requirements:"), Text(str(descr._requires))))
    content.add(ListItem().join(Strong("is hidden:"), Text(str(descr._is_hidden()))))
    content.add(ListItem().join(Strong("is disabled:"), Text(str(descr._is_disabled()))))
    content.add(Transition())
    content.add(Title(abovechar="", belowchar="-").join(Text("Ordered list of childrens for:"), Text(path)))
    for opt in descr._children:
        name = opt._name
        link = Link(name + ":", join(path + '.' + name + ".html"))
        # because of SympLink opt
        if hasattr(opt, 'doc'):
            doc = opt.doc
        else:
            doc = name
        content.add(OrderedListItem(link, Text(opt.doc)))
    content.add(Transition())
    content.add(Paragraph(Link("back to index", "index.html")))
    make_rst_file(join(docdir, path + '.txt'), content)
    if root:
        make_rst_file(join(docdir, 'index.txt'), content)
    
def opt_rst_content(path, prefix, descr, value):
    content = Rest()
    title = Title(abovechar="", belowchar="=")
    title.join(Text("Configuration's option overview for: "), Quote(descr._name))
    content.add(title)
    content.add(ListItem().join(Strong("name:"), Text(descr._name)))
    content.add(ListItem().join(Strong("value:"), Text(str(value))))
    content.add(ListItem().join(Strong("path:"), Text(path)))
    content.add(ListItem().join(Strong("container:"), Text(prefix)))
    if isinstance(descr, ChoiceOption):
        content.add(ListItem().join(Strong("possible values:"), Text(str(descr.values))))
    if not isinstance(descr, SymLinkOption):
        content.add(ListItem().join(Strong("type:"), Text(str(descr.opt_type))))
        content.add(ListItem().join(Strong("default:"), Text(str(descr.getdefault()))))
        content.add(ListItem().join(Strong("description:"), Text(str(descr.getdoc()))))
        content.add(ListItem().join(Strong("requirements:"), Text(str(descr._requires))))
        content.add(ListItem().join(Strong("is hidden:"), Text(str(descr._is_hidden()))))
        content.add(ListItem().join(Strong("is disabled:"), Text(str(descr._is_disabled()))))
        content.add(ListItem().join(Strong("is frozen:"), Text(str(descr._frozen))))
        content.add(ListItem().join(Strong("is multi:"), Text(str(descr.multi))))
        content.add(ListItem().join(Strong("is mandatory:"), Text(str(descr.is_mandatory()))))
    else:
        content.add(ListItem().join(Strong("links to:"), Text(str(descr.path))))
    content.add(Transition())
    content.add(Paragraph(Link("back to container", join(prefix + ".html"))))
    make_rst_file(join(docdir, path + '.txt'), content)
        
def make_rest_overview(cfg, title=True):
    rootname = cfg._cfgimpl_descr._name
    descr_content(rootname, rootname, cfg._cfgimpl_descr, root=True)
    #cfg.cfgimpl_read_write()
    cfg._cfgimpl_disabled = False
    cfg._cfgimpl_hidden = False
    for path in cfg.getpaths(include_groups=True, allpaths=True):
        child = cfg.unwrap_from_path(path)
        fullpath = rootname + '.' + path 
        prefix = fullpath.rsplit(".", 1)[0]
        if isinstance(child, OptionDescription):
            descr_content(fullpath, prefix, child)
        else: 
            value = getattr(cfg, path)
            opt_rst_content(fullpath, prefix, child, value)

if __name__ == '__main__':
    from test_config_big_example import get_example_config
    make_rest_overview(get_example_config())
# ____________________________________________________________