# 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 wcs.qommon import _
from wcs.qommon import errors, template
from wcs.qommon.form import *
from wcs.qommon.misc import json_response
from wcs.qommon.backoffice.menu import html_top
from wcs.data_sources import (NamedDataSource, DataSourceSelectionWidget,
        get_structured_items)
from wcs.formdef import FormDef, get_formdefs_of_all_kinds

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

    def is_used(self):
        for formdef in get_formdefs_of_all_kinds():
            if self.datasource.is_used_in_formdef(formdef):
                return True
        return False

    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)
        form.add(DurationWidget, 'cache_duration',
                value=self.datasource.cache_duration,
                title=_('Cache Duration'),
                hint=_('Caching data will improve performances but will keep changes '
                       'from being visible immediately.  You should keep this duration '
                       'reasonably short.'),
                required=False,
                advanced=False,
                attrs={
                    'data-dynamic-display-child-of': 'data_source$type',
                    'data-dynamic-display-value': 'json',
                })
        form.add(StringWidget, 'query_parameter',
                value=self.datasource.query_parameter,
                title=_('Query Parameter'),
                hint=_('Name of the parameter to use for querying source (typically, q)'),
                required=False,
                advanced=False,
                attrs={
                    'data-dynamic-display-child-of': 'data_source$type',
                    'data-dynamic-display-value': 'json',
                })
        form.add(StringWidget, 'id_parameter',
                value=self.datasource.id_parameter,
                title=_('Id Parameter'),
                hint=_('Name of the parameter to use to get a given entry from data source (typically, id)'),
                required=False,
                advanced=False,
                attrs={
                    'data-dynamic-display-child-of': 'data_source$type',
                    'data-dynamic-display-value': 'json',
                })
        if self.datasource.slug and not self.is_used():
            form.add(StringWidget, 'slug',
                    value=self.datasource.slug,
                    title=_('Identifier'),
                    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()
        slug_widget = form.get_widget('slug')
        if slug_widget:
            slug = form.get_widget('slug').parse()

        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_widget and slug == nds.slug:
                slug_widget.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')
        self.datasource.cache_duration = form.get_widget('cache_duration').parse()
        self.datasource.query_parameter = form.get_widget('query_parameter').parse()
        self.datasource.id_parameter = form.get_widget('id_parameter').parse()
        if slug_widget:
            self.datasource.slug = slug
        self.datasource.store()


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

    def __init__(self, component):
        try:
            self.datasource = NamedDataSource.get(component)
        except KeyError:
            raise errors.TraversalError()
        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)
        return template.QommonTemplateResponse(
                templates=['wcs/backoffice/data-sources.html'],
                context={'view': self, 'datasource': self.datasource})

    def usage_in_formdefs(self):
        formdefs = []
        for formdef in get_formdefs_of_all_kinds():
            if self.datasource.is_used_in_formdef(formdef):
                formdefs.append(formdef)
        formdefs.sort(key=lambda x: x.name.lower())
        return formdefs

    def preview_block(self):
        if self.datasource.data_source.get('type') not in ('json', 'formula'):
            return ''
        items = get_structured_items(self.datasource.data_source)
        if not items:
            return ''
        r = TemplateIO(html=True)
        r += htmltext('<h3>%s</h3>') % _('Preview (first items only)')
        r += htmltext('<div class="bo-block data-source-preview">')
        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>')
        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')
        if not self.datasource_ui.is_used():
            form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'You are about to irrevocably delete this data source.')))
            form.add_submit('delete', _('Submit'))
        else:
            form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'This datasource is still used, it cannot be deleted.')))
        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',
            'qommon.wysiwyg.js'])
        html_top('datasources', title = _('Data Sources'))
        r = TemplateIO(html=True)

        r += htmltext('<div id="appbar">')
        r += htmltext('<h2>%s</h2>') % _('Data Sources')
        r += htmltext('<span class="actions">')
        r += htmltext('<a class="new-item" href="new">%s</a>') % _('New Data Source')
        r += htmltext('</span>')
        r += htmltext('</div>')
        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('</li>')
        r += htmltext('</ul>')
        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)
