from app import get_app_url, avatar
from data import model
from util.names import parse_robot_username
from jinja2 import Template, Environment, FileSystemLoader, contextfilter

def icon_path(icon_name):
  return '%s/static/img/icons/%s.png' % (get_app_url(), icon_name)

def icon_image(icon_name):
  return  '<img src="%s" alt="%s">' % (icon_path(icon_name), icon_name)

def user_reference(username):
  user = model.get_namespace_user(username)
  if not user:
    return username

  is_robot = False
  if user.robot:
    parts = parse_robot_username(username)
    user = model.get_namespace_user(parts[0])

    return """<span><img src="%s" alt="Robot"> <b>%s</b></span>""" % (icon_path('wrench'), username)

  alt = 'Organization' if user.organization else 'User'
  return """
  <span>
  <img src="%s"
      style="vertical-align: middle; margin-left: 6px; margin-right: 4px;" alt="%s">
  <b>%s</b>
  </span>""" % (avatar.get_url(user.email, 16), alt, username)


def repository_tag_reference(repository_path_and_tag):
  (repository_path, tag) = repository_path_and_tag
  (namespace, repository) = repository_path.split('/')
  owner = model.get_namespace_user(namespace)
  if not owner:
    return tag

  return """<a href="%s/repository/%s/%s?tag=%s">%s</a>""" % (get_app_url(), namespace, repository,
                                                              tag, tag)

def repository_reference(pair):
  if isinstance(pair, tuple):
    (namespace, repository) = pair
  else:
    pair = pair.split('/')
    namespace = pair[0]
    repository = pair[1]

  owner = model.get_namespace_user(namespace)
  if not owner:
    return "%s/%s" % (namespace, repository)

  return """
  <span style="white-space: nowrap;">
  <img src="%s" style="vertical-align: middle; margin-left: 6px; margin-right: 4px;">
  <a href="%s/repository/%s/%s">%s/%s</a>
  </span>
  """ % (avatar.get_url(owner.email, 16), get_app_url(), namespace, repository, namespace, repository)


def admin_reference(username):
  user = model.get_user_or_org(username)
  if not user:
    return 'account settings'

  if user.organization:
    return """
    <a href="%s/organization/%s/admin">organization's admin setting</a>
    """ % (get_app_url(), username)
  else:
    return """
    <a href="%s/user/">account settings</a>
    """ % (get_app_url())


def get_template_env(searchpath):
  template_loader = FileSystemLoader(searchpath=searchpath)
  template_env = Environment(loader=template_loader)
  add_filters(template_env)
  return template_env


def add_filters(template_env):
  template_env.filters['icon_image'] = icon_image
  template_env.filters['user_reference'] = user_reference
  template_env.filters['admin_reference'] = admin_reference
  template_env.filters['repository_reference'] = repository_reference
  template_env.filters['repository_tag_reference'] = repository_tag_reference