from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.forms import UserChangeForm
from django.utils.translation import ugettext as _

from newsreader.accounts.models import User


class UserAdminForm(UserChangeForm):
    class Meta:
        widgets = {
            "email": forms.EmailInput(attrs={"size": "50"}),
            "reddit_access_token": forms.PasswordInput(
                attrs={"size": "90"}, render_value=True
            ),
            "reddit_refresh_token": forms.PasswordInput(
                attrs={"size": "90"}, render_value=True
            ),
            "twitter_oauth_token": forms.PasswordInput(
                attrs={"size": "90"}, render_value=True
            ),
            "twitter_oauth_token_secret": forms.PasswordInput(
                attrs={"size": "90"}, render_value=True
            ),
        }


class UserAdmin(DjangoUserAdmin):
    list_display = ("email", "last_name", "date_joined", "is_active")
    list_filter = ("is_active", "is_staff", "is_superuser")
    ordering = ("email",)

    search_fields = ["email", "last_name", "first_name"]
    readonly_fields = ("last_login", "date_joined")

    form = UserAdminForm
    fieldsets = (
        (
            _("User settings"),
            {"fields": ("email", "password", "first_name", "last_name", "is_active")},
        ),
        (
            _("Reddit settings"),
            {"fields": ("reddit_access_token", "reddit_refresh_token")},
        ),
        (
            _("Twitter settings"),
            {"fields": ("twitter_oauth_token", "twitter_oauth_token_secret")},
        ),
        (
            _("Permission settings"),
            {"classes": ("collapse",), "fields": ("is_staff", "is_superuser")},
        ),
        (_("Misc settings"), {"fields": ("date_joined", "last_login")}),
    )


admin.site.register(User, UserAdmin)
