from django.conf import settings
from django.db import models
from django.templatetags.static import static
from django.utils.translation import gettext_lazy as _
from solo.models import SingletonModel


class SiteConfiguration(SingletonModel):
    site_name = models.CharField(
        verbose_name=_("Site name"), max_length=255, default="Pommes de lune"
    )
    site_url = models.CharField(
        verbose_name=_("Site url"), max_length=255, default="https://pommesdelune.fr"
    )
    site_description = models.CharField(
        verbose_name=_("Site description"),
        max_length=255,
        default="Agenda participatif des sorties culturelles à Clermont-Ferrand et aux environs",
    )

    html_keywords = models.CharField(
        verbose_name=_("Keywords in html header"),
        max_length=1024,
        default="Clermont-Ferrand, Puy-de-Dôme, agenda culturel, agenda participatif, sortir à clermont, sorties, concerts, théâtre, danse, animations, ateliers, lectures",
    )
    html_description = models.CharField(
        verbose_name=_("Description in html header"),
        max_length=1024,
        default="Où sortir à Clermont-Ferrand? Retrouve tous les bons plans sur l'agenda participatif des événements culturels à Clermont-Ferrand et dans le Puy-de-Dôme",
    )
    google_site_verification = models.CharField(
        verbose_name=_("Google site verification value"),
        max_length=255,
        default=None,
        blank=True,
        null=True,
    )
    ms_site_verification = models.CharField(
        verbose_name=_("Microsoft (bing) site verification value"),
        max_length=255,
        default=None,
        blank=True,
        null=True,
    )

    favicon = models.ImageField(
        verbose_name=_("Illustration"),
        max_length=1024,
        blank=True,
        null=True,
    )
    favicon_dev = models.ImageField(
        verbose_name=_("Illustration (development version)"),
        max_length=1024,
        blank=True,
        null=True,
    )

    def guess_mimetype(url):
        if url.endswith("png"):
            return "image/png"
        if url.endswith("jpg"):
            return "image/jpg"
        if url.endswith("svg"):
            return "image/svg+xml"
        if url.endswith("ico"):
            return "image/x-icon"
        return "image/jpg"

    def get_favicon_url(self):
        if settings.DEBUG:
            if self.favicon_dev:
                return self.favicon_dev.url
            else:
                return static("images/pdl-64-dev.png")
        else:
            if self.favicon:
                return self.favicon.url
            else:
                return static("images/pdl-64.png")

    def get_favicon_mimetype(self):
        return SiteConfiguration.guess_mimetype(self.get_favicon_url())

    def __str__(self):
        return str(_("Site Configuration"))

    class Meta:
        verbose_name = _("Site Configuration")
