from datetime import date, timedelta
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import (
    CreateView, DeleteView, ListView, TemplateView, UpdateView
)

from .forms import MembreEditForm
from .models import Agenda, Document, Membre


class HomeView(TemplateView):
    template_name="index.html"

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "items": Agenda.objects.filter(
                date_heure__date__gte=date.today(), prive=False,
            ).order_by("date_heure")[:2],
        }


class ManifestView(TemplateView):
    template_name = "manifest.json"
    content_type = "application/json"


class PresentationView(TemplateView):
    template_name="presentation.html"

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "next_event": Agenda.objects.filter(
                date_heure__date__gte=date.today(), prive=False,
            ).order_by("date_heure").first(),
        }


class AgendaView(ListView):
    model = Agenda
    template_name = "agenda.html"

    def get_queryset(self):
        qs = Agenda.objects.filter(
            date_heure__gt=date.today() - timedelta(days=3),
        ).order_by("date_heure")
        if not self.request.user.is_authenticated:
            qs = qs.filter(prive=False)
        return qs


class MediaView(ListView):
    model = Document
    template_name = "media.html"
    prive = False

    def get_queryset(self):
        return Document.objects.filter(prive=self.prive).order_by("-quand", "titre")

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "title": {
                False: "Enregistrements, photos et vidéos de la chorale",
                True: "Documents privés",
            }.get(self.prive)
        }


class EspaceMembresView(LoginRequiredMixin, TemplateView):
    template_name = "membres/index.html"


class ListeMembresView(LoginRequiredMixin, ListView):
    model = Membre
    template_name = "membres/liste.html"

    def get_queryset(self):
        return super().get_queryset().order_by("nom")


class MembreAddView(PermissionRequiredMixin, CreateView):
    model = Membre
    form_class = MembreEditForm
    permission_required = "beesgospel.add_membre"
    template_name = "membres/edit.html"
    success_url = reverse_lazy("liste-membres")


class MembreEditView(PermissionRequiredMixin, UpdateView):
    model = Membre
    form_class = MembreEditForm
    permission_required = "beesgospel.change_membre"
    template_name = "membres/edit.html"
    success_url = reverse_lazy("liste-membres")


class MembreDeleteView(PermissionRequiredMixin, DeleteView):
    model = Membre
    permission_required = "beesgospel.delete_membre"
    success_url = reverse_lazy("liste-membres")

    def form_valid(self, form):
        self.object.user.delete()
        return super().form_valid(form)
