from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _, ngettext
from django.views.generic import ListView, UpdateView, CreateView, DeleteView
from django.shortcuts import render

from ..forms import SpecialPeriodFileForm, SpecialPeriodForm
from ..models import SpecialPeriod


class SpecialPeriodCreateView(
    PermissionRequiredMixin, LoginRequiredMixin, SuccessMessageMixin, CreateView
):
    model = SpecialPeriod
    permission_required = "agenda_culturel.add_specialperiod"
    success_message = _("The special period has been successfully created.")
    success_url = reverse_lazy("list_specialperiods")
    form_class = SpecialPeriodForm


class SpecialPeriodListView(PermissionRequiredMixin, LoginRequiredMixin, ListView):
    model = SpecialPeriod
    paginate_by = 10
    permission_required = "agenda_culturel.add_specialperiod"
    ordering = ["start_date", "name__unaccent"]


class SpecialPeriodDeleteView(
    SuccessMessageMixin,
    PermissionRequiredMixin,
    LoginRequiredMixin,
    DeleteView,
):
    model = SpecialPeriod
    permission_required = "agenda_culturel.delete_specialperiod"
    success_url = reverse_lazy("list_specialperiods")
    success_message = _("The special period has been successfully deleted.")


class SpecialPeriodUpdateView(
    SuccessMessageMixin,
    PermissionRequiredMixin,
    LoginRequiredMixin,
    UpdateView,
):
    model = SpecialPeriod
    permission_required = "agenda_culturel.change_specialperiod"
    success_message = _("The special period has been successfully updated.")
    success_url = reverse_lazy("list_specialperiods")
    form_class = SpecialPeriodForm


def load_specialperiods_from_ical(request):
    if request.method == "POST":
        form = SpecialPeriodFileForm(request.POST, request.FILES)
        if form.is_valid():
            nb_created, nb_overlap, nb_error, error = SpecialPeriod.load_from_ical(
                request.FILES["file"], request.POST["periodtype"]
            )

            if nb_created > 0:
                messages.success(
                    request,
                    ngettext(
                        "%(nb_created)d interval inserted.",
                        "%(nb_created)d intervals inserted.",
                        nb_created,
                    )
                    % {"nb_created": nb_created},
                )

            if nb_overlap > 0:
                messages.success(
                    request,
                    ngettext(
                        "%(nb_overlap)d insersion was not possible due to overlap.",
                        "%(nb_overlap)d insersion were not possible due to overlap.",
                        nb_overlap,
                    )
                    % {"nb_overlap": nb_overlap},
                )

            if nb_error > 0:
                messages.success(
                    request,
                    ngettext(
                        "%(nb_error)d error while reading ical file.",
                        "%(nb_error)d error while reading ical file.",
                        nb_error,
                    )
                    % {"nb_error": nb_error},
                )
            if error is not None:
                messages.success(
                    request, _("Error during file reading: {}").format(error)
                )

            return HttpResponseRedirect(reverse_lazy("list_specialperiods"))
    else:
        form = SpecialPeriodFileForm()
    return render(
        request, "agenda_culturel/load_specialperiods_from_ical.html", {"form": form}
    )
