from django.db import models

# Create your models here.
class User(models.Model):
    password = models.CharField(max_length=16)
    nickname = models.CharField(max_length=32)
    photo = models.TextField()
    phone = models.CharField(max_length=16)
    contacts_version = models.CharField(max_length=15,null=True)
    longitude = models.DecimalField(max_digits=8, decimal_places=5,null=True)
    latitude = models.DecimalField(max_digits=8, decimal_places=5,null=True)
    code = models.CharField(max_length=20)

    def __unicode__(self):
        return str(self.id)+' '+self.nickname +' '+self.code+' '+self.phone


class Group(models.Model):
    name = models.CharField(max_length=32)
    ucount = models.IntegerField()
    code = models.CharField(max_length=20)
    users = models.ManyToManyField(User,through='GroupRelation')
    creater_id = models.IntegerField(null=True)

    def __unicode__(self):
        return str(self.id)+' '+self.name +' '+self.code+' '+self.ucount+' '+self.creater_id

class Message(models.Model):
    time = models.DateTimeField()
    type = models.IntegerField()
    content_id = models.IntegerField()
    users = models.ManyToManyField(User,through='ImmediatelyMessage')
    groups = models.ManyToManyField(Group,through='GroupMessage')

    def __unicode__(self):
        return str(self.id)+' '+self.type +' '+self.content_id+' '+self.time


class TextMessage(models.Model):
    message = models.ForeignKey(Message,null=True)
    content = models.TextField()

    def __unicode__(self):
        return str(self.id)+' '+self.message_id+' '+self.content


class PictureMessage(models.Model):
    message = models.ForeignKey(Message,null=True)
    content = models.TextField()

    def __unicode__(self):
        return str(self.id)+' '+self.message_id+' '+self.content


class VoiceMessage(models.Model):
    message = models.ForeignKey(Message,null=True)
    content = models.TextField()

    def __unicode__(self):
        return str(self.id)+' '+self.message_id+' '+self.content

class LocationMessage(models.Model):
    message = models.ForeignKey(Message,null=True)
    content = models.TextField()

    def __unicode__(self):
        return str(self.id)+' '+self.message_id+' '+self.content

class BottlePicture(models.Model):
    usedTimes = models.IntegerField()
    tookTime = models.DateTimeField()
    user = models.ForeignKey(User,null=True)
    author = models.CharField(max_length=32,null=True)
    content = models.TextField(max_length=90)
    longitude = models.DecimalField(max_digits=8, decimal_places=5,null=True)
    latitude = models.DecimalField(max_digits=8, decimal_places=5,null=True)

    def __unicode__(self):
        return str(self.id)+' '+self.author +' '+self.tookTime+' '\
               +self.usedTimes+' '+self.latitude+' '+self.longitude

class ImmediatelyMessage(models.Model):
    user_from = models.ForeignKey(User)
    message = models.ForeignKey(Message)
    user_to_id = models.IntegerField()
    time = models.DateTimeField()
    type = models.IntegerField()

    def __unicode__(self):
        return str(self.id)+' '+self.user_from_id +' '\
               +self.user_to_id+' '+self.type+' '+self.message_id+' '+self.time

class AddFriend(models.Model):
    user_from = models.ForeignKey(User)
    user_to_id = models.IntegerField()
    time = models.DateTimeField()

    def __unicode__(self):
        return str(self.id)+' '+self.user_from_id +' '+self.user_to_id+' '+self.time


class FriendRelation(models.Model):
    user_from = models.ForeignKey(User)
    user_to_id = models.IntegerField()
    time = models.DateTimeField()
    inBlackList = models.BooleanField(default=None)
    remark = models.CharField(max_length=16)
    top = models.BooleanField(default=None)

    def __unicode__(self):
        return str(self.id)+' '+self.user_from_id +' '\
               +self.user_to_id+' '+self.time+' '+self.inBlackList+' '+self.remark+' '+self.top

class GroupRelation(models.Model):
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)
    time = models.DateTimeField()

    def __unicode__(self):
        return str(self.id)+' '+self.user_id +' '+self.group_id+' '+self.time

class GroupMessage(models.Model):
    user_from = models.ForeignKey(User)
    message = models.ForeignKey(Message)
    group_to = models.ForeignKey(Group)
    time = models.DateTimeField()

    def __unicode__(self):
        return str(self.id)+' '+self.user_from_id +' '+self.group_to_id+' '+self.time+' '+self.message_id