from django.db import models

# Create your models here.
class Sport(models.Model):
    """
    don't use models.choices because we want the list to be transactional data
    example list: [
        'bike',
        'run',
        'swim',
        'measurements',
        'yoga',
        'weights',

        # for multi-sport `Event`s:
        'multisport', #EventType.sport
        'transition', #Entry.sport
    ]
    """
    sport = models.CharField(max_length=20)

    class Meta:
        ordering = ['sport']

    def __unicode__(self):
        return self.sport

    def __str__(self):
        return self.sport

class Measurements(models.Model):
    id = models.AutoField(primary_key=True) #added by default
    weight = models.FloatField(blank=True, null=True)

class Equipment(models.Model):
    """
    this is for things such as bikes, shoes, wheelsets; i.e. things with a
    determinable depreciation cost or maintenance periods
    """
    name = models.CharField(max_length=50)
    cost = models.DecimalField(blank=True, null=True, max_digits=8, decimal_places=2)
    acquired_date = models.DateField()
    disposal_date = models.DateField(blank=True, null=True)
    disposal_method = models.CharField(blank=True, max_length=7, choices=[
        ('sold', 'sold'),
        ('donated', 'donated'),
        ('retired', 'retired'),# i.e. 'broken'
    ])
    disposal_proceeds = models.DecimalField(blank=True, null=True, max_digits=8, decimal_places=2)
    expected_lifespan = models.DurationField(blank=True, null=True)
    maintenance_interval = models.DurationField(blank=True, null=True)

    def history(self):
        return EquipmentMaintenance.objects.filter(equipment=self.id)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

class EquipmentMaintenance(models.Model):
    date = models.DateField()
    description = models.CharField(max_length=250)
    equipment = models.ForeignKey(Equipment)
    cost = models.DecimalField(blank=True, null=True, max_digits=8, decimal_places=2)
    vendor = models.CharField(max_length=50, default='DIY')

class EventType(models.Model):
    """
    examples: '5k', 'Olympic', 'Criterium'
    """
    event_type = models.CharField(max_length=20)
    sport = models.ForeignKey(Sport)

    class Meta:
        ordering = ['sport', 'event_type']

    def __unicode__(self):
        return str(self.sport) + ': ' + self.event_type

    def __str__(self):
        return str(self.sport) + ': ' + self.event_type

class Event(models.Model):
    name = models.CharField(max_length=35)
    location = models.CharField(max_length=50)
    event_type = models.ForeignKey(EventType, blank=True, null=True)
    bib_number = models.IntegerField(blank=True, null=True)

    dnf = models.BooleanField()
    finish_overall = models.IntegerField(blank=True, null=True)
    finishers_overall = models.IntegerField(blank=True, null=True)
    #maybe just use "handicapped" as the age group description??
    finish_handicapped = models.IntegerField(blank=True, null=True)
    finish_gender = models.IntegerField(blank=True, null=True)
    finishers_gender = models.IntegerField(blank=True, null=True)
    finish_age_group = models.IntegerField(blank=True, null=True)
    finishers_age_group = models.IntegerField(blank=True, null=True)
    # category/age_group seem to be mutually-exclusive?
    category = models.CharField(max_length=10, blank=True, null=True)
    age_group = models.CharField(max_length=10, blank=True)
    results_url = models.URLField(blank=True, null=True)
    official_time = models.TimeField(blank=True, null=True) #used for total event time (brevets & triathlons)

    ## TODO: maybe this should be handled by multiple `Entry`s?
    # swim_distance = models.FloatField(blank=True)
    # bike_distance = models.FloatField(blank=True)
    # run_distance = models.FloatField(blank=True)
    # swim_time = models.TimeField(blank=True)
    # bike_time = models.TimeField(blank=True)
    # run_time = models.TimeField(blank=True)
    # t1_time = models.TimeField(blank=True)
    # t2_time = models.TimeField(blank=True)

    def get_absolute_url(self):
        return "/events?event=%d" % self.id

    def __unicode__(self):
        return self.name + ' ['+self.date.strftime('%b %d, %Y')+']'

    def __str__(self):
        return self.name + ' ['+self.date.strftime('%b %d, %Y')+']'

class Entry(models.Model):
    #entry_id:
    date = models.DateField()
    sport = models.ForeignKey(Sport)
    event = models.ForeignKey(Event, blank=True, null=True)
    route = models.CharField(max_length=50, blank=True) # routes Model?
    notes = models.CharField(max_length=256, blank=True)
    equipment = models.ForeignKey(Equipment, blank=True, null=True)
    distance = models.FloatField(blank=True, null=True)
    time = models.TimeField(blank=True, null=True)
    avg_speed = models.FloatField(blank=True, null=True)
    max_speed = models.FloatField(blank=True, null=True)
    elevation_gain = models.IntegerField(blank=True, null=True)
    calories = models.IntegerField(blank=True, null=True)
    #pace: models.TimeField(blank=True, default=calc_pace(self.time/self.distance)) #could be calculated...

    class Meta:
        ordering = ['date', 'id']

    def __unicode__(self):
        return self.date.strftime('%b %d, %Y') + ' ['+str(self.sport)+']'

    def __str__(self):
        return self.date.strftime('%b %d, %Y') + ' ['+str(self.sport)+']'
