# -*- coding: utf-8 -*-
# copyright 2014 Logilab (Paris, FRANCE), all rights reserved.

"""cubicweb-fevis views/tables for web ui"""

from cubicweb.web.views.startup import IndexView
from cubes.uitest.views import utils

_ = unicode

class StaticTablesStartupView(IndexView):
    """demo Forms view"""
    __regid__ = 'uitest.static_tables'
    title = _('Static tables')

    def call(self, **kwargs):
        self.w(u'<h1>%s</h1>' % self._cw._('Static tables'))
        self.static_table()

    def static_table(self):
        thead = '''<thead><th><label>
        <input class="ace" type="checkbox">
        <span class="lbl"></span>
        </label></th>
        <th>Language</th><th>Text</th><th>Date</th></thead>'''
        tbody = []
        for key, value in utils.LOREM.iteritems():
            tbody.append(u'''<tr><td><input class="ace" type="checkbox">
            <span class="lbl"></span></td>
            <td>%s</td><td>%s</td><td></td></tr>''' % (key, value))
        tbody = '''<tbody>%s</tbody>''' % u'\n'.join(tbody)
        html = ['<table class="table table-striped table-bordered">', thead, tbody, '</table>']
        self.w(utils.simple_box('static table', 'table', u'\n'.join(html)))

class DynamicTablesStartupView(IndexView):
    """demo Forms view"""
    __regid__ = 'uitest.dynamic_tables'
    title = _('Dynamic tables')

    def call(self, **kwargs):
        _ = self._cw._
        self.w(u'<h1>%s</h1>' % _('Dynamic tables'))
        self.w(u'<p>%s</p>' % '''The table below is generated by
          the basic __regid__='table' view
          applyed on "Any X WHERE X is CWEType" rql. Click on entities links to see other tables.''')
        self.w(u'''<code><ol class="codeline"><li>rset = self._cw.execute('Any X, N, D, T  WHERE X is CWEType, X name N, X description D, X creation_date T')</li>
                 <li>self._cw.wview('table', rset)</li></ol>
        </code>''')

        self.dynamic_table()

    def static_table(self):
        thead = '''<thead><th><label>
        <input class="ace" type="checkbox">
        <span class="lbl"></span>
        </label></th>
        <th>Language</th><th>Text</th><th>Date</th></thead>'''
        tbody = []
        for key, value in utils.LOREM.iteritems():
            tbody.append(u'''<tr><td><input class="ace" type="checkbox">
            <span class="lbl"></span></td>
            <td>%s</td><td>%s</td><td></td></tr>''' % (key, value))
        tbody = '''<tbody>%s</tbody>''' % u'\n'.join(tbody)
        html = ['<table class="table table-striped table-bordered">', thead, tbody, '</table>']
        self.w(utils.simple_box('static table', 'table', u'\n'.join(html)))

    def dynamic_table(self):
        rset = self._cw.execute('Any X, N, D, T  WHERE X is CWEType, X name N, X description D, X creation_date T')
        html = []
        w = html.append
        self._cw.view('table', rset, w=w)
        self.w(utils.simple_box('dynamic table', 'table', u'\n'.join(html)))
