from django.shortcuts import Http404, HttpResponse, get_object_or_404, render
from django.utils.translation import get_language
from django.views.generic import TemplateView, DetailView, ListView, View
from .models import BlogPost, Category

from start.views import SearchableView

import logging

logger = logging.getLogger(__name__)

class Index(TemplateView, SearchableView):
    """
    The index page of the gawa/blog app.

    Utilizes a generic view, because I will do so for all views, 
    a regular view function would suffice.
    """

    template_name: str = "blog/index.html"

class Post(DetailView):
    """
    Main page of a blog post
    """

    model=BlogPost
    template_name = "blog/blogpost.html"
    context_object_name = "post"

    def get_object(self, queryset=None):
        obj = get_object_or_404(
                BlogPost,
                category__slug=self.kwargs['category'], # first slug is category
                slug=self.kwargs['slug']    # second slug is article itself
                )
        logger.debug(obj)
        match get_language():
            case 'de':
                obj['title'] = obj['title_de']

            case 'en':
                obj['title'] = obj['title_en']

            case _:
                # this should not happen, but who knows what dumb stuff users will come up with
                logger.warning("article for unsupported language was requested")
                return obj

class CategoryList(ListView):
    """
    Scroll through a list of blog Categories
    """

    model=Category
    template_name = "blog/categories.html"
    context_object_name = "categories"

class ArticleList(ListView):
    """
    Scroll through a list of blog posts

    There should also be some kind of filter, for category and date.
    Of course, Articles will also show up in the MainSearchView
    """

    model=BlogPost
    template_name = "blog/posts.html"
    context_object_name = "posts"
