import floppyforms as forms
from registration.forms import RegistrationForm
from models import *
from registration.models import RegistrationProfile
import json

from padmavideos.models import PadmaVideo, PadmaClip
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django.contrib.contenttypes.models import ContentType
from app.forms import *

#Forms and Inlines for Generic Classes

class LocationForm(ItfForm):

    class Meta:
        model = Location
        exclude = ('lat', 'lon',)

LocationsInline = itf_inlineformset_factory(Location, form=LocationForm, title="Locations", is_generic=True)


class BuzzItemsForm(ItfForm):

    class Meta:
        model = BuzzItem
        
BuzzItemsInline = itf_inlineformset_factory(BuzzItem, form=BuzzItemsForm, title="Buzz Items", is_generic=True)


class AwardsForm(ItfForm):

    class Meta:
        model = Award

AwardsInline = itf_inlineformset_factory(Award, form=AwardsForm, title="Awards", is_generic=True)


class PadmaClipForm(ItfForm):
    pass

    class Meta:
        model = PadmaClip

PadmaClipsInline = itf_inlineformset_factory(PadmaClip, extra=1, is_generic=True, form=PadmaClipForm, title="Videos")




#Popup Form Classes:
class PopupPersonForm(PopupForm):        

    class Meta:
        model = Person
        fields = ('first_name', 'last_name', 'email',)


class PopupGroupForm(PopupForm):

    class Meta:
        model = TheatreGroup
        fields = ('name', 'email',)


class PopupProductionForm(PopupForm):
    group = forms.ModelChoiceField(TheatreGroup.objects.all(), widget=AutocompleteAddWidget(model_class=TheatreGroup))

    class Meta:
        model = Production
        fields = ('name', 'group',)    


#Inline form definitions and inlines for Person form
class ConnectionsForm(ItfForm):
    #ctype = ContentType.objects.get_for_model(Person)
    person2 = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person))
    
    class Meta:
        model = PersonPerson
        exclude = ('disapproved',)

class PersonProductionForm(ItfForm):
    production = forms.ModelChoiceField(Production.objects.all(), widget=AutocompleteAddWidget(model_class=Production))
    
    class Meta:
        model = PersonProduction

class PersonGroupForm(ItfForm):
    group = forms.ModelChoiceField(TheatreGroup.objects.all(), widget=AutocompleteAddWidget(model_class=TheatreGroup))

    class Meta:
        model = PersonGroup

ConnectionsInline = itf_inlineformset_factory(Person, PersonPerson, fk_name='person1', form=ConnectionsForm, extra=1, title="Add / Edit Connections", help_text="select the people you are connected with and how you are related in the theatre world")
ProductionsInline = itf_inlineformset_factory(Person, PersonProduction, form=PersonProductionForm, extra=1, title="Productions you have worked in")
GroupsInline = itf_inlineformset_factory(Person, PersonGroup, extra=1, form=PersonGroupForm, title="Theatre groups you are a part of or have worked with")


#Actual person form definition
class PersonForm(ItfForm):
#    occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
#    connections = forms.ModelMultipleChoiceField(Person.objects.all(), widget=ConnectionsWidget())
#    occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
    email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'john@example.com'}))
#    inlines = [ConnectionsInline]   
    inlines = [ConnectionsInline, ProductionsInline, GroupsInline, BuzzItemsInline, AwardsInline, PadmaClipsInline]

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.add_input(Submit('submit', 'Submit'))
        super(PersonForm, self).__init__(*args, **kwargs)


    class Meta:
        exclude = ('connections', 'productions', 'groups', 'last_accessed', 'occupations',)
        model = Person
        widgets = {
            'first_name': forms.TextInput,
            'last_name': forms.TextInput,
#            'occupations': forms.CheckboxSelectMultiple
        }
        #exclude = ('connections', 'productions', 'occupations', 'groups',)


#Inline definitions and form for TheatreGroup      
class GroupPersonForm(ItfForm):
    person = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person)) 

    class Meta:
        model = PersonGroup

PersonsInline = itf_inlineformset_factory(TheatreGroup, PersonGroup, form=GroupPersonForm, title="People in the Group")

class TheatreGroupForm(ItfForm):
    
    inlines = [PersonsInline, BuzzItemsInline, AwardsInline, LocationsInline]

    class Meta:
        model = TheatreGroup
        exclude = ('added_by', 'locations',)



#Inline definitions and form for Production

class ProductionForm(ItfForm):
    inlines = []
    director = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person))     
    playwright = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person)) 
    group = forms.ModelChoiceField(TheatreGroup.objects.all(), widget=AutocompleteAddWidget(model_class=TheatreGroup))
    class Meta:
        model = Production
        exclude = ('added_by',)


#Registration forms / not connected to Itf Profiles
class ItfAllAuthRegForm(forms.Form):
    firstname=forms.CharField()
    lastname=forms.CharField()
    
    def save(self, user):
        first_name = self.cleaned_data['firstname']
        last_name = self.cleaned_data['lastname']
        p = Person(user=user, first_name=first_name, last_name=last_name, email=user.email)
        p.save()
        return self


class ItfRegistrationForm(RegistrationForm):
    subscribe = forms.BooleanField(help_text='Subscribe to newsletter?')

    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email'])
        new_profile = ItfProfile(user=new_user, subscribed=self.cleaned_data['subscribe'])
        new_profile.save()
        return new_user

