from django.conf.urls import patterns, url
from django.contrib import admin
from django.core import urlresolvers
from adminwizard.admin import AdminWizard
from . import forms
from . import models
from . import views
from . import wizard_cpe2


class RouterConfigInline(admin.TabularInline):
    model = models.Router
    readonly_fields = ['name', 'model', 'configuration']
    fields = readonly_fields
    can_delete = False

    def has_add_permission(self, request):
        return False

    def configuration(self, obj):
        if not obj.pk:
            return ''
        info = (obj._meta.app_label, obj._meta.model_name)
        url_name = 'admin:%s_%s_configuration' % info
        config_url = urlresolvers.reverse(url_name, args=(obj.pk,))
        return '<a href="%s">%s</a>' % (config_url, obj)


class PhysicalLinkConfigInline(admin.TabularInline):
    model = models.PhysicalLink
    readonly_fields = ('router_interface_1', 'router_interface_2')
    fields = readonly_fields
    can_delete = False

    def has_add_permission(self, request):
        return False


@admin.register(models.Project)
class ProjectAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_filter = ['wizard']
    fieldsets = [
        ['', dict(
            fields=[('name', 'wizard')],
        )],
        ['Wizard configuration', dict(
            classes=['collapse'],
            fields=['configuration'],
        )],
    ]
    inlines = [
        RouterConfigInline,
        PhysicalLinkConfigInline,
    ]

    def has_add_permission(self, request):
        return False


@admin.register(models.ProjectCpe2)
class ProjectCpe2Admin(AdminWizard):
    search_fields = ['name']
    exclude = ['wizard']
    save_on_top = True
    form = forms.ProjectForm
    wizard_view = wizard_cpe2.ProjectWizardView


class RouterInline(admin.TabularInline):
    model = models.Router
    readonly_fields = ['url', 'configuration']
    fields = ['url', 'name', 'model', 'configuration']
    # show_change_link will work with django 1.8
    show_change_link = True
    extra = 0

    def url(self, obj):
        return '<a href="%s">%s</a>' % (obj.get_url(), obj)

    def configuration(self, obj):
        if not obj.pk:
            return ''
        info = (obj._meta.app_label, obj._meta.model_name)
        url_name = 'admin:%s_%s_configuration' % info
        config_url = urlresolvers.reverse(url_name, args=(obj.pk,))
        return '<a href="%s">%s</a>' % (config_url, obj)


class PhysicalLinkInline(admin.TabularInline):
    model = models.PhysicalLink
    extra = 0

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        field = super(PhysicalLinkInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.name in ('router_interface_1', 'router_interface_2'):
            if request.obj:
                field.queryset = field.queryset.filter(router__project__pk=request.obj.pk)
            else:
                field.queryset = field.queryset.none()
        return field


@admin.register(models.ProjectCustom)
class ProjectCustomAdmin(admin.ModelAdmin):
    fields = ['name']
    inlines = [
        RouterInline,
        PhysicalLinkInline,
    ]

    def has_add_permission(self, request):
        return False

    def get_form(self, request, obj=None, **kwargs):
        request.obj = obj
        return super(ProjectCustomAdmin, self).get_form(request, obj, **kwargs)


class VrfInline(admin.TabularInline):
    model = models.Vrf
    readonly_fields = ('delete_vrf_link',)
    can_delete = False
    extra = 0


class VlanInline(admin.TabularInline):
    model = models.Vlan
    form = forms.VlanForm
    extra = 0


class PhysicalInterfaceInline(admin.TabularInline):
    model = models.PhysicalInterface
    form = forms.PhysicalInterfaceForm
    readonly_fields = ['url']
    fields = ['url', 'name', 'description', 'type', 'layer', 'dot1q_mode', 'native_vlan']
    # show_change_link will work with django 1.8
    show_change_link = True
    extra = 0

    def url(self, obj):
        return '<a href="%s">%s</a>' % (obj.get_url(), obj.name)


@admin.register(models.Router)
class RouterAdmin(admin.ModelAdmin):
    readonly_fields = ['project']
    change_form_template = 'genconf/router/change_form.html'
    fieldsets = [
        ['', dict(
            fields=[('project', 'name', 'model')],
        )],
    ]
    inlines = [
        VrfInline,
        VlanInline,
        PhysicalInterfaceInline,
    ]

    def get_queryset(self, request):
        qs = super(RouterAdmin, self).get_queryset(request)
        return qs.filter(project__wizard='')

    def get_urls(self):
        info = self.model._meta.app_label, self.model._meta.model_name
        urls = patterns('',
            url(r'^(?P<pk>\d+)/configuration/$',
                self.admin_site.admin_view(views.ConfigurationView.as_view()),
                name='%s_%s_configuration' % info),
        )
        return urls + super(RouterAdmin, self).get_urls()

    def has_add_permission(self, request):
        return False


@admin.register(models.Vrf)
class VrfAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        if obj and not obj.name:
            return False
        return True


class SubInterfaceInline(admin.TabularInline):
    model = models.SubInterface
    form = forms.SubInterfaceForm
    fields = ('change', 'name', 'description', 'layer_3_interface', 'ipnetwork', 'vrf')
    readonly_fields = ['change']
    # show_change_link will work with django 1.8
    show_change_link = True
    extra = 0

    def change(self, obj):
        return '<a href="%s">%s</a>' % (obj.get_url(), obj.name)


@admin.register(models.PhysicalInterface)
class PhysicalInterfaceAdmin(admin.ModelAdmin):
    list_display = ['project', 'router', 'name', 'description', 'type', 'layer', 'mtu']
    list_display_links = ['name']
    form = forms.PhysicalInterfaceForm
    readonly_fields = ['router']
    fieldsets = (
        (None, dict(
            fields=(
                ('router', 'name', 'description'),
                ('type', 'layer', 'mtu'),
            ),
        )),
        ('Ethernet', dict(
            fields=(
                ('dot1q_mode', 'native_vlan', 'dot1q_encapsulation', 'duplex', 'speed'),
            ),
        )),
    )
    inlines = [
        SubInterfaceInline,
    ]

    def get_queryset(self, request):
        qs = super(PhysicalInterfaceAdmin, self).get_queryset(request)
        return qs.filter(router__project__wizard='')


@admin.register(models.SubInterface)
class SubInterfaceAdmin(admin.ModelAdmin):
    form = forms.SubInterfaceForm
    readonly_fields = ['physical_interface']
    fieldsets = (
        (None, dict(
            fields=(
                ('physical_interface', 'name', 'description',),
                ('layer_3_interface', 'ipnetwork', 'vrf'),
            ),
        )),
        ('Ethernet', dict(
            fields=(
                ('layer', 'vlan'),
            ),
        )),
        ('Atm', dict(
            fields=(
                ('link', 'pvc_vp', 'pvc_vc',),
                ('pvc_encapsulation', 'pvc_mux', 'pvc_dialer_pool_number',),
            ),
        )),
    )

    def get_queryset(self, request):
        qs = super(SubInterfaceAdmin, self).get_queryset(request)
        return qs.filter(physical_interface__router__project__wizard='')


@admin.register(models.PhysicalLink)
class PhysicalLinkAdmin(admin.ModelAdmin):
    readonly_fields = ['project', 'router_interface_1', 'router_interface_2']

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False
