import datetime

from django.shortcuts import render
from django.utils.timezone import now, localtime
from rest_framework import authentication, permissions, status, viewsets
import rest_framework.generics
from rest_framework.response import Response
from rest_framework.views import APIView

import recipe.models
import recipe.serializers
from utils import const

class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView):

  # authentication_classes = (authentication.TokenAuthentication,
  #                           authentication.SessionAuthentication,
  #                           authentication.BasicAuthentication)
  # permission_classes = (permissions.IsAuthenticated,)
  queryset = recipe.models.Recipe.objects.all()
  serializer_class = recipe.serializers.RecipeSerializer


class RecipeListAPI(rest_framework.generics.ListAPIView,
                    rest_framework.generics.CreateAPIView):

  # authentication_classes = (authentication.TokenAuthentication,
  #                           authentication.SessionAuthentication,
  #                           authentication.BasicAuthentication)
  # permission_classes = (permissions.IsAuthenticated,)
  queryset = recipe.models.Recipe.objects.all()
  serializer_class = recipe.serializers.RecipeSerializer
  filterset_fields = {
    'recipe_type': const.FILTER_EXACT,
    'difficulty': const.FILTER_GTE_LTE,
    'rate': const.FILTER_GTE_LTE,
  }


class WeekRecipeListAPI(rest_framework.generics.ListAPIView,
                        rest_framework.generics.CreateAPIView):

  queryset = recipe.models.DailyRecipe.objects.all()
  serializer_class = recipe.serializers.WeekRecipeSerializer

  def post(self, request, *args, **kwargs):
    # Monday == 0 ... Sunday == 6
    today = localtime()
    recipes = []
    for x in range(0, 7-today.weekday()):
      daily_recipe, __ = recipe.models.DailyRecipe.objects.get_or_create(
        date=today + datetime.timedelta(days=x)
      )
      daily_recipe.generate_recipe(recipes)
      recipes.extend(daily_recipe.recipes.values_list('id', flat=True))
    return Response(recipe.models.DailyRecipe.get_week_recipe_data(),
                    status=status.HTTP_201_CREATED, headers={})

  def get(self, request, *args, **kwargs):
    data = recipe.models.DailyRecipe.get_week_recipe_data()
    return Response(data)


class DailyRecipeAPI(rest_framework.generics.RetrieveUpdateAPIView):

  queryset = recipe.models.DailyRecipe.objects.all()
  serializer_class = recipe.serializers.DailyRecipeSerializer

  def post(self, request, *args, **kwargs):
    daily_recipe = recipe.models.DailyRecipe.objects.get(id=kwargs['pk'])
    daily_recipe.generate_recipe()
    return Response(daily_recipe.serialize(), status=status.HTTP_201_CREATED,
                    headers={})

  def put(self, request, *args, **kwargs):
    daily_recipe = recipe.models.DailyRecipe.objects.get(id=kwargs['pk'])
    recipes = request.data.get('meat',[])
    recipes.extend(request.data.get('vegetable', []))
    recipes.extend(request.data.get('soup', []))
    daily_recipe.recipes.set(recipe.models.Recipe.objects.filter(
      id__in=recipes))
    return Response(daily_recipe.serialize(), status=status.HTTP_201_CREATED,
                    headers={})
