from django.db import models
from admin_ordering.models import OrderableModel
from filebrowser.fields import FileBrowseField
from django.urls.base import reverse_lazy

from db.calendar.models import Calendar
from db.membership.models import Membership

# Create your models here.


class Button(OrderableModel):
    published = models.BooleanField('Przycisk opublikowany', default=True)
    title = models.CharField('Tekst na przycisku', max_length=50)
    href = models.CharField('Link', max_length=50)
    blank = models.BooleanField('Otwórz w nowej karcie')

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

    @property
    def link(self):
        h = self.href
        c = None

        if h == reverse_lazy('calendar'):
            c = Calendar.get_current()
        elif h in reverse_lazy('membership') and h != '/':
            c = Membership.get_current()

        return h + (('#' + str(c.year)) if c is not None else '')

    class Meta(OrderableModel.Meta):
        verbose_name = 'Przycisk nawigacji (zielony)'
        verbose_name_plural = 'Przyciski nawigacji (zielone)'


class UpperButton(OrderableModel):
    published = models.BooleanField('Przycisk opublikowany', default=True)
    title = models.CharField('Tytuł przycisku', max_length=50)
    photo = FileBrowseField(
        'Ikona', directory='gorneprzyciski/', max_length=200, blank=True)
    href = models.CharField('Link', max_length=50)
    blank = models.BooleanField('Otwórz w nowej karcie')

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

    class Meta(OrderableModel.Meta):
        verbose_name = 'Przycisk górny (łabs, atu, itd...)'
        verbose_name_plural = 'Przyciski górne (łabs, atu, itd...)'
