from django import forms
from django.utils.translation import gettext_lazy as _
from django_recaptcha.fields import ReCaptchaField

from ocyan.plugin.google.constants import CAPTCHA_ENABLED
from ocyan.plugin.google.recaptcha import RECAPTCHA_WIDGET

from .utils import validate_phone_number


class ContactForm(forms.Form):
    name = forms.CharField(label=_("Name"))
    phonenumber = forms.CharField(
        label=_("Phone number"), required=False, validators=[validate_phone_number]
    )
    email_from = forms.EmailField(label=_("Email address"))
    message = forms.CharField(widget=forms.Textarea, label=_("Message"))

    name.widget.attrs.update({"autocomplete": "name", "placeholder": _("Name")})
    phonenumber.widget.attrs.update(
        {"autocomplete": "tel", "type": "tel", "placeholder": _("Phone number")}
    )
    email_from.widget.attrs.update(
        {"autocomplete": "email", "placeholder": _("Email address")}
    )
    message.widget.attrs.update({"placeholder": _("Message")})

    if CAPTCHA_ENABLED:
        captcha = ReCaptchaField(widget=RECAPTCHA_WIDGET, label="")

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request", None)
        super().__init__(*args, **kwargs)
