def app_logo_attr():
    """ Return app logo LI attributes. """
    attr = {
        '_class': 'navbar-brand',
        '_role': 'button'}
    if request.controller == 'studies':
        attr['_onclick'] = 'tocModal(event);'
        attr['_title'] = 'Chapter Studies'
    elif request.controller == 'poems' and request.function == 'chapter':
        page = (int(request.args(0)) + 8) / 9
        if page == 1:
            attr['_href'] = URL('poems', 'index')
            attr['_title'] = 'Poems'
        else:
            attr['_href'] = URL('poems', 'page', args=[str(page)])
            attr['_title'] = 'Poems page %d' % page
    else:
        attr['_href'] = URL('poems', 'index')
        attr['_title'] = 'Poems'
    return attr

def auth_navbar():
    """ Set up right navbar for logged-in user. """
    navbar_right = auth.navbar(mode='dropdown')
    menu = navbar_right.element('.dropdown-menu')
    menu.insert(0, LI(A('Blacklist', _href=URL('blacklist', 'index'))))
    menu.insert(0, LI(A('Whitelist', _href=URL('whitelist', 'index'))))
    menu.insert(0, LI('', _class='divider'))
    menu.insert(0, LI(A('Unihan Dump', _href=URL('unihan', 'dump'))))
    if request.controller != 'studies':
        menu.insert(0, LI(A('Studies', _href=URL('studies', 'index'))))
    menu.insert(0, LI(A('Manage Poems', _href=URL('poems', 'manage'))))
    if request.controller != 'poems':
        menu.insert(0, LI(A('Poems', _href=URL('poems', 'index'))))
    if request.controller != 'about':
        menu.insert(0, LI(A('About', _href=URL('about', 'index'))))
    return UL(navbar_right, _class='nav navbar-nav navbar-right')

def default_study():
    """ Return a URL for the default study app chapter. """
    public, private = cache.ram('toc', lambda: studies_toc())
    if auth.user:
        toc_map = private
    else:
        toc_map = public
    if toc_map:
        return URL('studies', 'chapter', args=[toc_map.items()[0][0]])
    return URL('poems', 'index')

def plain_navbar():
    """ Return right navbar for non-logged-in user. """
    return UL(
        LI(
            A('Poems', _class='nav-link', _href=URL('poems', 'index')),
            _class='nav-item',
        ),
        LI(
            A('Studies', _class='nav-link', _href=URL('studies', 'index')),
            _class='nav-item',
        ),
        LI(
            A('About', _class='nav-link', _href=URL('about', 'index')),
            _class='nav-item',
        ),
        _class='nav navbar-nav navbar-right'
    )

def studies_toc():
    """ Return a tuple of ordered dicts that map chapter id to toc links.
    The first dict contains chapters that don't have an associated English
    poem, the second dict contains chapters that do. """
    from collections import OrderedDict

    def study_link(chapter):
        verse = db.verse[chapter]
        url = URL('studies', 'chapter', args=[verse.chapter.number])
        cls = 'studies-toc-link'
        lnk = '%i %s' % (verse.chapter.number, verse.chapter.title or '')
        return DIV(A(lnk, _class=cls, _href=url))

    public = OrderedDict()
    private = OrderedDict()
    for poem in db(db.poem).select(orderby=db.poem.chapter):
        link = study_link(poem.chapter)
        public[int(poem.chapter)] = link
    for chapter in range(1, 82):
        link = study_link(chapter)
        private[int(chapter)] = link
    return public, private

# Set navbar elements.
response.navbar_logo = LI(A('Daoistic', **app_logo_attr()))
if auth.user:
    response.navbar_right = auth_navbar()
else:
    response.navbar_right = cache.ram('navbar_right', lambda: plain_navbar())