# w.c.s. - web application for online forms
# Copyright (C) 2005-2015  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from quixote import redirect
from quixote.directory import Directory
from quixote.html import TemplateIO, htmltext

from qommon import _
from qommon.form import *
from qommon.backoffice.menu import html_top
from wcs.data_sources import (NamedDataSource, DataSourceSelectionWidget,
        get_structured_items)

class NamedDataSourceUI(object):
    def __init__(self, datasource):
        self.datasource = datasource
        if self.datasource is None:
            self.datasource = NamedDataSource()

    def get_form(self):
        form = Form(enctype='multipart/form-data',
                advanced_label=_('Additional options'))
        form.add(StringWidget, 'name', title=_('Name'), required=True, size=30,
                value=self.datasource.name)
        form.add(TextWidget, 'description', title=_('Description'),
                cols=40, rows=5,
                value=self.datasource.description)
        form.add(DataSourceSelectionWidget, 'data_source',
                value=self.datasource.data_source,
                title=_('Data Source'),
                allow_named_sources=False,
                required=True)
        if self.datasource.slug:
            form.add(StringWidget, 'slug',
                    value=self.datasource.slug,
                    title=_('Identifier'),
                    hint=_('Beware it is risky to change it'),
                    required=True, advanced=True,
                    )
        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        return form

    def submit_form(self, form):
        name = form.get_widget('name').parse()
        if self.datasource.slug:
            slug = form.get_widget('slug').parse()
        else:
            slug = None

        for nds in NamedDataSource.select():
            if nds.id == self.datasource.id:
                continue
            if name == nds.name:
                form.get_widget('name').set_error(_('This name is already used.'))
            if slug == nds.slug:
                form.get_widget('slug').set_error(_('This value is already used.'))
        if form.has_errors():
            raise ValueError()

        self.datasource.name = name
        self.datasource.description = form.get_widget('description').parse()
        self.datasource.data_source = form.get_widget('data_source')
        if self.datasource.slug:
            self.datasource.slug = slug
        self.datasource.store()


class NamedDataSourcePage(Directory):
    _q_exports = ['', 'edit', 'delete']

    def __init__(self, component):
        self.datasource = NamedDataSource.get(component)
        self.datasource_ui = NamedDataSourceUI(self.datasource)
        get_response().breadcrumb.append((component + '/', self.datasource.name))

    def _q_index(self):
        html_top('datasources', title=self.datasource.name)
        r = TemplateIO(html=True)
        get_response().filter['sidebar'] = self.get_sidebar()

        r += htmltext('<h2>%s - ') % _('Data Source')
        r += self.datasource.name
        r += htmltext('</h2>')

        if self.datasource.description:
            r += htmltext('<div class="bo-block">')
            r += self.datasource.description
            r += htmltext('</div>')

        data_source_labels = {
            'json': _('JSON'),
            'jsonp': _('JSONP'),
            'formula': _('Python Expression'),
        }

        if self.datasource.data_source and self.datasource.data_source.get('type') in data_source_labels:
            data_source_type = self.datasource.data_source.get('type')
            r += htmltext('<h3>%s</h3>') % _('Configuration')
            r += htmltext('<ul>')
            r += htmltext('<li>%s</li>') % _(
                    'Type of source: %s') % data_source_labels.get(data_source_type)
            if data_source_type in ('json', 'jsonp'):
                r += htmltext('<li>%s<a href="%s">%s</a></li>') % (
                        _('URL: '),
                        self.datasource.data_source.get('value'),
                        self.datasource.data_source.get('value'))
            elif data_source_type == 'formula':
                r += htmltext('<li>%s<tt>%s</tt></li>') % (
                        _('Python Expression: '),
                    self.datasource.data_source.get('value'))
            r += htmltext('</ul>')

            if data_source_type in ('json', 'formula'):
                items = get_structured_items(self.datasource.data_source)
                if items:
                    r += htmltext('<h3>%s</h3>') % _('Preview (first items only)')
                    r += htmltext('<div class="bo-block data-source-preview"><ul>')
                    r += htmltext('<ul>')
                    additional_keys = set()
                    for item in items[:10]:
                        if not isinstance(item.get('text'), str):
                            r += htmltext('<li><tt>%s</tt>: <i>%s (%r)</i></li>') % (
                                    item.get('id'),
                                    _('error: not a string'),
                                    item.get('text'))
                        else:
                            r += htmltext('<li><tt>%s</tt>: %s</li>') % (
                                    item.get('id'), item.get('text'))
                            additional_keys |= set(item.keys())
                    if len(items) > 10:
                        r += htmltext('<li>...</li>')
                    r += htmltext('</ul>')
                    additional_keys -= set(['id', 'text'])
                    if additional_keys:
                        r += htmltext('<p>%s %s</p>') % (_('Additional keys are available:'),
                                ', '.join(sorted(additional_keys)))
                    r += htmltext('</div>')
        else:
            # the data source field is required so this should never happen
            r += htmltext('<p class="infonotice">%s</p>') % _('Not configured')

        return r.getvalue()

    def get_sidebar(self):
        r = TemplateIO(html=True)
        r += htmltext('<ul id="sidebar-actions">')
        r += htmltext('<li><a href="edit">%s</a></li>') % _('Edit')
        r += htmltext('<li><a href="delete" rel="popup">%s</a></li>') % _('Delete')
        r += htmltext('</ul>')
        return r.getvalue()

    def edit(self):
        form = self.datasource_ui.get_form()
        if form.get_submit() == 'cancel':
            return redirect('.')

        if form.get_submit() == 'submit' and not form.has_errors():
            try:
                self.datasource_ui.submit_form(form)
            except ValueError:
                pass
            else:
                return redirect('.')

        get_response().breadcrumb.append( ('edit', _('Edit')) )
        html_top('datasources', title = _('Edit Data Source'))
        r = TemplateIO(html=True)
        r += htmltext('<h2>%s</h2>') % _('Edit Data Source')
        r += form.render()
        return r.getvalue()

    def delete(self):
        form = Form(enctype='multipart/form-data')
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'You are about to irrevocably delete this data source.')))
        form.add_submit('delete', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_widget('cancel').parse():
            return redirect('..')
        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('delete', _('Delete')))
            html_top('datasources', title = _('Delete Data Source'))
            r = TemplateIO(html=True)
            r += htmltext('<h2>%s %s</h2>') % (_('Deleting Data Source:'), self.datasource.name)
            r += form.render()
            return r.getvalue()
        else:
            self.datasource.remove_self()
            return redirect('..')


class NamedDataSourcesDirectory(Directory):
    _q_exports = ['', 'new']

    def _q_traverse(self, path):
        get_response().breadcrumb.append( ('data-sources/', _('Data Sources')) )
        return super(NamedDataSourcesDirectory, self)._q_traverse(path)

    def _q_index(self):
        get_response().add_javascript(['jquery.js', 'jquery-ui.js', 'biglist.js',
            'ckeditor/ckeditor.js', 'qommon.wysiwyg.js', 'ckeditor/adapters/jquery.js'])
        get_response().filter['sidebar'] = self.get_sidebar()
        html_top('datasources', title = _('Data Sources'))
        r = TemplateIO(html=True)

        r += htmltext('<div class="bo-block">')
        r += htmltext('<h2>%s</h2>') % _('Data Sources')
        r += htmltext('<ul class="biglist" id="datasource-list">')
        datasources = NamedDataSource.select(order_by='name')
        for datasource in datasources:
            r += htmltext('<li class="biglistitem" id="itemId_%s">') % datasource.id
            r += htmltext('<strong class="label"><a href="%s/">%s (%s)</a></strong>') % (
                            datasource.id, datasource.name, datasource.slug)
            r += htmltext('<p class="details">')
            r += htmltext('</p></li>')
        r += htmltext('</ul>')
        r += htmltext('</div>')
        return r.getvalue()

    def get_sidebar(self):
        r = TemplateIO(html=True)
        r += htmltext("""<ul id="sidebar-actions">
          <li><a class="new-item" href="new">%s</a></li>
        </ul>""") % _('New Data Source')

        return r.getvalue()

    def new(self):
        get_response().breadcrumb.append( ('new', _('New')) )
        datasource_ui = NamedDataSourceUI(None)
        form = datasource_ui.get_form()
        if form.get_widget('cancel').parse():
            return redirect('.')

        if form.get_submit() == 'submit' and not form.has_errors():
            try:
                datasource_ui.submit_form(form)
            except ValueError:
                pass
            else:
                return redirect('.')

        html_top('datasources', title = _('New Data Source'))
        r = TemplateIO(html=True)
        r += htmltext('<h2>%s</h2>') % _('New Data Source')
        r += form.render()
        return r.getvalue()

    def _q_lookup(self, component):
        return NamedDataSourcePage(component)
