from django.db import models
from django.contrib.auth.models import User
import datetime
from django.contrib.localflavor.in_.forms import INZipCodeField
from ox.django.fields import DictField

GENDER_CHOICES = (
  ('M', 'Male'),
  ('F', 'Female'),
  ('O', 'Other'),
)

class Person(models.Model):

#Basic Info
  user = models.ForeignKey(User, blank=True, null=True, db_index=True)
  first_name = models.CharField(max_length=255)
  last_name = models.CharField(max_length=255)
  email = models.EmailField(blank=True, null=True, unique=True, db_index=True)
  tel_no = models.CharField(max_length=100, blank=True)
  about = models.TextField(blank=True, null=True)

#Occupation info
  occupations = models.ManyToManyField("Occupation", through='PersonOccupation', blank=True, null=True)
  is_practitioner = models.BooleanField(default=False)
  is_enthusiast = models.BooleanField(default=True)
  
#Personal info
  dob = models.DateField(null=True, verbose_name="Date of Birth")
  gender = models.CharField(max_length=255, choices=GENDER_CHOICES, blank=True)
  image = models.ImageField(upload_to='images/', blank=True, null=True)
  locations = models.ManyToManyField("Location", blank=True, null=True)  
  
#Groups and Connections
  is_freelancer = models.BooleanField(default=False)
  groups = models.ManyToManyField("TheatreGroup", blank=True, null=True, through='PersonGroup')
  connections = models.ManyToManyField('Person', blank=True, null=True, through='PersonPerson')
  productions = models.ManyToManyField("Production", blank=True, null=True, through='PersonProduction')
#  trainings = models.ManyToManyField("Training", blank=True, null=True)
  photos = models.ManyToManyField("PhotoAlbum", blank=True, null=True)
#  orphans = models.ManyToManyField("Orphan", blank=True, null=True, through='PersonOrphan')

  def __unicode__(self):
    return self.first_name + " " + self.last_name

  def is_orphan(self):
    return self.user == None
    
class PhotoAlbum(models.Model):
  name = models.CharField(max_length=512)
  description = models.TextField(blank=True)

  def __unicode__(self):
    return name

class Photo(models.Model):
  image = models.ImageField(upload_to='photos/') # PLEASE CHANGE UPLOAD_TO TO A FUNCTION THAT RETURNS A SANE FOLDER NAME AND CREATES IT.
  caption = models.TextField(blank=True)
  exif = models.TextField(blank=True)
  album = models.ForeignKey("PhotoAlbum")

class Occupation(models.Model):
  name = models.CharField(max_length=255)
  parent = models.ForeignKey('Occupation', blank=True, null=True)

  def __unicode__(self):
    return self.name

  def is_child(self):
    return self.parent != None

  def is_parent(self):
    if self.is_child():
      return False
    else:
      if Occupation.objects.filter(parent=self).count() > 0: #self has children
        return True
    return False

class PersonOccupation(models.Model):
  person = models.ForeignKey(Person, db_index=True)
  occupation = models.ForeignKey(Occupation)
  order = models.IntegerField()


'''
# Orphans was a ridiculous concept. Orphans are Persons too. They just don't have a django auth user entry.
class Orphan(models.Model):
  first_name = models.CharField(max_length=255)
  last_name = models.CharField(max_length=255, blank=True)
  email = models.EmailField(blank=True, null=True, unique=True)
#  about = models.TextField(blank=True, null=True) 

class PersonOrphan(models.Model):
  person = models.ForeignKey(Person)
  orphan = models.ForeignKey(Orphan)
  relation = models.ForeignKey("Relation")
'''

class PersonPerson(models.Model):
  person1 = models.ForeignKey(Person, related_name='PersonFrom', db_index=True)
  person2 = models.ForeignKey(Person, related_name='PersonTo', db_index=True)
  relation = models.ForeignKey("Relation")
  disapproved = models.BooleanField(default=False)

class Relation(models.Model):
  name = models.CharField(max_length=255, db_index=True)
  reverse = models.CharField(max_length=255, db_index=True)

  def __unicode__(self):
    return name

class Location(models.Model):
  address = models.TextField(blank=True, null=True)
  city = models.CharField(blank=True, max_length=255, db_index=True)
  pin_code = INZipCodeField()
  lat = models.FloatField(blank=True, null=True)
  lng = models.FloatField(blank=True, null=True)

class Training(models.Model):
  person = models.ForeignKey("Person")
  area = models.CharField(max_length=255) # Choices?
  with_whom = models.CharField(max_length=255) # Is this a foreign key to person, or group, or just text field like now?
  where = models.ForeignKey("TheatreGroup")
  from_when = models.DateField(blank=True, null=True)
  until_when = models.DateField(blank=True, null=True)


class Production(models.Model):
  name = models.CharField(max_length=255, db_index=True)
  language = models.ForeignKey("Language", blank=True, null=True)
  group = models.ForeignKey("TheatreGroup")
  director = models.ForeignKey(Person)

SHOWS_NO_CHOICES = (
  ('5', '0-5 Shows'),
  ('10', '6-10 Shows'),
  ('50', '11-50 Shows'),
  ('100', '51-100 Shows'),
  ('1000', 'More Than 100 Shows'),
#add more.
)

class PersonProduction(models.Model):
  person = models.ForeignKey(Person, db_index=True)
  production = models.ForeignKey(Production, db_index=True)
  role = models.CharField(max_length=255)
  start_year = models.IntegerField(max_length=4)
  years = models.IntegerField(blank=True, null=True)
  no_of_shows = models.CharField(max_length=25, choices = SHOWS_NO_CHOICES)  
  original_cast = models.BooleanField(default=False)

class TheatreGroup(models.Model):
  name = models.CharField(max_length=255, db_index=True) # name + location is unique
  city = models.CharField(max_length=255)
  location = models.ForeignKey(Location, blank=True, null=True)
  tel = models.IntegerField(blank=True, null=True)
  email = models.EmailField(blank=True, null=True)
  about = models.TextField(blank=True, null=True)
  nature_of_work = models.CharField(max_length=255)
  founded = models.CharField(max_length=10)


class PersonGroup(models.Model):
  person = models.ForeignKey(Person, db_index=True)
  group = models.ForeignKey(TheatreGroup, db_index=True)
  is_admin = models.BooleanField(default=False)
  role = models.CharField(max_length=255, blank=True)

class Language(models.Model):
  code = models.CharField(max_length=3, db_index=True)
  name = models.CharField(max_length=255)
