from django.db import models
from app.models import ItfModel
from django.contrib.auth.models import User
from datetime import datetime
from django.contrib.localflavor.in_.forms import INZipCodeField
#from ox.django.fields import DictField
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from itfprofiles.models import Location, Production, TheatreGroup, Person



class Event(ItfModel):
    #ItfModel stuff:
    form_names = ['EventForm']
    main_form = 'EventForm'
    fts_fields = ['title', 'oneliner', 'description']

    title = models.CharField(max_length=1024)
    oneliner = models.CharField(max_length=1024,help_text="Event slogan/one liner")
    description = models.TextField(blank=True, null=True)
    locations = generic.GenericRelation(Location) 
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    booking_link = models.URLField(blank=True, null=True, help_text="Ticket booking")
    tel_no = models.CharField(max_length=100, blank=True)
    production = models.ForeignKey(Production, blank=True, null=True, help_text="If this is a showing of a production...")
    people = models.ManyToManyField(Person, blank=True, null=True, through="PersonEvent")  
    image = models.ImageField(upload_to='images/', blank=True, null=True)
    groups = models.ManyToManyField(TheatreGroup, blank=True, null=True, through="GroupEvent")
    
#Entities and Connections - 
#    connections = generic.GenericRelation("Connection")


    def __unicode__(self):
        return "%s" % (self.title,)
        
    def get_title(self):
        return self.__unicode__()

    def get_dict(self):
#        links = self.connections.all().order_by('link_type')
#        link_keys =   self.connections.all().values_list['link_type'].distinct()

#        conn = dict()
#        for key in link_keys:
#            conn[key]= [obj for obj in links if obj.type==key]
        #add conn keys to main dict 
        return {
            'title': self.title,
            'oneliner': self.oneliner,
            'description': self.description
        }


PERSON_EVENT_CHOICES = (
    ('attendee', 'Attendee'),
    ('organiser', 'Organiser'),
    ('performer', 'Performer'),
)

class PersonEvent(models.Model):
    person = models.ForeignKey(Person)
    event = models.ForeignKey(Event)
    typ = models.CharField(choices=PERSON_EVENT_CHOICES, max_length=128)


GROUP_EVENT_CHOICES = (
    ('venue', 'Venue'),
    ('performing', 'Performing'),
)

class GroupEvent(models.Model):
    group = models.ForeignKey(TheatreGroup)
    event = models.ForeignKey(Event)
    typ = models.CharField(choices=GROUP_EVENT_CHOICES, max_length=128)


class Connection(models.Model):
    link_type = models.CharField(max_length=255)
    extra_text = models.CharField(max_length=1024)   
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    def __unicode__(self):
        return self.link_type

