from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin

from .models import Region, Country, Location, State, Route, LuxCheckIn, Campsite, GPXFile, GPXTrack, Track

from utils.widgets import OLAdminBase
from utils.util import get_latlon
from utils.widgets import LGEntryForm


@admin.register(Region)
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.register(Country)
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',
                'lux_region'
            ),
            '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.register(State)
class StateAdmin(OSMGeoAdmin):
    list_display = ('name', 'code', 'slug', 'country', 'pub_date')
    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.register(Location)
class LocationAdmin(OSMGeoAdmin):
    list_display = ('name', 'pub_date', 'parent', 'state', 'slug')
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ('name', 'state')
    list_filter = ('pub_date', 'state__country__lux_region', 'state')
    save_as = True
    search_fields = ['name']
    list_select_related = True
    fieldsets = (
        ('Location', {
            'fields': (
                'name',
                'slug',
                'pub_date',
                'state',
                'parent'
            ),
            '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.register(Route)
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.register(LuxCheckIn)
class LuxCheckInAdmin(OLAdminBase):
    list_display = ('date', 'location')
    # options for OSM map Using custom ESRI topo map
    default_lat, default_lon = get_latlon()
    default_zoom = 9


@admin.register(Campsite)
class CampsiteAdmin(OLAdminBase):
    form = LGEntryForm
    list_display = ('name', 'location', 'date_arrived', 'date_left', 'campsite_type', 'total_price', 'nights_stayed')
    list_filter = ('date_arrived', 'campsite_type')
    search_fields = ['location__name', ]
    fieldsets = (
        ('Campsite', {
            'fields': (
                'name',
                ('date_arrived', 'date_left'),
                'campsite_type',
                'body_markdown',
                'point',
                'campsite_price',
                'campsite_number',
                'campsite_we_wish_we_had',
            ),
            'classes': (
                'show',
                'extrapretty',
                'wide'
            )
        }
        ),
    )

    default_lat, default_lon = get_latlon()
    default_zoom = 9

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


@admin.register(GPXFile)
class GPXFileAdmin(OLAdminBase):
    pass


@admin.register(GPXTrack)
class GPXTrackAdmin(OLAdminBase):
    pass


@admin.register(Track)
class TrackAdmin(OLAdminBase):
    form = LGEntryForm
    list_display = ('title', 'date_walked', 'distance', 'rating', 'location')
    list_filter = ('rating', 'location', 'date_walked')
    prepopulated_fields = {'slug': ('title',)}
    search_fields = ('tite',)
    fieldsets = (
        ('Region', {
            'fields': (
                ('title', 'rating'),
                'subtitle',
                'body_markdown',
                ('date_walked', 'slug'),
                ('distance', 'duration'),
                'gpx_file',
                'point',
            ),
            'classes': (
                'show',
                'extrapretty'
            )
        }),
    )

    # 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'
    
    class Media:
        js = ('image-loader.js', 'next-prev-links.js')
        css = {
            "all": ("my_styles.css",)
        }