from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseForbidden
from django.shortcuts import render
from django.views.generic import CreateView, ListView, DetailView, DeleteView
from django.urls import reverse, reverse_lazy

from .forms import FoodImageForm
from .models import FoodImage
from users.models import CustomUser


class FoodImageUploadView(CreateView):
    model = FoodImage
    form_class = FoodImageForm
    template_name = "home.html"

    def get_success_url(self):
        return reverse_lazy(
            "food-image-detail-view", kwargs={"pk": self.object.pk}
        )

    def form_valid(self, form):
        if self.request.user.is_authenticated:
            form.instance.owner = self.request.user
        return super(FoodImageUploadView, self).form_valid(form)


class FoodImageListView(LoginRequiredMixin, ListView):
    model = FoodImage
    #  paginate_by = 50
    # TODO: set up pagination
    ordering = ["-date_uploaded"]
    template_name = "list.html"


class FoodImageDetailView(DetailView):
    model = FoodImage
    template_name = "detail.html"

    def get_object(self, queryset=None):
        return FoodImage.objects.get(reference_id=self.kwargs.get("pk"))


class FoodImageDeleteView(DeleteView):
    model = FoodImage
    success_url = reverse_lazy("food-image-list-view")
    template_name = "delete.html"

    def get(self, request, *args, **kwargs):
        if (
            request.user.is_staff
            or request.user.is_superuser
            or FoodImage.objects.get(pk=self.kwargs.get("pk")).owner
            == request.user
        ):
            self.object = self.get_object()
            return render(
                request, self.template_name, context=self.get_context_data()
            )
        else:
            return HttpResponseForbidden(render(request, "403.html"))

    def post(self, request, *args, **kwargs):
        if (
            request.user.is_staff
            or request.user.is_superuser
            or FoodImage.objects.get(pk=self.kwargs.get("pk")).owner
            == request.user
        ):
            self.object = self.get_object()
            return self.delete(request, *args, **kwargs)
        else:
            return HttpResponseForbidden(render(request, "403.html"))

    def get_object(self, queryset=None):
        return FoodImage.objects.get(pk=self.kwargs.get("pk"))


class FoodImageUserListView(ListView):
    model = CustomUser
    template_name = "user_list.html"

    def get_context_data(self, **kwargs):
        context = super(FoodImageUserListView, self).get_context_data(**kwargs)
        context["user_details"] = CustomUser.objects.get(
            username=self.kwargs.get("username")
        )
        context["user_upload_total"] = FoodImage.objects.filter(
            owner=CustomUser.objects.get(username=self.kwargs.get("username"))
        ).count()

        return context

    def get_queryset(self):
        return FoodImage.objects.filter(
            owner=CustomUser.objects.get(username=self.kwargs.get("username"))
        )
