import django.newforms as forms
from personasbackend.personas import models

class BasePersonaForm( forms.ModelForm ):
    """
    Form given to normal users who don't have the permission to
    publish Personas.
    """

    class Meta:
        model = models.Persona
        exclude = ["owner", "date_published", "popularity"]

    def _color_cleaner( self, field ):
        models.ensure_color_is_valid(
            self.cleaned_data[field],
            error_class = forms.ValidationError
            )
        return self.cleaned_data[field]

    def clean_text_color( self ):
        return self._color_cleaner( "text_color" )

    def clean_accent_color( self ):
        return self._color_cleaner( "accent_color" )

    def clean( self ):
        hasHeader = False
        hasFooter = False

        if self.instance:
            hasHeader = self.instance.header_img
            hasFooter = self.instance.footer_img

        if (not self.files.has_key("header_img")) and (not hasHeader):
            raise forms.ValidationError( "You must upload a header image." )
        elif (not self.files.has_key("footer_img")) and (not hasFooter):
            raise forms.ValidationError( "You must upload a footer image." )

        return self.cleaned_data

class AdminPersonaForm( BasePersonaForm ):
    pass

DEVELOPER_AGREEMENT = """
          <p>By uploading your Persona to this site, you agree that
          the following are true:</p>
          <ul>
            <li>you have the right to distribute this Persona,
            including any rights required for material that may be
            trademarked or copyrighted by someone else; and</li>
            <li>if any information about the user or usage of this
            Persona is collected or transmitted outside of the user's
            computer, the details of this collection will be provided
            in the description of the software, and you will provide a
            link to a privacy policy detailing how the information is
            managed and protected; and</li>
            <li>your Persona may be removed from the site,
            re-categorized, have its description or other information
            changed, or otherwise have its listing changed or removed,
            at the sole discretion of Mozilla and its authorized
            agents; and</li>
            <li>the descriptions and other data you provide about the
            Persona are true to the best of your knowledge.</li>
          </ul>
"""

class UserPersonaForm( BasePersonaForm ):
    class Meta:
        model = BasePersonaForm.Meta.model
        exclude = BasePersonaForm.Meta.exclude + ["status"]

    agree_to_terms = forms.BooleanField(
        label = "I have read and accept the developer agreement.",
        # TODO: setting required to True doesn't seem to have any effect,
        # but the Django docs say it should ensure that the checkbox
        # is filled out.
        required = True,
        help_text = DEVELOPER_AGREEMENT
        )

    def clean( self ):
        super( UserPersonaForm, self ).clean()
        if not self.cleaned_data["agree_to_terms"]:
            raise forms.ValidationError(
                "You must agree to the terms of service to "
                "submit your Persona."
                )

        return self.cleaned_data

class EditPersonaForm( UserPersonaForm ):
    pass

class NewPersonaForm( UserPersonaForm ):
    pass
