--[[
  Just for fun.
]]--

local func = {}

local function rfind(s, pattern, init)
    local x, y
    local len = #s
    local i = init or len
    x, y = string.find(string.reverse(s), string.reverse(pattern), len-i+1, true)
    if x then
        return len-y+1, len-x+1
    end
end

-- TODO: fix for English Wikipedia
function func.eatuserbox(frame)
    local text = frame.args[1] or ''
    local output = ''
    
    output = string.gsub(text, '(class="wikipediauserbox" style=".-)"', '%1;visibility:hidden;"')
    output = string.gsub(output, '(style=".-)(" class="wikipediauserbox")', '%1;visibility:hidden;%2')
    output = string.gsub(output, '(class="wikipediauserbox%-m" style=".-)"', '%1;visibility:hidden;"')
    output = string.gsub(output, '(<div style=".-)(">%s*{%|%s*cellspacing="0")', '%1;visibility:hidden;%2')
    -- 專殺{{現在}}
    output = string.gsub(output, '(<div style="float:left; border: 1px solid orange; margin:1px; background: white; width: 238px; height:45px; text%-align: center; vertical%-align: middle;.-)(">)', '%1;visibility:hidden;%2')
    
    -- 將「坐等刪除」換成已被刪光
    if (not frame.args['notaunt']) or (mw.text.trim(frame.args['notaunt']) == '') then
        local x, y = string.find(output, '這個用戶正坐等他人來刪除用戶框。</td>', 1, true)
        if x then
            local left1, left2 = rfind(output, 'visibility:hidden;', x)
            output = string.sub(output, 1, left1-1) .. string.sub(output, left2+1, x-1) .. '這個用戶的用戶框全都被人刪光啦!</td>' .. string.sub(output, y+1, -1)
        end
    end

    if (not frame.args['nocategory']) or (mw.text.trim(frame.args['nocategory']) == '') then
        output = output .. '[[Category:用戶框被刪光了的維基人]]'
    end

    return output
end

function func.countstars(frame)
    local text = frame.args[1] or ''
    local pattern = frame.args[2] or '%*'
    local count = 0
    for _ in string.gmatch(text, pattern) do
    	count = count + 1
    end
    return count
end

function func.strrev(frame)
    local text = frame.args[1] or ''
    return string.reverse(text)
end

function func.strreplace(frame)
    local text = frame.args[1] or ''
    local i=2
    local from, to
    while frame.args[i] do
        from = frame.args[i]
        to = frame.args[i+1] or ''
        text = string.gsub(text, from, to)
        i=i+2
    end
    return text
end

function func.nowiki(frame)
    local text = frame.args[1] or ''
    return mw.text.nowiki(text)
end

function func.nowiki2(frame)
    local text = mw.title.new(frame.args[1]):getContent()
    return mw.text.nowiki(text)
end

local function islinkeatable(text)
    local test = string.lower(text)
    return not (string.find(test, 'category:') == 1 or string.find(test, 'image:') == 1 or 
           string.find(test, 'file:') == 1 or
           string.find(test, 'zh:') == 1 or string.find(test, 'ja:') == 1)
end

function func.eatlinks(frame)
    local text = frame.args[1] or ''
    -- TODO: External links
    return (string.gsub(text, '%[%[([^\n]-)%]%]', function(x)
        if islinkeatable(x) then
            local target = string.gsub(x, '^(.-)%|.-$', '%1')
            local text = string.gsub(x, '^.-%|(.-)$', '%1')

            if string.find(target, ':') == 1 then
                if text == target then
                    text = string.sub(text, 2)
                end
                return '<span style="color:#36b;cursor:pointer;">' .. text .. '</span>'
            else
                if mw.title.new(target):getContent() then
                    return '<span style="color:#0645ad;cursor:pointer;">' .. text .. '</span>'
                else
                    return '<span style="color:#ba0000;cursor:pointer;">' .. text .. '</span>'
                end
            end
        else
            return '[[' .. x .. ']]'
        end
    end))
end

return func