from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin
from locations.models import Region, Country, Location, State, Route, CheckIn
from utils.widgets import OLAdminBase

from utils.util import get_latlon

class RegionAdmin(OSMGeoAdmin):
    list_display = ('name', 'slug')
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ('name',)
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    fieldsets = (
        ('Region', {
            'fields': (
                'name',
                'slug',
                'pub_date'
            ),
            'classes': (
                'show',
                'extrapretty'
            )
        }),
        ('Editable Map View', {
            'fields': ('geometry',),
            'classes': (
                'show',
                'wide'
            )
        }),
    )

    # options for OSM map Using custom ESRI topo map
    default_lon = -9285175
    default_lat = 4025046
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'

admin.site.register(Region, RegionAdmin)


class CountryAdmin(OSMGeoAdmin):
    list_display = ('name', 'pop2005', 'region', 'subregion')
    search_fields = ('name',)
    ordering = ('name',)
    list_filter = ('visited', 'region', 'subregion')
    save_as = True
    search_fields = ['name', 'iso2', 'iso3', 'subregion', 'region']
    list_select_related = True
    fieldsets = (
        ('Country Attributes', {
            'fields': (
                'name',
                'pop2005',
                'slug',
                'zoom_level',
                'visited'
            ),
            'classes': (
                'show',
                'extrapretty'
            )
        }),
        ('Country Codes', {
            'fields': (
                'region',
                'subregion',
                'iso2',
                'iso3',
                'un',
            ),
            'classes': ('collapse',)
        }),
        ('Area and Coordinates', {
            'fields': (
                'area',
                'lat',
                'lon',
            ),
            'classes': (
                'collapse',
                'wide'
            )
        }),
        ('Editable Map View', {
            'fields': ('geometry',),
            'classes': (
                'show',
                'wide'
            )
        }),
    )

    # Options for OSM map Using custom ESRI topo map
    default_lon = -9285175
    default_lat = 4025046
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'


admin.site.register(Country, CountryAdmin)


class StateAdmin(OSMGeoAdmin):
    list_display = ('name', 'code', 'slug', 'country')
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ('name', 'country')
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    fieldsets = (
        ('Location', {
            'fields': (
                'name',
                'slug',
                'code',
                'pub_date',
                'country'
            ),
            'classes': (
                'show',
                'extrapretty'
            )
        }),
        ('Editable Map View', {
            'fields': ('geometry',),
            'classes': (
                'show',
                'wide'
            )
        }),
    )

    # Options for OSM map Using custom ESRI topo map
    default_lon = -9285175
    default_lat = 4025046
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'

admin.site.register(State, StateAdmin)


class LocationAdmin(OSMGeoAdmin):
    list_display = ('name', 'slug', 'state', 'pub_date')
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ('name', 'state')
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    fieldsets = (
        ('Location', {
            'fields': (
                'name',
                'slug',
                'pub_date',
                'state'
            ),
            'classes': ('show', 'extrapretty')
        }),
        ('Editable Map View', {
            'fields': ('geometry',),
            'classes': ('show', 'wide')
        }),
    )
    # options for OSM map Using custom ESRI topo map
    default_lat, default_lon = get_latlon()
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'

    class Media:
        js = ('next-prev-links.js',)

admin.site.register(Location, LocationAdmin)


class RouteAdmin(OSMGeoAdmin):
    list_display = ('name', 'slug')
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ('name',)
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    fieldsets = (
        ('Location', {
            'fields': (
                'name',
                'slug',
                'pub_date',
                'template_var_name',
                'zoom'
            ),
            'classes': ('show', 'extrapretty')
        }),
        ('Editable Map View', {
            'fields': ('geometry',),
            'classes': ('show', 'wide')
        }),
    )

    # options for OSM map Using custom ESRI topo map
    default_lon = -9285175
    default_lat = 4025046
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'

admin.site.register(Route, RouteAdmin)


@admin.register(CheckIn)
class CheckInAdmin(OLAdminBase):
    list_display = ('date', 'location')
    # options for OSM map Using custom ESRI topo map
    default_lat, default_lon = get_latlon()
    default_zoom = 9