local p = {}

function p.link(frame)
  local usuario   = frame.args['usuário'] or ''
  local apelido   = frame.args['apelido'] or ''
  local separador = frame.args['separador'] or ' · '
  local prefixo   = frame.args['prefixo'] or '('
  local sufixo   = frame.args['sufixo'] or ')'
  
  if apelido == '' then apelido = usuario end
  
  local tag_open = '<span '
  local tag_close = '</span>'
  if separador ~= '' then
  	local decodedSeparador = mw.text.decode(separador)
  	
    if startsWith(decodedSeparador, "\20")          -- U+0020 <http://apps.timwhitlock.info/unicode/inspect/hex/0020>
    or startsWith(decodedSeparador, "\160")         -- U+00A0 <http://apps.timwhitlock.info/unicode/inspect/hex/00A0>
    or startsWith(decodedSeparador, "\225\154\128") -- U+1680 <http://apps.timwhitlock.info/unicode/inspect/hex/1680>
    or startsWith(decodedSeparador, "\225\160\142") -- U+180E <http://apps.timwhitlock.info/unicode/inspect/hex/180E>
    or startsWith(decodedSeparador, "\226\128\128") -- U+2000 <http://apps.timwhitlock.info/unicode/inspect/hex/2000>
    or startsWith(decodedSeparador, "\226\128\129") -- U+2001 <http://apps.timwhitlock.info/unicode/inspect/hex/2001>
    or startsWith(decodedSeparador, "\226\128\130") -- U+2002 <http://apps.timwhitlock.info/unicode/inspect/hex/2002>
    or startsWith(decodedSeparador, "\226\128\131") -- U+2003 <http://apps.timwhitlock.info/unicode/inspect/hex/2003>
    or startsWith(decodedSeparador, "\226\128\132") -- U+2004 <http://apps.timwhitlock.info/unicode/inspect/hex/2004>
    or startsWith(decodedSeparador, "\226\128\133") -- U+2005 <http://apps.timwhitlock.info/unicode/inspect/hex/2005>
    or startsWith(decodedSeparador, "\226\128\134") -- U+2006 <http://apps.timwhitlock.info/unicode/inspect/hex/2006>
    or startsWith(decodedSeparador, "\226\128\135") -- U+2007 <http://apps.timwhitlock.info/unicode/inspect/hex/2007>
    or startsWith(decodedSeparador, "\226\128\136") -- U+2008 <http://apps.timwhitlock.info/unicode/inspect/hex/2008>
    or startsWith(decodedSeparador, "\226\128\137") -- U+2009 <http://apps.timwhitlock.info/unicode/inspect/hex/2009>
    or startsWith(decodedSeparador, "\226\128\138") -- U+200A <http://apps.timwhitlock.info/unicode/inspect/hex/200A>
    or startsWith(decodedSeparador, "\226\128\139") -- U+200B <http://apps.timwhitlock.info/unicode/inspect/hex/200B>
    or startsWith(decodedSeparador, "\226\128\175") -- U+202F <http://apps.timwhitlock.info/unicode/inspect/hex/202F>
    or startsWith(decodedSeparador, "\226\129\159") -- U+205F <http://apps.timwhitlock.info/unicode/inspect/hex/205F>
    or startsWith(decodedSeparador, "\227\128\128") -- U+3000 <http://apps.timwhitlock.info/unicode/inspect/hex/3000>
    or startsWith(decodedSeparador, "\239\187\191") -- U+FEFF <http://apps.timwhitlock.info/unicode/inspect/hex/FEFF>
    then
      -- &#8203; é o ZERO WIDTH SPACE, e é um workaround para um bug que
      -- eventualmente remove os espaços no começo e no final da string
      -- Sem ele o código produz algumas formatações erradas.  Não tirar.
      separador = '&#8203;' .. separador
    end
  end
  
  -- Quando esse script foi feito, mw.language não estava disponível no Scribunto
  ---- local sexo = mw.language:gender(usuario, 'o', 'a', 'o(a)')
  -- utilizada a Magic Word {{GENDER|..}}
  local sexo = frame:callParserFunction( 'gender', usuario, 'o', 'a', 'o(a)' )
  local literal_o_usuario = sexo .. " usuári" .. sexo
  
  local discussao                = frame.args['discussão'               ] or "discussão"
  local contribuicoes            = frame.args['contribuições'           ] or "contribuições"
  local contribuicoes_globais    = frame.args['contribuições globais'   ] or "contribuições globais"
  local contribuicoes_eliminadas = frame.args['contribuições eliminadas'] or "contribuições eliminadas"
  local numero_edicoes           = frame.args['número de edições'       ] or "número de edições"
  local registros                = frame.args['registros'               ] or "registros"
  local registros_usuario        = frame.args['registros do usuário'    ] or "registros d" .. literal_o_usuario
  local registros_bloqueio       = frame.args['registros de bloqueio'   ] or "registros de bloqueio"
  local registros_movimento      = frame.args['registros de movimento'  ] or "registros de movimento"
  local registros_filtros        = frame.args['registros dos filtros'   ] or "registros dos filtros"
  local bloquear                 = frame.args['bloquear'                ] or "bloquear"
  
  local discussao_alt                = "Página de discussão d" .. literal_o_usuario
  local contribuicoes_alt            = "Contribuições d" .. literal_o_usuario
  local contribuicoes_globais_alt    = "Contribuições globais d" .. literal_o_usuario
  local contribuicoes_eliminadas_alt = "Contribuições eliminadas"
  local numero_edicoes_alt           = "Relatório de edições d" .. literal_o_usuario
  local registros_alt                = "Registros públicos d" .. literal_o_usuario
  local registros_usuario_alt        = "Registros que afetam " .. literal_o_usuario
  local registros_bloqueio_alt       = "Registros de bloqueio"
  local registros_movimento_alt      = "Registros de movimentação de páginas"
  local registros_filtros_alt        = "Registros dos filtros de edição"
  local bloquear_alt                 = "Bloquear " .. literal_o_usuario
  
  local usuarioURL = frame:callParserFunction('urlencode', usuario, 'QUERY')
  
  -- recupera link das estatísticas do usuário pela página [[Predefinição:EstatísticasUsuário]]
  local numero_edicoes_link = frame:expandTemplate{ title = 'EstatísticasUsuário', args = { usuario } }
  
  -- outros links que não podem ser informados através de Wikilinks
  local registros_usuario_link   = frame:callParserFunction('fullurl', 'Special:Log', 'page=User:' .. usuarioURL)
  local registros_bloqueio_link  = frame:callParserFunction('fullurl', 'Special:Log', 'type=block&page=User:' .. usuarioURL)
  local registros_movimento_link = frame:callParserFunction('fullurl', 'Special:Log', 'type=move&user=' .. usuarioURL)
  local registros_filtros_link   = frame:callParserFunction('fullurl', 'Special:AbuseLog', 'wpSearchUser=' .. usuarioURL)

  local txt = ''
  if discussao                ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. discussao_alt .. '">[[User talk:' .. usuario .. '|' .. discussao .. ']]' .. tag_close
  end
  if contribuicoes            ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. contribuicoes_alt .. '">[[Special:Contributions/' .. usuario .. '|' .. contribuicoes .. ']]' .. tag_close
  end
  if contribuicoes_globais    ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. contribuicoes_globais_alt .. '">[[Special:CentralAuth/' .. usuario .. '|' .. contribuicoes_globais .. ']]' .. tag_close
  end
  if contribuicoes_eliminadas ~= "" then
    txt = txt .. '<span class="sysop-show eliminator-show">' .. (txt ~= '' and separador or '')
    txt = txt .. tag_open .. 'title="' .. contribuicoes_eliminadas_alt .. '">[[Special:DeletedContributions/' .. usuario .. '|' .. contribuicoes_eliminadas .. ']]' .. tag_close
    txt = txt .. '</span>'
  end
  if numero_edicoes           ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. numero_edicoes_alt .. '">[' .. numero_edicoes_link .. ' ' .. numero_edicoes .. ']' .. tag_close
  end
  if registros                ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. registros_alt .. '">[[Special:Log/' .. usuario .. '|' .. registros .. ']]' .. tag_close
  end
  if registros_usuario       ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. registros_usuario_alt .. '">[' .. registros_usuario_link .. ' ' .. registros_usuario .. ']' .. tag_close
  end
  if registros_bloqueio       ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. registros_bloqueio_alt .. '">[' .. registros_bloqueio_link .. ' ' .. registros_bloqueio .. ']' .. tag_close
  end
  if registros_movimento      ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. registros_movimento_alt .. '">[' .. registros_movimento_link .. ' ' .. registros_movimento .. ']' .. tag_close
  end
  if registros_filtros      ~= "" then
    if txt ~= '' then txt = txt .. separador end
    txt = txt .. tag_open .. 'title="' .. registros_filtros_alt .. '">[' .. registros_filtros_link .. ' ' .. registros_filtros .. ']' .. tag_close
  end
  if bloquear                 ~= "" then
    if isIP(usuario) then
      txt = txt .. '<span class="sysop-show rollbacker-show">' .. (txt ~= '' and separador or '')
    else
      txt = txt .. '<span class="sysop-show">' .. (txt ~= '' and separador or '')
    end
    txt = txt .. tag_open .. 'title="' .. bloquear_alt .. '">[[Special:Block/' .. usuario .. '|' .. bloquear .. ']]' .. tag_close
    txt = txt .. '</span>'
  end
  
  if txt ~= '' then
    txt = ' ' .. prefixo .. txt .. sufixo
  end
  
  txt = '<span class="plainlinks">[[User:' .. usuario .. '|' .. apelido .. ']]' .. txt .. '</span>'
  return txt
end

function isIP(usuario)
  return GetIPType(usuario) ~= 0
end

-- code from http://stackoverflow.com/questions/10975935/lua-function-check-if-ipv4-or-ipv6-or-string
function GetIPType(ip)
  local R = {NOT_IP = 0, IPV4 = 1, IPV6 = 2}
  if type(ip) ~= "string" then return R.NOT_IP end

  -- check for format 1.11.111.111 for ipv4
  local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
  if #chunks == 4 then
    for _,v in pairs(chunks) do
      if tonumber(v) > 255 then return R.STRING end
    end
    return R.IPV4
  end

  -- check for ipv6 format, should be 8 'chunks' of numbers/letters
  -- without trailing chars
  local chunks = {ip:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}
  if #chunks == 8 then
    for _,v in pairs(chunks) do
      if #v > 0 and tonumber(v, 16) > 65535 then return R.STRING end
    end
    return R.IPV6
  end

  return R.NOT_IP
end

function startsWith(Haystack, Needle)
   return string.sub( Haystack, 1, string.len(Needle) ) == Needle
end

return p