local ImageLink = {}
 
-- Dependencies
local HF = require("Dev:HF")
local FN = require("Module:Filename")
 
--[[
{{#invoke: ImageLink | ability
| champion    = 
| ability     = 
...
}}
 
{{#invoke: ImageLink | champion
| champion    = 
| skin        = 
...
}}
 
{{#invoke: ImageLink | item
| item        = 
| enchantment = 
...
}}
 
{{#invoke: ImageLink | rune
| rune        = 
...
}}
 
{{#invoke: ImageLink | spell
| spell       = 
...
}}
 
{{#invoke: ImageLink | basic
| link        = 
| text        = 
| image       = 
| size        = 
| width       = 
| height      = 
| border      = 
| separator   = 
| display     = 
| class       = 
| style       = 
| iconclass   = 
| iconstyle   = 
| label       = 
| labelclass  = 
| labelstyle  = 
| possessive  =
| edit        =
| editlink    =
}}
]]--
 
function ImageLink.ability(frame)
    local args = arguments(frame.args)
 
    args['link'] = args['link'] or formatnil('%s#%s', args['champion'], args['ability']) or args['champion'] or ''
    args['text'] = args['text'] or args['ability'] or ''
    args['image'] = args['image'] or FN.ability{args['ability'], args['champion']}
    args['editlink'] = args['editlink'] or formatnil('Template:Data %s/%s', args['champion'], args['ability']) or ''
 
    local link, img = ImageLink._createLink(args)
    if args['champion'] and args['ability'] then
        link:addClass('ability-icon')
        if args['champion'] then link:attr('data-champion', args['champion']) end
        if args['ability'] then link:attr('data-ability', args['ability']) end
    end
    if(args['width']) and img ~= nil then img:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.champion(frame)
    local args = arguments(frame.args)
 
    args['link'] = args['link'] or args['champion'] or ''
    args['text'] = args['text'] or args['champion'] or ''
--    if args['skin'] then
--        args['image'] = args['image'] or FN.championcircle{args['champion'], args['skin']}
--        args['border'] = args['border'] or 'false'
--        args['size'] = args['size'] or 30
--    else
        args['image'] = args['image'] or FN.champion{args['champion']}
--    end
 
    args['editlink'] = args['editlink'] or formatnil('Template:Data %s', args['champion']) or ''
 
    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().rootText
 
    local link, img = ImageLink._createLink(args)
    link:addClass('champion-icon')
    if args['champion'] then link:attr('data-champion', args['champion']) end
    if args['skin'] then link:attr('data-skin', args['skin']) end
    if(args['width']) and img ~= nil then img:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.item(frame)
    local args = arguments(frame.args)
 
    local item = args['item']
    local enchantment = args['enchantment']
 
    if args['enchantment'] then
        args['link'] = args['link'] or args['enchantment'] or ''
        args['text'] = formatnil('%s (%s)', args['item'], args['enchantment']) or args['text'] or ''
        args['image'] = args['image'] or FN.item{item, enchantment}
    else
        args['link'] = args['link'] or args['item'] or ''
        args['text'] = args['text'] or args['item'] or ''
        args['image'] = args['image'] or FN.item{item}
    end
    args['editlink'] = args['editlink'] or formatnil('Template:Item data %s', args['item']) or ''
 
 
    local link, img = ImageLink._createLink(args)
    link:addClass('item-icon')
    if args['item'] then link:attr('data-param', item) end
    if args['enchantment'] then link:attr('data-enchantment', enchantment):addClass('enchantment-icon') end
    if(args['width']) and img ~= nil then img:addClass('icon-' .. args['width']) end
 
    return link
end
 
function ImageLink.rune(frame)
    local args = arguments(frame.args)
 
    args['link'] = args['link'] or args['rune'] .. ' (Rune)' or ''
    args['text'] = args['text'] or args['rune'] or ''
    args['image'] = args['image'] or FN.rune{args['rune']}
    args['editlink'] = args['editlink'] or formatnil('Template:Rune data %s', args['rune']) or ''
 
    local link, img  = ImageLink._createLink(args)
    link:addClass('rune-icon')
    if args['rune'] then link:attr('data-param', args['rune']) end
    if(args['width']) and img ~= nil then img:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.spell(frame)
    local args = arguments(frame.args)
 
    args['link'] = args['link'] or args['spell'] or ''
    args['text'] = args['text'] or args['spell'] or ''
    args['image'] = args['image'] or FN.spell{args['spell']}
    args['editlink'] = args['editlink'] or formatnil('Template:Spell data %s', args['spell']) or ''
 
    local link, img = ImageLink._createLink(args)
    link:addClass('spell-icon')
    if args['spell'] then link:attr('data-param', args['spell']) end
    if(args['width']) and img ~= nil then img:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.basic(frame)
    local args = arguments(frame.args)
    local link, img, label = ImageLink._createLink(args)
    return link
end
 
function ImageLink._createLink(args)
    -- define args.display (default: inline)
    args['display'] = (({['inlineblock'] = 'inline-block'})[args['display']]) or args['display'] or 'inline'
 
    -- define args.image (default: '')
    args['image'] = args['image'] or ''
    if args['image'] == '*none*' then args['image'] = '' end
 
    -- define args.border (true or false; default: true)
    args['border'] = args['border'] or true
    if args['border'] == 'false' or args['border'] == '0' then args['border'] = false end
 
    -- define args.edit (true or false; default: false)
    args['edit'] = args['edit'] or false
    if args['edit'] == 'true' or args['edit'] == '1' then args['edit'] = true end
 
 
    -- define args.style (default: 'white-space:pre')
    args['style'] = args['style'] or 'white-space:pre'
    if args['style'] == '*none*' then args['style'] = nil end
 
    -- define args.width, args.height (%%x%%px or %%x%% or %%px or %%; default: 20px)
    local width, height
    args['size'] = args['size'] or 20
    if(args['size']) then width, height = getSize(args['size']) end
    args['width'] = tonumber(mw.ustring.match(args['width'] or width or '', '%d*'))
    args['height'] = tonumber(mw.ustring.match(args['height'] or height or '', '%d*'))
 
    -- define args.selflink (default: articlename)
    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().prefixedText
 
    -- define args.link (default: '')
    args['link'] = args['link'] or ''
    if args['text'] == '*none*' then args['link'] = '' end
    if args['link'] == '*none*' then args['link'] = '' end
 
    -- define args.possessive (default: '')
    args['possessive'] = args['possessive'] or ''
    if args['possessive'] == 'true' or args['possessive'] == '1' then args['possessive'] = true end
 
    -- define args.text (default: '')
    args['text'] = args['text'] or args['link'] or ''
    if args['text'] == '*none*' then args['text'] = '' end
    if args['possessive'] == true then
        if mw.ustring.sub(args['text'], -1) == 's' then
            args['text'] = args['text'] .. '\''
        else
            args['text'] = args['text'] .. '\'s'
        end
    end
 
    -- define args.label (before or after; default: after)
    args['label'] = (({['after'] = 'after', ['before'] = 'before'})[args['label']]) or 'after'
    if args['image'] == '' then args['label'] = 'only' end
    if args['text'] == '' then args['label'] = 'none' end
 
    -- define args.seperator (default: ' ')
    args['separator'] = args['separator'] or ' '
 
    local tag = ternary(args['display'] == 'inline', 'span', 'div')
 
    local link = mw.html.create(tag):css('display', args['display']):addClass(args['display'] .. '-image')
    if args['class'] then link:addClass(args['class']) end
    if args['style'] then link:cssText(args['style']) end
 
    -- //img start
    local img = mw.html.create(tag)
    if args['image'] ~= '' then
        local size = ternary(args['height'], tostring(args['width'] or '')..'x'..tostring(args['height'] or '')..'px', tostring(args['width'] or '')..'px')
        if args['iconclass'] then img:addClass(args['iconclass']) end
        if args['width'] then img:attr('data-width', tostring(args['width'])) end
        if args['height'] then img:attr('data-height', tostring(args['height'])) end
        if args['border'] == true then img:addClass('border') end
        if args['iconstyle'] then img:cssText(args['iconstyle']) end
        -- [[File:test.jpg|24px|border|link=test|testing]]
        img:wikitext(mw.ustring.format('[[File:%s%s%s|link=%s%s]]', 
            args['image'],
            ternary(size == 'px', '', '|'..size),
            ternary(args['border'], '|border', ''),
            ternary(args['link'] == args['selflink'], '', args['link']),
            ternary(args['text'] == '', '', '|'..args['text'])))
        :done()
    else
        img = nil
    end
    -- //img end
 
    -- //label start
    local label = mw.html.create(tag)
    link:addClass('label-'..args['label'])
    if args['labelclass'] then label:addClass(args['labelclass']) end
    if args['labelstyle'] then label:cssText(args['labelstyle']) end
    if args['link'] == args['selflink'] then
        label:wikitext('<strong class="selflink">' .. args['text'] .. '</strong>')
    elseif args['link'] == '' then
        label:wikitext(args['text'])
    else
        label:wikitext(formatnil('[[%s|%s]]', args['link'], args['text']))
    end
    if args['edit'] == true and args['editlink'] ~= nil then
        label:wikitext(' <sup>[[' .. args['editlink'] .. '|e]]</sup>')
    end
    label:done()
    -- //label end
 
    if args['label'] == 'before' then
        if label then link:node(label) end
        if args['separator'] and img then link:wikitext(args['separator']) end
        if img then link:node(img) end
    else 
        if img then link:node(img) end
        if args['separator'] and img then link:wikitext(args['separator']) end
        if label then link:node(label) end
    end
    return link, img, label
end
 
function formatnil(text, ...)
    if select("#", ...) == 0 then return text end
    for i=1, select("#", ...) do
        if select(i, ...) == nil then return nil end
    end
    return mw.ustring.format(text, ...)
end
 
function getSize(size)
    local fields = HF.explode('x', mw.ustring.gsub(tostring(size or ''), 'px', '', 1))
    local width = tonumber(fields[1]) or nil
    local height = tonumber(fields[2]) or nil
    return width, height
end
 
function ternary(cond, T, F)
    if cond then return T else return F end
end
 
function arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then v = HF.trim(v) end
        if v ~= '' then args[k] = v end
    end
    return args
end
 
return ImageLink