from flask.ext.wtf import Form
from wtforms import IntegerField, StringField, FieldList
from wtforms.validators import DataRequired, Email, ValidationError, NumberRange, Length, Optional


def word_length(limit=None, message=None):
    message = message or 'Must not be more than %d words'
    message = message % limit

    def _length(form, field):
        if not field.data or not limit:
            return field

        if len(field.data.split()) > limit:
            raise ValidationError(message)

    return _length


class EditSupplierForm(Form):
    description = StringField('Supplier summary', validators=[
        word_length(50, 'Your summary must not be more than %d words')
    ])
    clients = FieldList(StringField())

    def validate_clients(form, field):
        if len(field.data) > 10:
            raise ValidationError('You must have 10 or fewer clients')


class EditContactInformationForm(Form):
    id = IntegerField()
    address1 = StringField('Business address')
    address2 = StringField('Business address')
    city = StringField('Town or city')
    country = StringField()
    postcode = StringField()
    website = StringField()
    phoneNumber = StringField('Phone number')
    email = StringField('Email address', validators=[
        DataRequired(message="Email can not be empty"),
        Email(message="Please enter a valid email address")
    ])
    contactName = StringField('Contact name', validators=[
        DataRequired(message="Contact name can not be empty"),
    ])


class DunsNumberForm(Form):
    duns_number = IntegerField('DUNS Number', validators=[
        DataRequired(message="You must enter a DUNS number with 9 digits."),
        NumberRange(min=100000000, max=999999999, message="You must enter a DUNS number with 9 digits.")
    ])


class CompaniesHouseNumberForm(Form):
    companies_house_number = StringField('Companies house number', validators=[
        Optional(),
        Length(min=8, max=8, message="You must enter a Companies House number with 8 characters.")
    ])


class CompanyNameForm(Form):
    company_name = StringField('Company name', validators=[
        DataRequired(message="You must provide a company name."),
        Length(max=255, message="You must provide a company name under 256 characters.")
    ])


class CompanyContactDetailsForm(Form):
    contact_name = StringField('Contact name', validators=[
        DataRequired(message="Contact name can not be empty"),
        Length(max=255, message="Contact name must be under 256 characters")
    ])
    email_address = StringField('Email address', validators=[
        DataRequired(message="Email can not be empty"),
        Email(message="Please enter a valid email address")
    ])
    phone_number = StringField('Phone number', validators=[
        DataRequired(message="Phone number can not be empty"),
        Length(max=20, message="Phone number must be under 20 characters")
    ])


class EmailAddressForm(Form):
    email_address = StringField('Email address', validators=[
        DataRequired(message="Email can not be empty"),
        Email(message="Please enter a valid email address")
    ])

@@ -2421,37 +2421,40 @@
 ge=%22
-Contact name can not be empty
+You must provide a contact name.
 %22),%0A
@@ -2478,33 +2478,52 @@
 x=255, message=%22
-C
+You must provide a c
 ontact name must
@@ -2518,24 +2518,16 @@
 ct name 
-must be 
 under 25
@@ -2530,32 +2530,33 @@
 r 256 characters
+.
 %22)%0A    %5D)%0A    em
@@ -2641,38 +2641,49 @@
 essage=%22
-Email can not be empty
+You must provide a email address.
 %22),%0A    
@@ -2697,36 +2697,40 @@
 il(message=%22
-Please enter
+You must provide
  a valid ema
@@ -2731,32 +2731,33 @@
 id email address
+.
 %22)%0A    %5D)%0A    ph
@@ -2844,37 +2844,40 @@
 ge=%22
-Phone number can not be empty
+You must provide a phone number.
 %22),%0A
@@ -2908,17 +2908,36 @@
 essage=%22
-P
+You must provide a p
 hone num
@@ -2944,16 +2944,8 @@
 ber 
-must be 
 unde
@@ -2959,16 +2959,17 @@
 aracters
+.
 %22)%0A    %5D

Change validation messages for company contact