# w.c.s. - web application for online forms
# Copyright (C) 2005-2018  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/>.

import sys

from quixote import get_publisher
from django.template import Context, Template, TemplateSyntaxError
from django.utils.encoding import force_text

from .qommon import _, get_logger, force_str


class ValidationError(ValueError):
    pass


class Condition(object):
    record_errors = True
    log_errors = False

    def __init__(self, condition, context={}):
        if not condition:
            condition = {}
        self.type = condition.get('type')
        self.value = condition.get('value')
        self.context = context

    def __repr__(self):
        return '<%s (%s) %r>' % (self.__class__.__name__, self.type, self.value)

    def get_data(self):
        return get_publisher().substitutions.get_context_variables(
                mode='%s-condition' % self.type)

    def unsafe_evaluate(self):
        if not self.type or not self.value:
            return True
        local_variables = self.get_data()
        return getattr(self, 'evaluate_' + self.type)(local_variables)

    def evaluate(self):
        try:
            return self.unsafe_evaluate()
        except Exception as e:
            if self.log_errors:
                get_logger().warn('failed to evaluate %r (%r)', self, e)
            if self.record_errors:
                from wcs.logged_errors import LoggedError
                summary = _('Failed to evaluate condition')
                LoggedError.record(summary,
                                   formdata=self.context.get('formdata'),
                                   status_item=self.context.get('status_item'),
                                   expression=self.value, expression_type=self.type,
                                   exception=e)
            raise RuntimeError()

    def evaluate_python(self, local_variables):
        global_variables = get_publisher().get_global_eval_dict()
        return eval(self.value, global_variables, local_variables)

    def evaluate_django(self, local_variables):
        template = Template('{%% load %s %%}{%% if %s %%}OK{%% endif %%}' % (
            get_publisher().get_default_templatetags_libraries(), self.value))
        context = Context(local_variables)
        return template.render(context) == 'OK'

    def validate(self):
        if not self.type or not self.value:
            return
        if not hasattr(self, 'validate_' + self.type):
            raise ValidationError(_('unknown condition type'))
        return getattr(self, 'validate_' + self.type)()

    def validate_python(self):
        if '{{' in self.value:
            raise ValidationError(_('invalid usage, Python condition cannot contain {{'))
        try:
            compile(self.value, '<string>', 'eval')
        except (SyntaxError, TypeError) as e:
            raise ValidationError(_('syntax error: %s') % force_str(force_text(e)))

    def validate_django(self):
        try:
            Template('{%% load %s %%}{%% if %s %%}OK{%% endif %%}' % (
                get_publisher().get_default_templatetags_libraries(), self.value))
        except TemplateSyntaxError as e:
            raise ValidationError(_('syntax error: %s') % force_str(force_text(e)))
