# coding=utf-8
from django.contrib.auth.models import User
from django.db import models
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _


MEASUREMENT_UNITS = [('KG', 'kg'),
                     ('G', 'g'),
                     ('MG', 'mg'),
                     ('L', 'l'),
                     ('ML', 'ml'),
                     ('TEA', _('teaspoon')),
                     ('TBL', _('tablespoon')),
                     ('PINCH', _('pinch')),
                     ('PIECE', _('piece'))]


class Profile(models.Model):
    user = models.OneToOneField(User, models.CASCADE)
    daily_fat_demand = models.PositiveIntegerField(_('Daily fat demand'),
                                                   help_text=_('At most this amount of fat is needed per day'),
                                                   default=117)
    daily_carbohydrate_demand = models.PositiveIntegerField(
            _('Daily carbohydrate demand'),
            help_text=_('At most this amount of carbohydrates is needed per day'),
            default=150)
    daily_sugar_demand = models.PositiveIntegerField(_('Daily sugar demand'),
                                                     help_text=_('At most this amount of sugar is needed per day'),
                                                     default=25)
    daily_roughage_demand = models.PositiveIntegerField(
            _('Daily roughage demand'),
            help_text=_('At least this amount of roughage should be eaten per day'),
            default=30)
    daily_protein_demand = models.PositiveIntegerField(
            _('Daily protein demand'),
            help_text=_('At most this amount of protein should be eaten per day'),
            default=90)
    
    class Meta:
        verbose_name = _('Profile')
        verbose_name_plural = _('Profiles')
    
    def __str__(self):
        return self.user.username


class KitchenUtility(models.Model):
    name = models.CharField(_('Name'), max_length=255)
    
    class Meta:
        verbose_name = _('Kitchen utility')
        verbose_name_plural = _('Kitchen utilities')
    
    def __str__(self):
        return self.name


class Ingredient(models.Model):
    name = models.CharField(_('Name'), max_length=255)
    
    class Meta:
        verbose_name = _('Ingredient')
        verbose_name_plural = _('Ingredients')
    
    def __str__(self):
        return self.name


class Recipe(models.Model):
    author = models.ForeignKey(User, models.SET_NULL, null=True)
    kitchen_utilities = models.ManyToManyField(KitchenUtility,
                                               through='UsedKitchenUtility')
    ingredients = models.ManyToManyField(Ingredient, through='UsedIngredient')
    
    name = models.CharField(_('Name'), max_length=255)
    image = models.ImageField(_('Image'), blank=True, null=True)
    experience_level = models.CharField(_('Required cooking experience'), max_length=255,
                                        choices=[
                                            ('NOV', _('Novice')),
                                            ('INT', _('Intermediate')),
                                            ('ADV', _('Advanced')),
                                            ('EXP', _('Expert'))
                                        ])
    
    fat = models.PositiveIntegerField(_('Fat'),
                                      help_text=_('How much fat does one portion contain?'))
    carbohydrates = models.PositiveIntegerField(_('Carbohydrates'),
                                                help_text=_('How much carbohydrates does one portion contain?'))
    sugar = models.PositiveIntegerField(_('Sugar'),
                                        help_text=_('How much sugar does one portion contain?'))
    roughage = models.PositiveIntegerField(_('Roughage'),
                                           help_text=_('How much roughage does one portion contain?'))
    protein = models.PositiveIntegerField(_('Protein'),
                                          help_text=_('How much protein does one portion contain?'))
    
    class Meta:
        verbose_name = _('Recipe')
        verbose_name_plural = _('Recipes')
    
    def __str__(self):
        return self.name


class RecipeStep(models.Model):
    recipe = models.ForeignKey(Recipe, models.CASCADE)
    image = models.ImageField(_('Image'), blank=True, null=True)
    description = models.TextField(_('Description'))
    number_of_step = models.PositiveIntegerField(_('Number of step'))
    
    class Meta:
        unique_together = ['recipe', 'number_of_step']
        verbose_name = _('Recipe step')
        verbose_name_plural = _('Recipe steps')


class UsedKitchenUtility(models.Model):
    recipe = models.ForeignKey(Recipe, models.CASCADE)
    kitchen_utility = models.ForeignKey(KitchenUtility, models.CASCADE)
    amount = models.PositiveIntegerField(_('Amount'),
                                         help_text=_('How often is this kitchen utility used?'),
                                         default=1)
    
    class Meta:
        unique_together = ['recipe', 'kitchen_utility']
        verbose_name = _('Used kitchen utility')
        verbose_name_plural = _('Used kitchen utilities')


class UsedIngredient(models.Model):
    recipe = models.ForeignKey(Recipe, models.CASCADE)
    ingredient = models.ForeignKey(Ingredient, models.CASCADE)
    amount = models.PositiveIntegerField(_('Amount'),
                                         help_text=_('Please specify the used amount of the ingredient'))
    unit = models.CharField(_('Unit for amount'), max_length=10, choices=MEASUREMENT_UNITS)
    
    class Meta:
        unique_together = ['recipe', 'ingredient']
        verbose_name = _('Used ingredient')
        verbose_name_plural = _('Used ingredients')


class Vendor(models.Model):
    name = models.CharField(_('Vendor'), max_length=50)
    
    class Meta:
        verbose_name = _('Vendor')
        verbose_name_plural = _('Vendors')
    
    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(_('Product'), max_length=255)
    vendor = models.ForeignKey(Vendor, models.CASCADE)
    ingredient = models.ForeignKey(Ingredient, models.SET_NULL, null=True)
    amount = models.PositiveIntegerField(_('Amount'),
                                         help_text=_('Please specify the provided amount of the ingredient'))
    unit = models.CharField(_('Unit for amount'), max_length=10,
                            choices=MEASUREMENT_UNITS)
    price = models.DecimalField(_('Price'), decimal_places=2, max_digits=4,
                                blank=True, default=0.0)
    
    class Meta:
        verbose_name = _('Product')
        verbose_name_plural = _('Products')
    
    def __str__(self):
        return self.name


class Pantry(models.Model):
    owner = models.ForeignKey(User, models.CASCADE)
    ingredients = models.ManyToManyField(Ingredient, through='StoredIngredient')
    
    class Meta:
        verbose_name = _('Pantry')
        verbose_name_plural = _('Pantries')
    
    def __str__(self):
        return str.format('Pantry of {username}',
                          username=self.owner.username)


class StoredIngredient(models.Model):
    pantry = models.ForeignKey(Pantry, models.CASCADE)
    ingredient = models.ForeignKey(Ingredient, models.CASCADE)
    amount = models.PositiveIntegerField(_('Amount'),
                                         help_text=_('Please specify the stored amount of the ingredient'))
    unit = models.CharField(_('Unit for amount'), max_length=10,
                            choices=MEASUREMENT_UNITS)
    
    class Meta:
        unique_together = ['pantry', 'ingredient']
        verbose_name = _('Stored ingredient')
        verbose_name_plural = _('Stored ingredients')
    
    def __str__(self):
        return str.format('{amount} {unit} of {ingredient}',
                          amount=self.amount, unit=self.unit, ingredient=self.ingredient)
