import logging
logger = logging.getLogger(__name__)

from django.contrib import admin

from sorl.thumbnail.admin import AdminImageMixin
from sorl.thumbnail.shortcuts import get_thumbnail

from .models import (
    Project, IdeaPhase, FundPhase, ActPhase, ResultsPhase, BudgetLine,
    Testimonial, Message, ProjectTheme, PartnerOrganization
)


class IdeaPhaseInline(admin.StackedInline):
    model = IdeaPhase
    can_delete = False


class BudgetInline(admin.TabularInline):
    model = BudgetLine


class FundPhaseInline(admin.StackedInline):
    model = FundPhase


class ActPhaseInline(admin.StackedInline):
    model = ActPhase


class ResultsPhaseInline(admin.StackedInline):
    model = ResultsPhase


class ProjectAdmin(AdminImageMixin, admin.ModelAdmin):
    date_hierarchy = 'created'

    prepopulated_fields = {"slug": ("title",)}

    list_filter = ('phase', 'project_language', 'themes', 'country')
    list_display = ('title', 'organization', 'country')
    # TODO: decide if we want to have thumbnails here
    #list_display = ('thumbnail', 'title', 'organization', 'country')
    #list_display_links = ('thumbnail', 'title')
    
    inlines = [
        BudgetInline, IdeaPhaseInline,
        FundPhaseInline, ActPhaseInline, ResultsPhaseInline
    ]

    filter_horizontal = ('themes', 'albums')

    search_fields = (
        'title', 'organization__name',
        'owner__first_name', 'owner__last_name'
    )

    def thumbnail(self, instance):
        """
        Generate a nice little thumbnail for our project.

        Source: https://github.com/sorl/sorl-thumbnail/blob/master/sorl/thumbnail/admin/current.py#L19
        """
        value = '<div style="height:80px;width:80px">%s</div>'

        if instance.image:
            try:
                mini = get_thumbnail(
                    instance.image, '80x80', upscale=False, crop='center'
                )
            except Exception:
                logger.exception(
                    "An error occurred while scaling an image in the admin."
                )
                return value % ''
            else:
                # We might need this later, for explicitly linking and/or
                # formatting of the image and/or link.
                #
                # output = (
                #     u'<div style="float:left">'
                #     u'<a style="width:%spx;display:block;margin:0 0 10px" class="thumbnail" target="_blank" href="%s">'
                #     u'<img src="%s"></a>%s</div>'
                #     ) % (mini.width, value.url, mini.url, output)
                return value % (u'<img width="%s" height="%s" src="%s">' \
                    % (mini.width, mini.height, mini.url))
        else:
            return value % ''
    thumbnail.allow_tags = True

admin.site.register(Project, ProjectAdmin)
admin.site.register(Message)
admin.site.register(Testimonial)


class PartnerOrganizationAdmin(AdminImageMixin, admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}

admin.site.register(PartnerOrganization, PartnerOrganizationAdmin)


class ProjectThemeAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}

admin.site.register(ProjectTheme, ProjectThemeAdmin)

@@ -344,20 +344,16 @@
 %0A%0Aclass 
-Idea
 PhaseInl
@@ -347,32 +347,36 @@
 lass PhaseInline
+Base
 (admin.StackedIn
@@ -391,88 +391,72 @@
    m
-odel = IdeaPhase%0A    can_delete = False%0A%0A%0Aclass BudgetInline(admin.Tabular
+ax_num = 1%0A    extra = 0%0A%0A%0Aclass IdeaPhaseInline(Phase
 Inline
+Base
 ):%0A 
@@ -466,25 +466,47 @@
 model = 
-BudgetLin
+IdeaPhase%0A    can_delete = Fals
 e%0A%0A%0Aclas
@@ -523,35 +523,31 @@
 eInline(
-admin.Stacked
+Phase
 Inline
+Base
 ):%0A    m
@@ -586,35 +586,31 @@
 eInline(
-admin.Stacked
+Phase
 Inline
+Base
 ):%0A    m
@@ -652,35 +652,31 @@
 eInline(
-admin.Stacked
+Phase
 Inline
+Base
 ):%0A    m
@@ -693,24 +693,90 @@
 ultsPhase%0A%0A%0A
+class BudgetInline(admin.TabularInline):%0A    model = BudgetLine%0A%0A%0A
 class Projec
@@ -1196,20 +1196,16 @@
 title')%0A
-    
 %0A    inl

Allow adding exactly one phase of each. Fixes #5.