from django.contrib import admin
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView

from aircox.controllers.log_archiver import LogArchiver
from .log import LogListView

__all__ = ["AdminMixin", "StatisticsView"]


class AdminMixin(LoginRequiredMixin, UserPassesTestMixin):
    title = ""
    init_app = True
    """If true, create vue app."""

    @property
    def station(self):
        return self.request.station

    def test_func(self):
        return self.request.user.is_staff

    def get_context_data(self, **kwargs):
        kwargs.update(admin.site.each_context(self.request))
        kwargs.setdefault("title", self.title)
        kwargs.setdefault("station", self.station)
        kwargs.setdefault("init_app", self.init_app)
        return super().get_context_data(**kwargs)


class StatisticsView(AdminMixin, LogListView, ListView):
    template_name = "admin/aircox/statistics.html"
    redirect_date_url = "admin:tools-stats"
    title = _("Statistics")
    date = None

    def get_object_list(self, logs, full=False):
        if not logs.exists():
            logs = LogArchiver().load(self.station, self.date) if self.date else []
        return super().get_object_list(logs, True)
