from django.template import Library
from django.urls import reverse
from minecraft_manager.models import Alert
from django.conf import settings

register = Library()


@register.simple_tag
def get_sidebar(current_app, request):

    # Get unseen Alerts
    unseen_alerts = Alert.objects.filter(user=request.user, seen=False)
    unseen_html = ""
    if len(unseen_alerts) > 0:
        unseen_html = " <span class='badge badge-light'>" + str(len(unseen_alerts)) + "</span>"


    ret = ""
    if current_app == 'overview':
        ret += "<li class=\"active\"><a href=\"" + reverse('overview') + "\">Overview</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('overview') + "\">Overview</a></li>"
    if current_app == 'ban':
        ret += "<li class=\"active\"><a href=\"" + reverse('ban') + "\">Bans</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('ban') + "\">Bans</a></li>"
    if current_app == 'alert':
        ret += "<li class=\"active\"><a href=\"" + reverse('alert') + "\">Alerts{0}</a></li>".format(unseen_html)
    else:
        ret += "<li><a href=\"" + reverse('alert') + "\">Alerts{0}</a></li>".format(unseen_html)

    # Models
    if current_app == 'application':
        ret += "<li class=\"active\"><a href=\"" + reverse('application') + "\">Applications</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('application') + "\">Applications</a></li>"
    if current_app == 'player':
        ret += "<li class=\"active\"><a href=\"" + reverse('player') + "\">Players</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('player') + "\">Players</a></li>"
    if current_app == 'ticket':
        ret += "<li class=\"active\"><a href=\"" + reverse('ticket') + "\">Tickets</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('ticket') + "\">Tickets</a></li>"
    if current_app == 'warning':
        ret += "<li class=\"active\"><a href=\"" + reverse('warning') + "\">Warnings</a></li>"
    else:
        ret += "<li><a href=\"" + reverse('warning') + "\">Warnings</a></li>"

    # Split up MCM and "other"
    ret += "<hr/>"

    if current_app == 'report':
        ret += "<li class=\"active\"><a href=\"" + reverse("report") + "\">Report</a></li>"
    else:
        ret += "<li><a href=\"" + reverse("report") + "\">Report</a></li>"
    show_chat = True if getattr(settings, 'GLOBAL_LOG', None) is not None else False
    if show_chat and request.user.has_perm('auth.chat'):
        if current_app == 'chat':
            ret += "<li class=\"active\"><a href=\"" + reverse("chat") + "\">Chat</a></li>"
        else:
            ret += "<li><a href=\"" + reverse("chat") + "\">Chat</a></li>"
    if request.user.has_perm('auth.bots'):
        if current_app == 'bots':
            ret += "<li class=\"active\"><a href=\"" + reverse("bots") + "\">Bots</a></li>"
        else:
            ret += "<li><a href=\"" + reverse("bots") + "\">Bots</a></li>"
    return ret
