import urllib

from functools import wraps
from uuid import uuid4


def parse_namespace_repository(repository, include_tag=False):
  parts = repository.rstrip('/').split('/', 1)
  if len(parts) < 2:
    namespace = 'library'
    repository = parts[0]
  else:
    (namespace, repository) = parts

  if include_tag:
    parts = repository.split(':', 1)
    if len(parts) < 2:
      tag = 'latest'
    else:
      (repository, tag) = parts

  repository = urllib.quote_plus(repository)
  if include_tag:
    return (namespace, repository, tag)
  return (namespace, repository)

def parse_repository_name(f):
  @wraps(f)
  def wrapper(repository, *args, **kwargs):
    (namespace, repository) = parse_namespace_repository(repository)
    return f(namespace, repository, *args, **kwargs)
  return wrapper

def parse_repository_name_and_tag(f):
  @wraps(f)
  def wrapper(repository, *args, **kwargs):
    namespace, repository, tag = parse_namespace_repository(repository, include_tag=True)
    return f(namespace, repository, tag, *args, **kwargs)
  return wrapper


def format_robot_username(parent_username, robot_shortname):
  return '%s+%s' % (parent_username, robot_shortname)


def parse_robot_username(robot_username):
  if not '+' in robot_username:
    return None

  return robot_username.split('+', 2)


def parse_urn(urn):
  """ Parses a URN, returning a pair that contains a list of URN
      namespace parts, followed by the URN's unique ID.
  """
  if not urn.startswith('urn:'):
    return None

  parts = urn[len('urn:'):].split(':')
  return (parts[0:len(parts) - 1], parts[len(parts) - 1])


def parse_single_urn(urn):
  """ Parses a URN, returning a pair that contains the first
      namespace part, followed by the URN's unique ID.
  """
  result = parse_urn(urn)
  if result is None or not len(result[0]):
    return None

  return (result[0][0], result[1])

uuid_generator = lambda: str(uuid4())


def urn_generator(namespace_portions, id_generator=uuid_generator):
  prefix = 'urn:%s:' % ':'.join(namespace_portions)
  def generate_urn():
    return prefix + id_generator()
  return generate_urn
