local p = {}
revlib = require("Module:Revision");
function p.row(frame)
    local status = frame.args["s"]
    local title = frame.args["t"]
    local short = p.shorttitle(title, 40)
    local size = frame.args["z"]
    local modified_by = frame.args["mr"]
    local modified_at = frame.args["md"]
    local old_id = frame.args["mi"]
    local special_user = frame.args["sr"]
    local special_time = frame.args["sd"]
    local special_id = tonumber(frame.args["si"])
    local display_notes = tonumber(frame.args["n"])
    local rowtemplate = "<tr style=\"background-color:%s\"> %s </tr>"
    local colorthing =  p.color(status, false)
    local cols = {}
    cols[1] = string.format("[[:%s|%s]]", title, short)
    cols[2] = string.format('<span style="display:none" class="sortkey">%d</span>%.1f kB', size, size / 1000)
    
    local is_userspace = string.sub(frame.args["t"], 1, 4) == "User"
    if is_userspace then display_notes = 1 end
    
    if display_notes then cols[3] = p.notes(frame) else cols[3] = "" end
    
    if special_id then
        cols[4] = p.printuser(special_user)
        cols[5] = string.format('<span style="display:none" class="sortkey">%s</span>%s', special_id, p.invoke("Module:Revision", "diff", frame, title, "prev", special_id, special_time))
    else
        cols[4] = "Unknown"
        cols[5] = "Unknown"
    end
            
    cols[6] = p.printuser(modified_by)
    cols[7] = string.format('<span style="display:none" class="sortkey">%s</span>%s', old_id, p.invoke("Module:Revision", "diff", frame, title, "prev", old_id, modified_at))
    local colstring = ""
    for i=1, 7 do colstring = colstring .. string.format("<td>%s</td>", cols[i]) end
    return string.format(rowtemplate, colorthing, colstring)
end

function p.notes(frame)
    local result = ""
    local is_suspected_copyvio = tonumber(frame.args["nc"])
    local is_unsourced = tonumber(frame.args["nu"])
    local no_inline = tonumber(frame.args["ni"])
    local is_short = tonumber(frame.args["ns"])
    local is_resubmit = tonumber(frame.args["nr"])
    local is_old = tonumber(frame.args["no"])
    local submitter_is_blocked = tonumber(frame.args["nb"])
    local is_userspace = string.sub(frame.args["t"], 1, 4) == "User"
    
    if is_suspected_copyvio then result = result .. "<abbr title=\"Submission is a suspected copyright violation\">copyvio</abbr>&#32;&#32;" end
    if is_unsourced then result = result .. "<abbr title=\"Submission lacks references completely\">unsourced</abbr>&#32;&#32;" end
    if no_inline then result = result .. "<abbr title=\"Submission has no inline citations\">no-inline</abbr>&#32;&#32;" end
    if is_short then result  = result .."<abbr title=\"Submission is less than a kilobyte in length\">short</abbr>&#32;&#32;" end
    if is_resubmit then result  = result .. "<abbr title=\"Submission was resubmitted after a previous decline\">resubmit</abbr>&#32;&#32;" end
    if is_old then result  = result .. "<abbr title=\"Submission has not been touched in over four days\">old</abbr>&#32;&#32;" end
    if submitter_is_blocked then result  = result .. "<abbr title=\"Submitter is currently blocked\">blocked</abbr>&#32;&#32;" end
    if is_userspace then result  = result .. "<abbr title=\"Submission is located in the User or User Talk space\">userspace</abbr>&#32;&#32;" end
        
    return result
end

function p.color(status, dark)
    local result
    local dark_colors = {
        p = "#979158",
        d = "#a07980",
        r = "#708a92",
        a = "#5f9e5f"
    }
    local normal_colors = {
        p = "#f3eba3",
        d = "#ffcdd5",
        r = "#b1dae8",
        a = "#adfcad"
    }
    if dark then
        return dark_colors[status] or "#7e7f7a"
    else
        return normal_colors[status] or "#d2d3cc"
    end
end

function p.printuser(user)
  return string.format("[[User:%s|%s]] ([[User talk:%s|t]])", user, user, user)
end

function p.shorttitle(fulltitle, maxlength)
    --strip off namespace:basepage/ if it exists and anything is left
    --if not, strip off namespace
    --truncate to maxlength
    local startindex, size, namespace, basetitle, subtitle = mw.ustring.find(fulltitle, "([^:]*):([^\/]*)\/?(.*)")
    local effective_title = subtitle or basetitle
    if (effective_title == nil or string.len(effective_title) == 0) then effective_title = fulltitle end
    effective_title = string.gsub(effective_title, "^Submissions/", "")
    -- return mw.text.truncate( effective_title, maxlength ) (mw.text is not yet deployed!)
    if (mw.ustring.len(effective_title) > maxlength) then
        return mw.ustring.sub(effective_title, 1, maxlength - 3) .. "..."
    else
        return effective_title
    end
    
    
end

--equivalent of {{#Invoke:module|function|arg1|arg2|namedarg=arg3|namedarg2=arg4}} becomes
--p.invoke("module", "function", frame, arg1, arg2, {namedarg=arg3}, {namedarg2=arg4})
function p.invoke(modname, call, frame, ...)
    local mymod = require(modname)
    local nargs = {}
    for i,v in ipairs(arg) do
        if type(v) == "table" then
            local argname, argv = ipairs(v)
            nargs[argname] = tostring(argv)
        else
            nargs[i] = tostring(v)
        end
    end
    local childframe = frame:newChild{title = nil, args = nargs}
    return mymod[call](childframe)
end



return p