# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from coin.isp_database.models import ISPInfo, RegisteredOffice, OtherWebsite, ChatRoom, CoveredArea


class SingleInstanceAdminMixin(object):
    """Hides the "Add" button when there is already an instance"""
    def has_add_permission(self, request):
        num_objects = self.model.objects.count()
        if num_objects >= 1:
            return False
        return super(SingleInstanceAdminMixin, self).has_add_permission(request)


class RegisteredOfficeInline(admin.StackedInline):
    model = RegisteredOffice
    extra = 0
    fields = (('street_address', 'extended_address', 'post_office_box'),
              ('postal_code', 'locality'),
              ('region', 'country_name'))


class OtherWebsiteInline(admin.StackedInline):
    model = OtherWebsite
    extra = 0


class ChatRoomInline(admin.StackedInline):
    model = ChatRoom
    extra = 0


class CoveredAreaInline(admin.StackedInline):
    model = CoveredArea
    extra = 0


class ISPInfoAdmin(SingleInstanceAdminMixin, admin.ModelAdmin):
    model = ISPInfo
    fieldsets = (
        ('General', {'fields': (
            ('name', 'shortname'),
            'description',
            'logoURL',
            ('creationDate', 'ffdnMemberSince'),
            'progressStatus',
            ('latitude', 'longitude'))}),
        ('Contact', {'fields': (
            ('email', 'mainMailingList'),
            'website')}),
    )

    inlines = (RegisteredOfficeInline, OtherWebsiteInline, ChatRoomInline,
               CoveredAreaInline)
    save_on_top = True


admin.site.register(ISPInfo, ISPInfoAdmin)
