# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ('WOComponentTempl',)

from os import path

from ulib.base.uio import _s, _u
from ulib.base.encoding import UTF8
from ulib.base.lines import Lines
from ulib.base.input import is_yes
from ulib.base.output import etitle, eend
from base import Templ, FileTempl, register_templates
from javatempl import JavaTempl

def doctype_parser(value):
    if value: return u' doctype="%s"' % _u(value).strip()
    else: return u""

class WOSrcTempl(FileTempl):
    TEMPLATE = ('component wosrc file',)

    VPS, ACPS, NCPS, ICPS = FileTempl.PS('html', 'doctype',
                                         'titlehl', 'head_title',
                                         ncps=dict(title=None),
                                         icps=dict(html=is_yes, doctype=doctype_parser))

    html = True
    doctype = None
    titlehl = 'h1'
    head_title = None
    title = None

    def fix_properties(self):
        FileTempl.fix_properties(self)
        self.doctype = self.doctype or u""
        if not self.doctype and self.html:
            self.doctype = doctype_parser(u"html4")
        self.title = self.title or u""
        if self.head_title is None:
            self.head_title = self.title

    DECL = u"""<?sox encoding="%(encoding)s"%(doctype)s namespaces="wo,wo.qattrs" modeline="-*- coding: %(encoding)s -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=%(encoding)s"?>"""
    HTML = u"""\
html>
    head>
        title> "%(head_title)s"
    body>
        %(titlehl)s> "%(title)s"\
"""

    def get_lines(self):
        lines = Lines()
        self.appendtext(lines, self.DECL)
        if self.html: self.appendtext(lines, self.HTML)
        return lines

class WOApiTempl(FileTempl):
    TEMPLATE = ('component api file',)

    VPS, ACPS, NCPS, ICPS = FileTempl.PS('-encoding')

    def fix_properties(self):
        FileTempl.fix_properties(self)
        self.encoding = UTF8

    API = u"""\
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<wodefinitions>
    <wo wocomponentcontent="false" class="%(basename)s">
    </wo>
</wodefinitions>"""

    def get_lines(self):
        return self.appendtext(None, self.API)

class WOJavaTempl(JavaTempl):
    TEMPLATE = ('component java file',)

class WOComponentTempl(Templ):
    TEMPLATE = ('webobjects component', 'wocomponent', 'wosrc')
    EXT = '.wosrc'

    VPS, ACPS, NCPS, ICPS = Templ.PS('html', 'doctype',
                                     ncps=dict(title=None),
                                     icps=dict(html=is_yes, doctype=doctype_parser))

    html = True
    doctype = None
    title = None

    def create(self):
        self._check_file()

        dir, basename, ext = self.dir, self.basename, self.ext
        if ext in ('.wosrc', '.api', '.java', ''):
            file = path.join(dir, basename)
        else:
            raise ValueError("destination name should not have an extension")

        etitle(self.file)
        try:
            wosrc = WOSrcTempl(file + ".wosrc",
                       **self.get_properties('nl', 'overwrite', 'encoding', 'html', 'doctype', 'title')
                       )
            wosrc.create()

            api = WOApiTempl(file + ".api", **self.get_properties('nl', 'overwrite'))
            api.create()

            java = WOJavaTempl(file + ".java", **self.get_properties('nl', 'overwrite', 'encoding'))
            java.set_imports('com.webobjects.appserver.WOComponent',
                             'com.webobjects.appserver.WOContext')
            java.set_extends('WOComponent')
            java.body = u"""\
    private static final long serialVersionUID = 1L;

    public %(basename)s(WOContext context) {
        super(context);
    }"""
            java.create()
        finally:
            eend()

register_templates(WOSrcTempl, WOApiTempl, WOJavaTempl, WOComponentTempl)
