from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from .models import (
    Rower, Workout,GraphImage,FavoriteChart,SiteAnnouncement,
    Team,TeamInvite,TeamRequest,
    WorkoutComment,C2WorldClassAgePerformance,PlannedSession,
    GeoCourse,GeoPolygon,GeoPoint,VirtualRace,VirtualRaceResult,
    PaidPlan,IndoorVirtualRaceResult,ShareKey,
    CourseStandard,StandardCollection,
    )

# Register your models here so you can use them in the Admin module



# Rower details directly under the User
class RowerInline(admin.StackedInline):
    model = Rower
    can_delete = False
    verbose_name_plural = 'rower'
    filter_horizontal = ('team','friends')

    fieldsets = (
        ('Billing Details',
         {'fields':('street_address','city','postal_code','country','paymentprocessor','customer_id')}),
        ('Rower Plan',
         {'fields':('paidplan','rowerplan','paymenttype','planexpires','teamplanexpires','protrialexpires','plantrialexpires','clubsize','offercoaching')}),
        ('Rower Settings',
         {'fields':
          ('surveydone','surveydonedate','gdproptin','gdproptindate','weightcategory','sex','adaptiveclass','birthdate','getemailnotifications',
           'getimportantemails','emailbounced','defaultlandingpage',
           'defaulttimezone','showfavoritechartnotes')}),
        ('Rower Zones',
         {'fields':
          ('ftp','otwslack','pw_ut2','pw_ut1','pw_at','pw_tr','pw_an','max',
           'rest','ut2','ut1','at','tr','an','hrftp',)}),
        ('Import/Export Keys',
         {'fields':('c2token','tokenexpirydate',
                    'c2refreshtoken','c2_auto_export','c2_auto_import',
                    'sporttrackstoken','sporttrackstokenexpirydate',
                    'sporttracksrefreshtoken',
                    'sporttracks_auto_export',
                    'underarmourtoken','underarmourtokenexpirydate',
                    'underarmourrefreshtoken',
                    'mapmyfitness_auto_export',
                    'tptoken','tptokenexpirydate','tprefreshtoken',
                    'trainingpeaks_auto_export',
                    'polartoken','polartokenexpirydate',
                    'polarrefreshtoken','polaruserid',
                    'polar_auto_import',
                    'stravatoken','stravatokenexpirydate','stravarefreshtoken',
                    'stravaexportas','strava_auto_export',
                    'strava_auto_import',
                    'runkeepertoken','runkeeper_auto_export',)}),
        ('Team',
         {'fields':('friends','privacy','team')}),
        )


#class UserAdmin(UserAdmin):
class UserAdmin(admin.ModelAdmin):
    inlines = (RowerInline,)
    list_display = ('username','email','first_name','last_name','rowerplan','clubsize')

    fieldsets = (
        ('Personal info',
         {'fields':
          ('first_name', 'last_name', 'email', 'date_joined', 'last_login',)}),
        ('Permissions',
         {'fields':
          ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions',)}),)

    search_fields = ["username","first_name","last_name","email"]

    def rowerplan(self, obj):
        return obj.rower.rowerplan

    def clubsize(self, obj):
        return obj.rower.clubsize

class WorkoutAdmin(admin.ModelAdmin):
    list_display = ('date','user','name','workouttype','boattype')

class FavoriteChartAdmin(admin.ModelAdmin):
    list_display = ('user','xparam','yparam1','yparam2','plottype','workouttype','reststrokes')

class C2WorldClassAgePerformanceAdmin(admin.ModelAdmin):
    list_display = ('sex','weightcategory','age','distance','power','name','season')

class SiteAnnouncementAdmin(admin.ModelAdmin):
    list_display = ('announcement','created','modified','expires','dotweet')

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name','manager')

class TeamInviteAdmin(admin.ModelAdmin):
    list_display = ('issuedate','team','user','code')

class TeamRequestAdmin(admin.ModelAdmin):
    list_display = ('issuedate','team','user','code')

class WorkoutCommentAdmin(admin.ModelAdmin):
    list_display = ('created','user','workout')

class PlannedSessionAdmin(admin.ModelAdmin):
    list_display = ('name','startdate','enddate','manager','sessionvalue','sessionunit')

class GraphImageAdmin(admin.ModelAdmin):
    list_display = ('creationdatetime','workout','filename')

class GeoPolygonInline(admin.StackedInline):
    model = GeoPolygon

class ShareKeyAdmin(admin.ModelAdmin):
    list_display = ('location','creation_date','expiration_date')


class GeoCourseAdmin(admin.ModelAdmin):
    list_display = ('manager','name')
    inlines = (GeoPolygonInline,)
    search_fields = ['name']


class VirtualRaceAdmin(admin.ModelAdmin):
    list_display = ('manager','name','startdate','country')
    search_fields = ['name','country']

class VirtualRaceResultAdmin(admin.ModelAdmin):
    list_display = ('race','userid','username','boattype','age','weightcategory')
    search_fields = ['race__name','username']

class IndoorVirtualRaceResultAdmin(admin.ModelAdmin):
    list_display = ('race','userid','username','boatclass','age','weightcategory')
    search_fields = ['race__name','username']

class PaidPlanAdmin(admin.ModelAdmin):
    list_display = ('name','shortname','price','paymenttype','paymentprocessor','external_id')

class StandardCollectionAdmin(admin.ModelAdmin):
    list_display = ('name','manager')

class CourseStandardAdmin(admin.ModelAdmin):
    list_display = ('name','standardcollection')

admin.site.unregister(User)
admin.site.register(User,UserAdmin)
admin.site.register(Workout,WorkoutAdmin)
admin.site.register(GraphImage,GraphImageAdmin)
admin.site.register(Team,TeamAdmin)
admin.site.register(FavoriteChart,FavoriteChartAdmin)
admin.site.register(SiteAnnouncement,SiteAnnouncementAdmin)
admin.site.register(TeamInvite,TeamInviteAdmin)
admin.site.register(TeamRequest,TeamRequestAdmin)
admin.site.register(WorkoutComment,WorkoutCommentAdmin)
admin.site.register(C2WorldClassAgePerformance,
                    C2WorldClassAgePerformanceAdmin)
admin.site.register(PlannedSession,PlannedSessionAdmin)
admin.site.register(GeoCourse, GeoCourseAdmin)
admin.site.register(VirtualRace, VirtualRaceAdmin)
admin.site.register(VirtualRaceResult, VirtualRaceResultAdmin)
admin.site.register(IndoorVirtualRaceResult, IndoorVirtualRaceResultAdmin)
admin.site.register(PaidPlan,PaidPlanAdmin)
admin.site.register(ShareKey,ShareKeyAdmin)
admin.site.register(CourseStandard,CourseStandardAdmin)
admin.site.register(StandardCollection,StandardCollectionAdmin)
