from django.db import models
from django.utils.translation import gettext_lazy as _
from filer.fields.file import FilerFileField
from fluent_contents.models.db import ContentItem


class FilerFileItem(ContentItem):

    file = FilerFileField(verbose_name=_("file"), on_delete=models.CASCADE)
    name = models.CharField(_("name"), max_length=255, null=True, blank=True)

    target = models.CharField(
        _("target"),
        blank=True,
        max_length=100,
        choices=(
            (
                ("", _("same window")),
                ("_blank", _("new window")),
                ("_parent", _("parent window")),
                ("_top", _("topmost frame")),
            )
        ),
        default="",
    )

    class Meta:
        verbose_name = _("File")
        verbose_name_plural = _("Files")

    @property
    def filename(self):
        if self.file.name in ("", None):
            return self.file.original_filename
        return self.file.name

    def __str__(self):
        if self.name:
            return self.name
        elif self.file:
            return str(self.filename)
        return "<empty>"
