# Path: foosball/games/utils.py
# def random_colour():
#     """Generate random colour.
# 
#     Thanks: http://stackoverflow.com/a/14019260/1489738
#     """
#     r = lambda: random.randint(0, 255)
#     return '#%02X%02X%02X' % (r(), r(), r())


from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from model_utils.models import TimeStampedModel
from foosball.games.utils import random_colour
from foosball.users.models import User



class Table(models.Model):
    name = models.CharField(unique=True, max_length=255, verbose_name=_("name"))
    description = models.TextField(blank=True, verbose_name=_("description"))

    def __str__(self):
        return self.name


class Game(TimeStampedModel):
    created_by = models.ForeignKey(
        User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
    )
    modified_by = models.ForeignKey(
        User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
    )

    played_at = models.DateTimeField(
        default=timezone.now,
        verbose_name=_("played at"),
        help_text=_("Time when the game was played")
    )

    table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))

    class Meta:

# Path: foosball/games/utils.py
# def random_colour():
#     """Generate random colour.
# 
#     Thanks: http://stackoverflow.com/a/14019260/1489738
#     """
#     r = lambda: random.randint(0, 255)
#     return '#%02X%02X%02X' % (r(), r(), r())


# Path: foosball/games/models.py
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from model_utils.models import TimeStampedModel
from foosball.games.utils import random_colour
from foosball.users.models import User




class Table(models.Model):
    name = models.CharField(unique=True, max_length=255, verbose_name=_("name"))
    description = models.TextField(blank=True, verbose_name=_("description"))

    def __str__(self):
        return self.name


class Game(TimeStampedModel):
    created_by = models.ForeignKey(
        User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
    )
    modified_by = models.ForeignKey(
        User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
    )

    played_at = models.DateTimeField(
        default=timezone.now,
        verbose_name=_("played at"),
        help_text=_("Time when the game was played")
    )

    table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))

    class Meta:

        ordering = ['-played_at']