from django.db import models
from django.contrib.auth.models import User
import datetime

class Family(models.Model):
	last_name = models.CharField(max_length=40)
	address_line_1 = models.CharField(max_length=200)
	address_line_2 = models.CharField(max_length=200, null=True)
	address_city = models.CharField(max_length=100)
	address_state = models.CharField(max_length=50)
	address_zip_code = models.IntegerField(null=True)
	phone_number = models.FloatField(null=True)
	contacts = models.ManyToManyField("self", null=True, blank=True)

	def __unicode__(self):
		return u'%s Family' % (self.last_name)
		
	def familyAddress(self):
		return u'%s\\n%s\\n%s, %s %d' % (self.address_line_1, self.address_line_2, self.address_city, self.address_state, self.address_zip_code)

	def familyPhone(self):
		return u'%d' % (self.phone_number)

	def childrenList(self):
		display = ""
		first = True
		for child in self.children.all():
			if not first:
				display += ", "
			else:
				first = False
			display += child.first_name
		return display

	def parentList(self):
		display = ""
		first = True
		for parent in self.family_members.all():
			if not first:
				display += ", "
			else:
				first = False
			display += parent.first_name
		return display

	def displayFamilyNames(self):
		display = self.last_name + ", "
		first = True
		for adult in self.family_members.all():
			if not first:
				display += " and "
			else:
				first = False
			display += adult.first_name
		return display

class Child(models.Model):
	family = models.ForeignKey(Family, related_name="children")
	first_name = models.CharField(max_length=40)
	is_driver = models.BooleanField()

	def __unicode__(self):
		return u'%s %s' % (self.first_name, self.family.last_name)

class FamilyMember(models.Model):
	user = models.OneToOneField(User, related_name="user_account", null=True, blank=True)
	first_name = models.CharField(max_length=40)
	last_name = models.CharField(max_length=40) # TODO: all family members have the same family name
												#       nuke this field sometime
	date_of_birth = models.DateField()
	timestamp = models.DateTimeField() # TODO: nuke this field sometime
	family = models.ForeignKey(Family, related_name="family_members")
	is_adult = models.BooleanField()
	is_driver = models.BooleanField()

	def __unicode__(self):
		return u'%s %s' % (self.first_name, self.last_name)

class Schedule(models.Model):
	name = models.CharField(max_length=100)
	description=models.TextField(null=True)
	timestamp = models.DateTimeField()
	creator = models.ForeignKey(User, related_name="creator")

	def __unicode__(self):
		return u'%s' % (self.name,)

class FamilyScheduleDetails(models.Model):
	schedule = models.ForeignKey(Schedule, related_name="users_schedule_details")
	family = models.ForeignKey(Family, related_name="family_schedules_details")
	attendees = models.ManyToManyField(FamilyMember, related_name="attending_schedule_details")
	child_attendees = models.ManyToManyField(Child, related_name="attending_schedule_details")
	notes = models.TextField()

	def __unicode__(self):
		return u'Details for %s Schedule for the %s' % (self.schedule, self.family)

class Event(models.Model):
	name = models.CharField(max_length=100)
	description = models.TextField(null=True)
	location = models.CharField(max_length=50)
	timestamp = models.DateTimeField(blank=True, null=True)
	creator = models.ForeignKey(User, related_name="created_events")
	startTime = models.DateTimeField()
	endTime = models.DateTimeField()
	schedule = models.ForeignKey(Schedule, related_name="events", null=True, blank=True)

	def __unicode__(self):
		return u'%s' % (self.name,)

class FamilyEventDetails(models.Model):
	event = models.ForeignKey(Event, related_name="users_event_details")
	family = models.ForeignKey(Family, related_name="family_events_details")
	notes = models.CharField(max_length=1000)
	attendees = models.ManyToManyField(FamilyMember, related_name="attending_events_details")
	child_attendees = models.ManyToManyField(Child, related_name="attending_event_details")
	driverTo = models.ForeignKey(User, related_name="driving_responsibilites_to", null=True, blank=True)
	driverFrom = models.ForeignKey(User, related_name="driving_responsibilites_from", null=True, blank=True)

	def __unicode__(self):
		return u'Details for %s Event for the %s' % (self.event, self.family)

class Message(models.Model):
	sender = models.ForeignKey(Family, related_name="sent_messages")
	receiver = models.ForeignKey(Family, related_name="received_messages")
	in_inbox = models.BooleanField()
	message = models.TextField()
	subject = models.CharField(max_length=200)
	time_sent = models.DateTimeField()

	def __unicode__(self):
		return u'"%s" from %s to %s' % (self.subject, self.sender, self.receiver)

	def time_display(self):
		# now = datetime.datetime.now()
		# today = now.replace(hour=0, minute=0, second=0, microsecond=0)
		return self.time_sent.strftime("%I:%M %p - %a, %b %d, %Y")

class ContactRequest(models.Model):
	family = models.ForeignKey(Family, related_name="pending_requests")
	requested_contact = models.ForeignKey(Family, related_name="contact_requests")

	def __unicode__(self):
		return u'%s requesting %s' % (self.family, self.requested_contact)
