from django.http import HttpResponse
from django.shortcuts import redirect, render
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer
from rest_framework.response import Response

from vbv_lernwelt.simpletodo.models import SimpleList, SimpleTask
from vbv_lernwelt.simpletodo.serializers import SimpleTaskSerializer


def index(request):
    simple_lists = SimpleList.objects.filter(user=request.user)

    if simple_lists.count() == 0:
        simple_lists = [SimpleList.objects.create(user=request.user, title="Todos")]

    return render(request, "simpletodo/index.html", {"simple_lists": simple_lists})


class SimpleTaskViewSet(viewsets.ModelViewSet):
    serializer_class = SimpleTaskSerializer
    renderer_classes = [TemplateHTMLRenderer, JSONRenderer]

    def get_queryset(self):
        user = self.request.user
        return SimpleTask.objects.filter(list__user=user)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)

        if request.accepted_renderer.format == "html":
            serializer.is_valid(raise_exception=True)
        else:
            serializer.is_valid(raise_exception=True)

        serializer.save(user=request.user)

        if request.accepted_renderer.format == "html":
            return redirect("/todo/")

        headers = self.get_success_headers(serializer.data)
        return Response(
            serializer.data, status=status.HTTP_201_CREATED, headers=headers
        )

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.delete()

        if request.htmx:
            return HttpResponse(status=200, content="")

        return Response(status=status.HTTP_204_NO_CONTENT)

    @action(
        detail=True,
        methods=[
            "post",
        ],
    )
    def toggle_done(self, request, pk=None):
        task = self.get_object()
        task.done = not task.done
        task.save()

        if request.htmx:
            return render(request, "simpletodo/partials/task.html", {"task": task})

        return Response(self.get_serializer(task), status=status.HTTP_200_OK)

    #
    # def get_category_from_request(self, request):
    #     cat_name = request.query_params.get('cat_name')
    #     category_obj = None
    #
    #     if cat_name:
    #         category_obj = VideoCategory.objects.filter(category__iexact=cat_name).first()
    #     if not category_obj:
    #         category_obj = VideoCategory.objects.first()
    #
    #     return category_obj
    #
    # @action(detail=False, methods=['get'])
    # def form(self, request):
    #     category_obj = self.get_category_from_request(request)
    #     return Response(template_name='videos/partials/video_form.html', data={'category': category_obj})
    #
    # @action(detail=False, methods=['get'])
    # def cancel(self, request):
    #     category_obj = self.get_category_from_request(request)
    #     return Response(template_name='videos/partials/show_add_form.html', data={'category': category_obj})
