import logging
from django import forms
from django.core.mail import EmailMessage

from core.models import Order
from core import CoffeeGrind
from accounts import STATE_CHOICES

from .tasks import contact_form_email

logger = logging.getLogger(__name__)

class AddToCartForm(forms.Form):
    grind = forms.ChoiceField(choices=CoffeeGrind.GRIND_CHOICES)
    quantity = forms.IntegerField(min_value=1, max_value=20, initial=1)

class UpdateCartItemForm(forms.Form):
    quantity = forms.IntegerField(min_value=1, max_value=20, initial=1)
    update = forms.BooleanField(required=False, initial=True, widget=forms.HiddenInput)


class AddToSubscriptionForm(forms.Form):
    SEVEN_DAYS = 7
    FOURTEEN_DAYS = 14
    THIRTY_DAYS = 30
    SCHEDULE_CHOICES = [
        (SEVEN_DAYS, 'Every 7 days'),
        (FOURTEEN_DAYS, 'Every 14 days'),
        (THIRTY_DAYS, 'Every 30 days'),
    ]

    quantity = forms.IntegerField(min_value=1, initial=1)
    grind = forms.ChoiceField(choices=CoffeeGrind.GRIND_CHOICES)
    schedule = forms.ChoiceField(choices=SCHEDULE_CHOICES)


class AddressForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    email = forms.EmailField()
    street_address_1 = forms.CharField()
    street_address_2 = forms.CharField(required=False)
    city = forms.CharField()
    state = forms.ChoiceField(
        choices=STATE_CHOICES
    )
    postal_code = forms.CharField()

class OrderCreateForm(forms.ModelForm):
    email = forms.CharField(widget=forms.HiddenInput())
    first_name = forms.CharField(widget=forms.HiddenInput())
    last_name = forms.CharField(widget=forms.HiddenInput())

    class Meta:
        model = Order
        fields = (
            'total_net_amount',
            'shipping_total',
        )
        widgets = {
            'total_net_amount': forms.HiddenInput(),
            'shipping_total': forms.HiddenInput()
        }

class CouponApplyForm(forms.Form):
    code = forms.CharField(label='Coupon code')

class ContactForm(forms.Form):
    GOOGLE = 'Google Search'
    SHOP = 'The coffee shop'
    WOM = 'Word of mouth'
    PRODUCT = 'Coffee Bag'
    STORE = 'Store'
    OTHER = 'Other'

    REFERAL_CHOICES = [
        (GOOGLE, 'Google Search'),
        (SHOP, '"Better Living Through Coffee" coffee shop'),
        (WOM, 'Friend/Relative'),
        (PRODUCT, 'Our Coffee Bag'),
        (STORE, 'PT Food Coop/other store'),
        (OTHER, 'Other (please describe below)'),
    ]

    first_name = forms.CharField()
    last_name = forms.CharField()
    email_address = forms.EmailField()
    referal = forms.ChoiceField(
        label='How did you find our website?',
        choices=REFERAL_CHOICES
    )
    subject = forms.CharField()
    message = forms.CharField(widget=forms.Textarea)

    def send_email(self):
        contact_form_email.delay(self.cleaned_data)
