import re
from django.views.generic import DetailView
from django.core.exceptions import ImproperlyConfigured


class TileMixin:
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if hasattr(self, "width"):
            context["width"] = self.width
        else:
            context["width"] = "25rem"
        name = self.model._meta.object_name
        context["tile_title"] = re.sub(r"((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))", r" \1", name).capitalize()
        return context


class FormDetailView(TileMixin, DetailView):
    """ Generic view that presents object detail as specified in a form """

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if hasattr(self, "Form"):
            context["form"] = self.Form(instance=self.object)
        elif hasattr(self, "form_class"):
            context["form"] = self.form_class(instance=self.object)
        else:
            raise ImproperlyConfigured("No form or form_class")
        if hasattr(self, "edit"):
            context["edit"] = [self.edit, self.object.id]
        if hasattr(self, "width"):
            context["width"] = self.width
        else:
            context["width"] = "25rem"
        if hasattr(self, "title"):
            pass
        return context
