from quixote import get_request, get_response, get_session, get_publisher

from larpe import storage
from larpe import misc
from larpe.users import User
from larpe.hosts import Host

items = [
    ('hosts', N_('Hosts')),
    ('users', N_('Users')),
    ('settings', N_('Settings')),
    ('logger', N_('Logs')),
    # FIXME : use get_request().environ['SCRIPT_NAME']) ?
    ('/', N_('Liberty Alliance Reverse Proxy'))]

def generate_header_menu [html] (selected = None):
    s = ["""<ul id="menu">\n"""]
    base_url = get_request().environ['SCRIPT_NAME'] + '/admin'
    features = get_publisher().cfg.get('misc', {}).get('features', 'both')
    show_logger = get_publisher().cfg.get('debug', {}).get('logger', False)
    for k, v in items:
        if k == '/':
            continue # skip root
        if k == 'logger' and not show_logger:
            continue
        if k == selected:
            s.append('<li class="active">')
        else:
            s.append('<li>')
        s.append('<a href="%s/%s/">%s</a></li>\n' % (base_url, k, _(v)))
    s.append('</ul>\n')
    return ''.join(s)

def generate_user_info [html] ():
    hosts = Host.select(lambda x: x.name == 'larpe')
    if not hosts:
        return
    host = hosts[0]
    user = get_session().get_user(host.provider_id)
    if user and user.name:
        logout_url = '%s/liberty/%s/logout' % (get_request().environ['SCRIPT_NAME'], host.name)
        """<ul class="user-info">
  <li class="ui-name">%s</li>
  <li class="ui-logout"><a href="%s">%s</a></li>
</ul>""" % (user.name, logout_url, _('logout'))


def html_top [html] (section, title = None, scripts = None):
    header_menu = generate_header_menu(section)
    user_info = generate_user_info()
    subtitle = ''
    for s in items:
        if s[0] == section:
            subtitle = _(s[1])
    if not title:
        title = ''
    else:
        title = ' - ' + title
    if not scripts:
        scripts = ''
    else:
        scripts = '\n'.join(['<script src="%s" type="text/javascript"></script>' % x for x in scripts])

    sitetitle = _('Larpe Administration')
#    if title:
#        sitetitle += ' - '

    admin_ezt = True
    get_response().filter.update(locals())

def error_page [html] (section, error):
    html_top(section, title = _('Error'))
    '<div id="error-page">'
    '<h2>%s</h2>' % _('Error')
    '<p>%s</p>' % error
    '</div>'

def command_icon [html] (url, type, label = None, icon = None):
    script_name = get_request().environ['SCRIPT_NAME']
    icons = {
        'edit': 'stock_edit_16.png',
        'add': 'stock_add_16.png',
        'remove': 'stock_remove_16.png',
        'duplicate': 'stock_copy_16.png',
        'view': 'view_16.png',
    }
    labels = {
        'add': N_('Add'),
        'edit': N_('Edit'),
        'remove': N_('Remove'),
        'duplicate': N_('Duplicate'),
        'view': N_('View'),
        }
    if not label:
        label = _(labels[str(type)])
    if not icon:
        icon = icons[str(type)]
    if url:
        '''<span class="%(type)s">
  <a href="%(url)s"><img src="%(script_name)s/larpe/images/%(icon)s" alt="%(label)s" title="%(label)s" /></a>
</span>''' % locals()
    else:
        # no url -> image button
        '''<span class="%(type)s">
  <input type="image" src="%(script_name)s/larpe/images/%(icon)s" alt="%(label)s" title="%(label)s" />
</span>''' % locals()

