import logging
import json
from requests import ConnectionError
from django import forms
from django.conf import settings
from django.core.mail import EmailMessage
from django.core.exceptions import ValidationError
from usps import USPSApi, Address

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):
    full_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()

    def process_full_name(self, full_name):
        name = full_name.split()

        if len(name) > 2:
            last_name = ''.join(name.pop(-1))
            first_name = ' '.join(name)
        elif len(name) > 1:
            first_name = name[0]
            last_name = name[1]
        else:
            first_name = name[0]
            last_name = ''

        return first_name, last_name

    def clean(self):
        cleaned_data = super().clean()
        address = Address(
            name=cleaned_data.get('full_name'),
            address_1=cleaned_data.get('street_address_1'),
            address_2=cleaned_data.get('street_address_2'),
            city=cleaned_data.get('city'),
            state=cleaned_data.get('state'),
            zipcode=cleaned_data.get('postal_code')
        )
        usps = USPSApi(settings.USPS_USER_ID, test=True)

        try:
            validation = usps.validate_address(address)
        except ConnectionError:
            raise ValidationError(
                'Could not connect to USPS, try again.'
            )

        if 'Error' in validation.result['AddressValidateResponse']['Address']:
            error = validation.result['AddressValidateResponse']['Address']['Error']['Description']
            raise ValidationError(
                "USPS: " + error
            )

        try:
            cleaned_data['postal_code'] = validation.result['AddressValidateResponse']['Address']['Zip5']
        except KeyError:
            raise ValidationError(
                'Could not find Zip5'
            )


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)'),
    ]

    full_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)
