from django.utils.translation import gettext as _
from .help import actionbar as actionbar_add_help
import os
from themes import gray_icon_url, color_icon_url
from users.context import menubar as menubar_user
from users.context import PROFILE_ENTRY_UID
import pygal
from pygal.models import get_item_by_rel_path

MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
HELP_UID = 'help-main'
NAVIGATION_ENTRY_UID = 'navigation-main-%s'
HOME_ENTRY_UID = 'home-main'
SEARCH_ENTRY_UID = 'search-main'
BACK_ENTRY_UID = 'back-main'


def context_adaption(context, request, rel_path, wrapper_instance=None, title='', current_help_page=None):
    context[context.MENUBAR].append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
    menubar_user(context[context.MENUBAR], request)
    menubar(context[context.MENUBAR], request, rel_path)
    navigationbar(context[context.NAVIGATIONBAR], request, rel_path)
    if wrapper_instance is None:
        # Pages without direct connection to an item (e.g. Helpview, ...)
        context.set_additional_title(title)
        context[context.ACTIONBAR].append_entry(
            BACK_ENTRY_UID,                             # uid
            _('Back'),                                  # name
            color_icon_url(request, 'back.png'),        # icon
            'javascript:history.back()',                # url
            True,                                       # left
            False                                       # active
        )
    else:
        wrapper_instance.context_adaption(context)
    # HELP
    if pygal.is_helpview(request):
        actionbar_add_help(context, request, current_help_page=current_help_page)


def menubar(bar, request, rel_path):
    if request.user.is_authenticated:
        bar.append_entry(*my_favourites_entry_parameters(request, rel_path))
    try:
        bar.replace_entry(PROFILE_ENTRY_UID, *profile_entry_parameters(request))
    except ValueError:
        pass        # Profile entry does not exist, so exchange is not needed (e.g. no user is logged in)


def navigationbar(bar, request, path):
    if pygal.is_favouriteview(request):
        if path:
            bar.append_entry(*navigation_entry_parameters(request, path, None))
            anchor = get_item_by_rel_path(path).name
        else:
            anchor = None
    elif pygal.is_searchview(request):
        if path:
            bar.append_entry(*navigation_entry_parameters(request, path, None))
            anchor = get_item_by_rel_path(path).name
        else:
            anchor = None
    else:
        anchor = None
        while len(path) > 0 and path != os.path.sep:
            bar.append_entry(*navigation_entry_parameters(request, path, anchor))
            anchor = get_item_by_rel_path(path).name
            path = os.path.dirname(path)
    bar.append_entry(*home_entry_parameters(request, anchor))


def profile_entry_parameters(request):
    return (
        PROFILE_ENTRY_UID,                          # uid
        request.user.username,                      # name
        color_icon_url(request, 'user.png'),        # icon
        pygal.url_profile(request),                 # url
        False,                                      # left
        False                                       # active
    )


def home_entry_parameters(request, anchor=None):
    if pygal.is_favouriteview(request):
        icon = 'favourite.png'
    elif pygal.is_searchview(request):
        icon = 'search.png'
    else:
        icon = 'home.png'
    return (
        HOME_ENTRY_UID,
        ':',
        gray_icon_url(request, icon),
        pygal.url_userview(request, rel_path='', search=pygal.SEARCH_KEEP) + ('#%s' % anchor if anchor is not None else ''),
        True,
        False
    )


def search_entry_parameters(request):
    return (
        SEARCH_ENTRY_UID,
        ':',
        gray_icon_url(request, 'search.png'),
        pygal.url_search(request),
        True,
        False
    )


def navigation_entry_parameters(request, path, anchor=None):
    return (
        NAVIGATION_ENTRY_UID % os.path.basename(path),                                                                  # uid
        '/' + os.path.basename(path),                                                                                   # name
        None,                                                                                                           # icon
        pygal.url_userview(request, path, search=pygal.SEARCH_KEEP) + ('#%s' % anchor if anchor is not None else ''),   # url
        False,                                                                                                          # left
        False                                                                                                           # active
    )


def my_favourites_entry_parameters(request, rel_path):
    if pygal.is_favouriteview(request) and rel_path != '':
        url_addon = '#%s' % get_item_by_rel_path(rel_path).name
    else:
        url_addon = ''
    return (
        MY_FAVOURITES_ENTRY_UID,                        # uid
        _('My Favourites'),                             # name
        color_icon_url(request, 'favourite.png'),       # icon
        pygal.url_favouriteview(request) + url_addon,   # url
        True,                                           # left
        pygal.is_favouriteview(request)                 # active
    )

