from django.db import models
from django.contrib.auth.models import User
from taggit.managers import TaggableManager
#from tasks import process_item
from worldofloot.images.converter import ImageHandler

image_handler = ImageHandler('worldofloot-images')

class UserProfile(models.Model):
  user = models.OneToOneField(User)
  created = models.DateTimeField(auto_now_add=True)

  # nothing else for now

class Item(models.Model):
  item_id = models.CharField(max_length=10)
  item_type = models.CharField(max_length=30)   # gear or mount

  name = models.CharField(max_length=75)
  ilvl = models.IntegerField(default=-1)
  quality = models.CharField(max_length=20, null=True) # TODO convert to select
  icon = models.CharField(max_length=75, null=True)
  slot = models.CharField(max_length=25, null=True) # TODO convert to select

  wants = models.IntegerField(default=0)
  haves = models.IntegerField(default=0)
  popularity = models.IntegerField(default=0)

  created = models.DateTimeField(auto_now_add=True)
  modified = models.DateTimeField(auto_now=True)

  tags = TaggableManager()

  def get_uid(self):
    return '%s-%s' % (self.item_id, self.item_type)

  def increment_wants(self):
    self.wants += 1
    self.popularity += 1

  def increment_haves(self):
    self.haves += 1
    self.popularity += 1

  def __unicode__(self):
    return 'Item %s (%s, %s)' % (self.name, self.item_id, self.item_type)

class Pin(models.Model):
  item = models.ForeignKey(Item)
  user = models.ForeignKey(User, null=True) # TODO convert to profile?
  session = models.CharField(max_length=60, null=True)
  verb = models.CharField(max_length=10) # TODO convert to select

  comment = models.CharField(max_length=200, null=True)

  created = models.DateTimeField(auto_now_add=True)
  modified = models.DateTimeField(auto_now=True)

  def __unicode__(self):
    return 'Pin of %s: %s' % (self.item.name, self.comment)

class Image(models.Model):
  item = models.ForeignKey(Item)
  image_id = models.CharField(primary_key=True, max_length=20)
  path = models.CharField(max_length=200)
  thumb_path = models.CharField(max_length=200)
  attribution = models.CharField(max_length=25)

  priority = models.IntegerField(default=15)  # lower is better

  def resize(self):
    # NOTE only call me once!
    # resize, upload to s3, and then update path
    self.thumb_path = image_handler.resize_and_upload(self.path, \
        '%s-%s-%d' % (self.item.item_type, self.item.item_id, self.priority) \
        , 250, 1000)
    self.save()

  def __unicode__(self):
    return 'Image for %s' % (self.item.name)

"""
# Introspection rules for south
try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^tagging\.fields\.TagField"])
except ImportError:
    pass
"""
