from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models

from users.models import Player


class Author(models.Model):
    name        = models.CharField(max_length=255)
    author_id    = models.IntegerField()

    def __str__(self):
        return f"{self.author_id} - {self.name}"

class Level(models.Model):
    author      = models.ForeignKey(Author, on_delete=models.CASCADE)
    title       = models.CharField(max_length=16)
    level_id     = models.IntegerField()
    darts       = models.SmallIntegerField()
    target      = models.SmallIntegerField()
    plays       = models.IntegerField()
    beats       = models.IntegerField()  # TODO can be removed?
    rating      = models.FloatField(null=True, blank=True)
    data        = models.CharField(max_length=288)

    played_by = models.ManyToManyField(Player, blank=True, related_name="bloonsa_levels_played")

    @property
    def play_count(self):
        return self.played_by.count()

    def __str__(self):
        return f"{self.author} - {self.title}"

    def get_flash_vars(self, seperator):
        flashVars = {
            "levelNum": self.level_id,
            "title": self.title,
            "darts": self.darts,
            "target": self.target,
            "data": self.data,
            "authorID": self.author.author_id,
            "rating": self.rating if self.rating else 0.0,
        }
        flashVarsStr = f"{seperator}".join([f"{k}={v}"
                                            for k, v in flashVars.items()])
        return flashVarsStr


class LevelRating(models.Model):
    player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name="bloonsa_level_ratings", null=True)
    level = models.ForeignKey(Level, on_delete=models.CASCADE)
    rating = models.SmallIntegerField(validators=[MinValueValidator(1),
                                                  MaxValueValidator(10)])

    def __str__(self):
        return (f"{self.player.user.username}"
                f" - [<{int(self.rating) * '★'}> - {self.level.title}]")

# There should only be 1 score per player
# Highest popcount wins
class LevelScore(models.Model):
    player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name="bloonsa_level_scores", null=True)
    level = models.ForeignKey(Level, on_delete=models.CASCADE)
    clear = models.BooleanField(default=True)  # This is for if we ever submit scores for failed attempts
    darts_left = models.PositiveSmallIntegerField(null=True)
    pops = models.PositiveSmallIntegerField(null=True)

    def __str__(self):
        clearState = "✅" if self.clear else "❌"
        return (f"{self.player.user.username}'s {clearState} @ {self.level.title}: "
                f"🎈{self.pops} | 🎯{self.darts_left}")