#-*- coding: utf-8 -*-
u"""

.. moduleauthor:: Martí Congost <marti.congost@whads.com>
"""
from cocktail.translations import translations
from cocktail import schema
from cocktail.schema.exceptions import ValidationError
from woost import app
from woost.models import Publishable

default_document = object()

def requires_agreement(form, name = "terms", document = default_document):

    if document is default_document:
        document = "%s.%s" % (app.package, name)

    if isinstance(document, basestring):
        document = Publishable.require_instance(qname = document)

    member = FormAgreement(
        name = name,
        agreement_document = document
    )

    form.schema.add_member(member)

    if form.schema.groups_order:
        if "form_agreement" not in form.schema.groups_order:
            if not isinstance(form.schema.groups_order, list):
                form.schema.groups_order = list(form.schema.groups_order)
            form.schema.groups_order.append("form_agreement")

    form.adapter.exclude(name)
    return member


class FormAgreement(schema.Boolean):

    agreement_document = None

    def __init__(self, *args, **kwargs):
        kwargs.setdefault("required", True)
        kwargs.setdefault("member_group", "form_agreement")
        schema.Boolean.__init__(self, *args, **kwargs)
        self.add_validation(FormAgreement.consent_validation)

    def consent_validation(self, value, context):
        if not value:
            yield ConsentNotGivenError(self, value, context)

    def __translate__(self, language, **kwargs):
        return (
            schema.Boolean.__translate__(self, language, **kwargs)
            or translations(
                "woost.controllers.formagreement.%s" % self.name,
                member = self
            )
            or translations("woost.controllers.formagreement", member = self)
        )

    def parse_request_value(self, reader, value):
        return schema.Boolean.parse_request_value(self, reader, value) or False


class ConsentNotGivenError(ValidationError):
    """A validation error produced by forms that enforce an agreement to a
    terms and conditions document when the user attempts to submit the form
    without acknowledging the agreement.
    """

