from django.db import models
from guildbuilder.globals import Globals

# Profession attribute model.
class Attribute(models.Model):
   """
   This class represents one of a Profession's attributes.
   """
   name        = models.CharField("Attribute Name", maxlength=40, unique=True)
   description = models.TextField("Description", blank=True)

   def __str__(self):
      return self.name

   class Meta:
      ordering = ['name']

   class Admin:
      list_display = ('name',)

# Player profession model.
class Profession(models.Model):
   """
   This class represents a playable profession.
   """
   name            = models.CharField("Profession Name", maxlength=40, unique=True)
   energy_max      = models.SmallIntegerField("Maximum Energy", blank=False)
   energy_recovery = models.SmallIntegerField("Energy Recovery Rate", blank=False)
   icon            = models.ImageField(upload_to='icons_professions', default='icons_professions/default.png', blank=True)
   primary_attrib  = models.ForeignKey(Attribute, verbose_name="Primary Attribute", related_name="primary_attrib")
   attributes      = models.ManyToManyField(Attribute, filter_interface=models.HORIZONTAL, related_name="attributes")
   description     = models.TextField("Description", blank=True)

   def __str__(self):
      return self.name

   class Meta:
      ordering = ['name']

   class Admin:
      list_display = ('name', 'energy_max', 'energy_recovery', 'icon')
      fields = (
         ('None', {'fields': ('name', ('energy_max', 'energy_recovery'), 'primary_attrib', 'attributes', 'icon', 'description',), 'classes': 'wide'}),
      )

# Campaign model.
class Campaign(models.Model):
   """
   This class represents a particular chapter/episode of Guild Wars. This is
   primarily used for skill entry to indicate where each one originates from.
   """
   name        = models.CharField("Campaign Name", maxlength=40, unique=True)
   website     = models.URLField("Website", help_text="A-Net's page for the campaign.")
   description = models.TextField("Description", blank=True)

   def __str__(self):
      return self.name

   class Meta:
      ordering = ['name']

   class Admin:
      list_display = ('name',)

# Skill type model.
class SkillType(models.Model):
   """
   Each Skill has a certain type, whether it be 'Spell', 'Skill', 'Enchantment',
   or whatnot. This class allows us to dynamically add and modify them.
   """
   name        = models.CharField("Skill Type", maxlength=40, unique=True)
   description = models.TextField("Description", blank=True)

   def __str__(self):
      return self.name

   class Meta:
      ordering = ['name']

   class Admin:
      list_display = ('name',)

# Player skill model. Spells and other things are a subset of the term 'skill', so make
# sure to think of everything (including spells) as skills.
class Skill(models.Model):
   """
   This class represents an individual skill and its characteristics.
   """
   name        = models.CharField("Skill Name", maxlength=40, unique=True)
   is_elite    = models.BooleanField("Is Elite", help_text="Is this an elite skill or spell?")
   is_adrenal  = models.BooleanField("Uses Adrenaline", help_text="Does this skill use adrenaline?")
   campaign    = models.ForeignKey(Campaign, radio_admin=True, help_text="From which campaign did the skill originate?")
   cost        = models.PositiveSmallIntegerField("Energy/Adren Cost", blank=False, default=0)
   upkeep      = models.PositiveSmallIntegerField("Upkeep Cost", blank=False, default=0)
   cast_time   = models.FloatField("Casting Time", blank=False, max_digits=4, decimal_places=2, default=0)
   recycle     = models.FloatField("Recycle Time", blank=False, max_digits=4, decimal_places=2, default=0)
   type        = models.ForeignKey(SkillType, help_text="Spell, Skill, or Other?")
   nature      = models.CharField("Nature", maxlength=2, choices=Globals.SKILL_NATURE, help_text="Does this skill cause damage, or prolong survival?")
   profession  = models.ForeignKey(Profession, blank=True, null=True, help_text="What profession can use this skill? (Leave blank for all).")
   attribute   = models.ForeignKey(Attribute, blank=True, null=True, help_text="What (if any) attribute does this skill rely on?")
   icon        = models.ImageField(upload_to='icons_skills', default='icons_skills/default.png', blank=True, help_text="The A-Net provided icon for the skill.")
   description = models.TextField("Description", blank=True, help_text="The skill text shown in-game.")
   guildwiki_link = models.URLField("GuildWiki Page", blank=True, help_text="The URL to the relevant GuildWiki page.", verify_exists=False)

   def __str__(self):
      return self.name

   class Meta:
      ordering = ['name']

   class Admin:
      list_display = ('name', 'is_elite', 'is_adrenal', 'campaign', 'upkeep', 'cost', 'cast_time', 'recycle', 'profession', 'attribute')
      list_filter = ('profession', 'campaign', 'is_elite', 'is_adrenal', 'nature',)
      search_fields = ['name']
      save_on_top = True
      fields = (
         ('None', {'fields': ('name', 'profession', 'campaign', 'attribute', ('is_elite', 'is_adrenal'), ('upkeep', 'cost', 'cast_time', 'recycle'), 'type', 'nature', 'icon', 'description', 'guildwiki_link',), 'classes': 'wide'}),
      )
