from django.db import models, transaction
from django.urls.base import reverse_lazy
from django.utils.safestring import mark_safe
from tinymce.models import HTMLField
from admin_ordering.models import OrderableModel
from core.utils import SingletonModel
from db.help import buttons_help_text

# Create your models here.


class Post(OrderableModel):
    published = models.BooleanField('Wpis opublikowany', default=False)
    show_title = models.BooleanField('Pokaż tytuł', default=True)
    title = models.CharField('Tytuł', default='', blank=True, max_length=250)
    content = HTMLField('Treść', default='', blank=True)
    buttons = models.TextField(
        'Przyciski', default='', blank=True, help_text=buttons_help_text)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title or '(brak tytułu)'

    @property
    def link(self):
        href = reverse_lazy('home-reverse', args=[self.id])
        return mark_safe(f'<a href="{href}" target="_blank">{href}</a>')

    class Meta(OrderableModel.Meta):
        verbose_name = 'Aktualność PodlZBS'
        verbose_name_plural = 'Aktualności PodlZBS'


class GrandPrixW(OrderableModel):
    id = models.AutoField(primary_key=True)
    published = models.BooleanField('Grand Prix opublikowane', default=False)
    title = models.CharField('Tytuł', default='', blank=True, max_length=250)
    show_title = models.BooleanField('Pokaż tytuł', default=True)
    content = HTMLField('Tekst GPW', default='', blank=True)
    buttons = models.TextField(
        'Przyciski', default='', blank=True, help_text=buttons_help_text)

    @staticmethod
    def get_current():
        return GrandPrixW.objects.filter(published=True)

    def __str__(self):
        return self.title or '-'

    @property
    def link(self):
        href = reverse_lazy('gpw-reverse', args=[self.id])
        return mark_safe(f'<a href="{href}" target="_blank">{href}</a>')

    class Meta(OrderableModel.Meta):
        verbose_name = 'Grand Prix Województwa'
        verbose_name_plural = 'Grand Prix Województwa'

class PublishableModel(models.Model):
    published = models.BooleanField("Wpis opublikowany", default=False)

    @classmethod
    def Published(cls):
        return cls.objects.filter(published=True)
    
    class Meta():
        abstract = True


class SingletonModel(PublishableModel):
    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if not self.published:
            return super().save(*args, **kwargs)
        with transaction.atomic():
            self.__class__.objects.filter(published=True).update(published=False)
            return super().save(*args, **kwargs)

    @classmethod
    def Get(cls):
        return cls.objects.filter(published=True).first()


class PostableModel(PublishableModel, OrderableModel):
    reverse_href = ""
    show_title = models.BooleanField("Pokaż tytuł", default=True)
    title = models.CharField("Tytuł", default="", blank=True, max_length=250)
    content = HTMLField("Treść", default="", blank=True)
    buttons = models.TextField(
        "Przyciski", default="", blank=True, help_text=buttons_help_text
    )
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title or "(brak tytułu)"

    class Meta(OrderableModel.Meta):
        abstract = True

class Footer(PostableModel, SingletonModel):

    class Meta(OrderableModel.Meta):
        verbose_name = "Stopka"
        verbose_name_plural = "Stopki"

