from django.template import Library

from minecraft_manager.models import UserSettings
from minecraft_manager.forms import UserSettingsForm
from django.contrib.auth.forms import PasswordChangeForm

register = Library()


@register.simple_tag
def get_csrf_html(request):
    if request.COOKIES and request.COOKIES['csrftoken']:
        return "<input type='hidden' name='csrfmiddlewaretoken' value='%s' />" % request.COOKIES['csrftoken']
    return ""


@register.simple_tag
def get_csrf_token(request):
    if request.COOKIES and request.COOKIES['csrftoken']:
        return request.COOKIES['csrftoken']
    return ""



@register.simple_tag
def get_form(form, request):

    if form.lower() == 'usersettings' and request:
        try:
            return UserSettingsForm(instance=request.user.usersettings).as_p()
        except UserSettings.DoesNotExist:
            user_settings = UserSettings(auth_user=request.user)
            user_settings.save()
            return UserSettingsForm(instance=request.user.usersettings).as_p()
    elif form.lower() == "password":
        return PasswordChangeForm(request.user).as_p()
