from django.db import models
from django.urls.base import reverse_lazy
from django.utils.safestring import mark_safe
from admin_ordering.models import OrderableModel
from tinymce.models import HTMLField
from filebrowser.fields import FileBrowseField
from db.help import buttons_help_text

# Create your models here.



class Tournament(OrderableModel):
    published = models.BooleanField('Wpis opublikowany', default=True)
    show_title = models.BooleanField('Pokaż tytuł', default=True)
    title = models.CharField('Tytuł', default='', blank=True, max_length=250)
    photo = FileBrowseField(
        'Zdjęcie', directory='inneturnieje/', max_length=200, blank=True)
    content = HTMLField('Tekst', default='', blank=True)
    buttons = models.TextField(
        'Przyciski', default='', blank=True, help_text=buttons_help_text)

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

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

    class Meta(OrderableModel.Meta):
        verbose_name = 'Inny turniej'
        verbose_name_plural = 'Inne turnieje'
