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 = '<li {}><a href="{}"><span class="glyphicon glyphicon-home"></span>&nbsp;&nbsp;Overview</a></li>'.format('class="active"' if current_app == 'overview' else "", reverse('overview'))
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-bell"></span>&nbsp;&nbsp;Alerts{}</a></li>'.format('class="active"' if current_app == 'alert' else '', reverse('alert'), unseen_html)

    # Models
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-file"></span>&nbsp;&nbsp;Applications</a></li>'.format('class="active"' if current_app == 'application' else '', reverse('application'))
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;Players</a></li>'.format('class="active"' if current_app == 'player' else '', reverse('player'))
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-tags"></span>&nbsp;&nbsp;Tickets</a></li>'.format('class="active"' if current_app == 'ticket' else '', reverse('ticket'))
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-blackboard"></span>&nbsp;&nbsp;Notes</a></li>'.format('class="active"' if current_app == 'note' else '', reverse('note'))

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

    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-ban-circle"></span>&nbsp;&nbsp;Bans</a></li>'.format('class="active"' if current_app == 'ban' else '', reverse('ban'))
    ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-wrench"></span>&nbsp;&nbsp;Report</a></li>'.format('class="active"' if current_app == 'report' else '', reverse('report'))

    show_chat = True if getattr(settings, 'GLOBAL_LOG', None) is not None else False
    if show_chat and request.user.has_perm('minecraft_manager.chat'):
        ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Chat</a></li>'.format('class="active"' if current_app == 'chat' else '', reverse('chat'))
    if request.user.has_perm('minecraft_manager.bots'):
        ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-flash"></span>&nbsp;&nbsp;Bots</a></li>'.format('class="active"' if current_app == 'bots' else '', reverse('bots'))
    return ret
