# coding=utf-8
from django.contrib.auth.models import User
from django.db import models
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_calories_demand = models.DecimalField(_('Daily kilo calories demand'),
                                                help_text=_('At most this amount of kilo calories is needed per day'),
                                                default=2000.00,
                                                decimal_places=2,
                                                max_digits=6)
    daily_fat_demand = models.DecimalField(_('Daily fat demand'),
                                           help_text=_('At most this amount of fat is needed per day'),
                                           default=117.00,
                                           decimal_places=2,
                                           max_digits=5)
    daily_carbohydrate_demand = models.DecimalField(
            _('Daily carbohydrate demand'),
            help_text=_('At most this amount of carbohydrates is needed per day'),
            default=150.00,
            decimal_places=2,
            max_digits=5,
    )
    daily_sugar_demand = models.DecimalField(_('Daily sugar demand'),
                                             help_text=_('At most this amount of sugar is needed per day'),
                                             default=25.00,
                                             decimal_places=2,
                                             max_digits=5)
    daily_roughage_demand = models.DecimalField(
            _('Daily roughage demand'),
            help_text=_('At least this amount of roughage should be eaten per day'),
            default=30.00,
            decimal_places=2,
            max_digits=5
    )
    daily_protein_demand = models.DecimalField(
            _('Daily protein demand'),
            help_text=_('At most this amount of protein should be eaten per day'),
            default=90.00,
            decimal_places=2,
            max_digits=5,
    )
    
    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'))
                                        ])
    
    calories = models.DecimalField(_('Kilo Calories'), decimal_places=2, max_digits=6,
                                   help_text=_('How many kilo calories are contained in one portion?'))
    fat = models.DecimalField(_('Fat'), decimal_places=2, max_digits=5,
                              help_text=_('How much fat does one portion contain?'))
    carbohydrates = models.DecimalField(_('Carbohydrates'), decimal_places=2, max_digits=5,
                                        help_text=_('How much carbohydrates does one portion contain?'))
    sugar = models.DecimalField(_('Sugar'), decimal_places=2, max_digits=5,
                                help_text=_('How much sugar does one portion contain?'))
    roughage = models.DecimalField(_('Roughage'), decimal_places=2, max_digits=5,
                                   help_text=_('How much roughage does one portion contain?'))
    protein = models.DecimalField(_('Protein'), decimal_places=2, max_digits=5,
                                  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'))
    
    class Meta:
        verbose_name = _('Recipe step')
        verbose_name_plural = _('Recipe steps')
        order_with_respect_to = 'recipe'


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)


class MealPlan(models.Model):
    author = models.ForeignKey(User, models.SET_NULL, null=True)
    name = models.CharField(_('Name'), max_length=255)
    
    monday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                         related_name='monday_breakfast')
    monday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                             related_name='monday_morning_snack')
    monday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                     related_name='monday_lunch')
    monday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                               related_name='monday_afternoon_snack')
    monday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                      related_name='monday_dinner')

    tuesday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                          related_name='tuesday_breakfast')
    tuesday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                              related_name='tuesday_morning_snack')
    tuesday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                      related_name='tuesday_lunch')
    tuesday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                                related_name='tuesday_afternoon_snack')
    tuesday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                       related_name='tuesday_dinner')

    wednesday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                            related_name='wednesday_breakfast')
    wednesday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                                related_name='wednesday_morning_snack')
    wednesday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                        related_name='wednesday_lunch')
    wednesday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                                  related_name='wednesday_afternoon_snack')
    wednesday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                         related_name='wednesday_dinner')

    thursday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                           related_name='thursday_breakfast')
    thursday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                               related_name='thursday_morning_snack')
    thursday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                       related_name='thursday_lunch')
    thursday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                                 related_name='thursday_afternoon_snack')
    thursday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                        related_name='thursday_dinner')

    friday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                         related_name='friday_breakfast')
    friday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                             related_name='friday_morning_snack')
    friday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                     related_name='friday_lunch')
    friday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                               related_name='friday_afternoon_snack')
    friday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                      related_name='friday_dinner')

    saturday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                           related_name='saturday_breakfast')
    saturday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                               related_name='saturday_morning_snack')
    saturday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                       related_name='saturday_lunch')
    saturday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                                 related_name='saturday_afternoon_snack')
    saturday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                        related_name='saturday_dinner')

    sunday_breakfast = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                         related_name='sunday_breakfast')
    sunday_morning_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                             related_name='sunday_morning_snack')
    sunday_lunch = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                     related_name='sunday_lunch')
    sunday_afternoon_snack = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                               related_name='sunday_afternoon_snack')
    sunday_dinner = models.ForeignKey(Recipe, models.SET_NULL, blank=True, null=True,
                                      related_name='sunday_dinner')
    
    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name = _('Meal plan')
        verbose_name_plural = _('Meal plans')
        unique_together = ['author', 'name']
