Documentation for this module may be created at Module:Render/Gear/doc

local render_icon = require('Module:Render').icon
local cargo
local model
local linkUtils = require('Module:LinkUtils')

local p = {}

function p.icon(frame)
  local args = require('Module:Arguments').getArgs(frame, {
    parentFirst = true,
    wrappers = {
      'Template:Gear/Icon',
    }
  })
  return p._icon(args)
end

function p._icon(args)
	local iname = args[1]
	if iname == nil then return nil end
	cargo = cargo or require('Module:CargoUtil')
	local page = cargo.query{
		tables = 'Pages',
		fields = 'iname, _pageName',
		where = {
			'type = "Gear"',
			'_pageName = "'..iname..'" OR iname = "'..iname..'"',
		},
		limit = 1,
	}[1]
	if page then iname = page.iname end
  local data = args.data or cargo.query{
    tables = 'Artifact',
    fields = 'iname, _pageName, server, rini, icon',
    where = {'iname="'..iname..'" OR _pageName="'..iname..'"'},
  }[1] or {rare=0,type=0}
  
  model = model or require('Module:Data').model
  local itemName = model.getLoc(data._pageName, 'name')
  
  local link = nil
  if args['link'] ~= 'none' then
    if args['jplink'] and args['jplink'] == true then
        link = linkUtils.acdbLink(iname, true)
    elseif data._pageName ~= nil then
        link = (page and page._pageName) or itemName or nil
    end
  end
  
  -- TODO Get the correct server
  local renderArgs = {
    data.icon and string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or 'ItemIcon,IT_UNKNOWN',
    itemName,
    link,
    tonumber(data.rini),
    2,
    64,
    args['count'] ~= '' and args['count'] or nil,
    args['classes'],
    nil,
    false,
  }
  
  local name = args['name'] ~= 'none' and (renderArgs[3] and renderArgs[3] ~= '' and ('[['..renderArgs[3]..'|'..renderArgs[2]..']]') or renderArgs[2]) or nil
  
  if args['size'] == 'large' then
    renderArgs[6] = 128
  end
  
  if args['size'] == 'small' then
    renderArgs[6] = 24
    renderArgs[9] = name and '<span>'..name..'</span>'
    renderArgs[10] = true
  elseif args['size'] == 'compact' then
    renderArgs[9] = name and '<div class="item-count" style="font-size: 0.7em;overflow: hidden;right: 0">'..renderArgs[2]..'</div>'
  else
    renderArgs[9] = name and '<div style="width: '..renderArgs[6]..'px">'..name..'</div>'
  end
  
  return render_icon(unpack(renderArgs))
end

-- Non cargo version
function p._icon2(args)
	local data = args.data or {}
	local iname = data.iname
	if not iname then return end
  
	local link = nil
	if args.link ~= 'none' then
		if args.jplink and args.jplink == true then
			link = linkUtils.acdbLink(iname, true)
		elseif data._pageName ~= nil then
			link = data._pageName or data.name or nil
		end
	end
	-- TODO Get the correct server
	local renderArgs = {
		data.icon and string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or 'ItemIcon,IT_UNKNOWN',
		data.name,
		link,
		tonumber(data.rini),
		2,
		64,
		tonumber(args.count),
		args.classes,
		nil,
		false,
	}
	local name = args.name ~= 'none' and (renderArgs[3] and renderArgs[3] ~= '' and ('[['..renderArgs[3]..'|'..renderArgs[2]..']]') or renderArgs[2]) or nil
	if args.size == 'large' then
		renderArgs[6] = 128
	elseif args.size == 'small' then
		renderArgs[6] = 24
		renderArgs[9] = name and '<span>'..name..'</span>'
		renderArgs[10] = true
	elseif args.size == 'compact' then
		renderArgs[9] = name and '<div class="item-count" style="font-size: 0.7em;overflow: hidden;right: 0">'..renderArgs[2]..'</div>'
	else
		renderArgs[9] = name and '<div style="width: '..renderArgs[6]..'px">'..name..'</div>'
	end
	return render_icon(unpack(renderArgs))
end

function p._pageName(args)
    local iname = args[1]
    model = model or require('Module:Data').model
    local data = args.data or model.query('Artifact','_pageName,server',{where='iname="'..iname..'"'})[1] or {}

    local name = model.getLoc(data._pageName, 'name')
  
    local link = nil
    if data._pageName ~= nil then
      link = (cargo.query{tables='Pages',fields='_pageName',where='type="Gear" and iname="'..iname..'"'}[1] or {linkUtils.wikiSafeName(name)})._pageName or nil
    end

    local displayName = (name and name ~= '' and (link and '[['..link..'|'..name..']]') or name) or iname
  
    return displayName
end

return p