# w.c.s. - web application for online forms
# Copyright (C) 2005-2011  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 contextlib import contextmanager

from quixote import get_publisher
from quixote.html import htmltext, TemplateIO


def invalidate_substitution_cache(func):
    def f(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        finally:
            get_publisher().substitutions.invalidate_cache()
    return f


class Substitutions(object):
    substitutions_dict = {}
    dynamic_sources = []
    sources = None

    _forced_mode = None

    def __init__(self):
        self.set_empty()

    def set_empty(self):
        from wcs.data_sources import NamedDataSource
        from wcs.wscalls import NamedWsCall
        from wcs.scripts import Script
        self.sources = [NamedDataSource, NamedWsCall, Script]

    @classmethod
    def register(cls, varname, category=None, comment=None):
        if varname in cls.substitutions_dict:
            return

        cls.substitutions_dict[varname] = {
            'category': category,
            'comment': comment
        }

    @classmethod
    def register_dynamic_source(cls, klass):
        if not cls.dynamic_sources:
            cls.dynamic_sources = []
        cls.dynamic_sources.append(klass)

    @invalidate_substitution_cache
    def reset(self):
        self.set_empty()

    def feed(self, source):
        if source is None:
            # silently ignore a None source, this is for example useful when
            # adding the current user, as it may be None if he is not logged
            # in.
            return
        if not source in self.sources:
            self.sources.append(source)
            self.invalidate_cache()

    def unfeed(self, predicate):
        self.sources = [x for x in self.sources if not predicate(x)]
        self.invalidate_cache()

    @contextmanager
    def freeze(self):
        orig_sources, self.sources = self.sources, self.sources[:]
        self.invalidate_cache()
        yield
        self.sources = orig_sources
        self.invalidate_cache()

    @contextmanager
    def temporary_feed(self, source, force_mode=None):
        if source is None or source in self.sources:
            yield
            return

        orig_sources, self.sources = self.sources, self.sources[:]
        self.sources.append(source)
        self.invalidate_cache()
        old_mode, self._forced_mode = self._forced_mode, force_mode
        yield
        self._forced_mode = old_mode
        self.sources = orig_sources
        self.invalidate_cache()

    def invalidate_cache(self):
        for value in (True, False):
            if hasattr(self, '_cache_context_variables%r' % value):
                delattr(self, '_cache_context_variables%r' % value)

    def get_context_variables(self, mode=None):
        if self._forced_mode:
            mode = self._forced_mode
        lazy = get_publisher().has_site_option('force-lazy-mode')
        if not lazy and mode:
            lazy = mode in get_publisher().get_lazy_variables_modes()
        d = getattr(self, '_cache_context_variables%r' % lazy, None)
        if d is not None:
            return d
        d = CompatibilityNamesDict()
        for source in self.sources:
            d.update(source.get_substitution_variables())
            if not lazy and hasattr(source, 'get_static_substitution_variables'):
                d.update(source.get_static_substitution_variables())
        setattr(self, '_cache_context_variables%r' % lazy, d)
        return d

    @classmethod
    def get_substitution_html_table(cls):
        from . import _
        r = TemplateIO(html=True)
        r += htmltext('<table id="substvars">')
        r += htmltext('<thead><tr><th>%s</th><th>%s</th><th>%s</th></tr></thead>' % (
                        _('Category'), _('Variable'), _('Comment')))
        r += htmltext('<tbody>')
        vars = [(_(y.get('category')), x, _(y.get('comment')))
                        for x, y in cls.substitutions_dict.items()]
        for dynamic_source in cls.dynamic_sources:
            vars.extend(dynamic_source.get_substitution_variables_list())
        vars.sort()
        for category, variable, comment in vars:
            r += htmltext('<tr><td>%s</td><td>%s</td><td>%s</td>' % (
                category, '{{ %s }}' % variable, comment))
        r += htmltext('</tbody>')
        r += htmltext('</table>')
        return r.getvalue()


class CompatibilityNamesDict(dict):
    # custom dictionary that provides automatic fallback to legacy variable
    # names (namespaced with underscores)
    def get(self, key, default=None):
        try:
            return self.__getitem__(key)
        except KeyError:
            return default

    def __getitem__(self, key):
        try:
            val = super(CompatibilityNamesDict, self).__getitem__(key)
        except KeyError:
            # fallback to deconstructing namespaces around underscores
            parts = key.split('_')
            current_dict = self
            while parts:
                for i in range(len(parts), 0, -1):
                    part = '_'.join(parts[:i])
                    try:
                        if current_dict is self:
                            current_dict = dict.__getitem__(current_dict, part)
                        elif hasattr(current_dict, '__getitem__'):
                            current_dict = current_dict[part]
                        else:
                            current_dict = getattr(current_dict, part)
                    except (AttributeError, KeyError, TypeError):
                        # TypeError will happen if indexing is used on a string
                        if i == 1:
                            raise KeyError(key)
                    else:
                        parts = parts[i:]
                        break
            return current_dict

        return val

    def __contains__(self, key):
        try:
            self.__getitem__(key)
        except KeyError:
            return False
        return True
