Module:Production method

Documentation for this module may be created at Module:Production method/doc

Implements {{Production method}}.

------------------------------------------------------------------------------
-- 
--                                 Module:Production method
-- 
-- This module implements Template:Production method.
------------------------------------------------------------------------------

local p = {};
local iconify = require('Module:Iconify').iconify
local getArgs = require('Module:Arguments').getArgs
local ttUtils = require('Module:Tooltip')
p = require('Module:Tooltip/Types')

function p.main(frame)
    local args = getArgs(frame)
    if args.table then
        return '<span style="color:red;">WIP</span>' --p.table(args)
    else
        return p.productionMethod(args)
    end
end
--[=[
function p.productionMethod(args)
    local index = tonumber(args.index)
    local pmID = mw.ustring.lower(args[1])
    local pmData = mw.loadData('Module:Production method/List')[pmID] --load pm data

    -- catch unknown/error
    if not pmData then
        return '<span style="color:red; font-size:11px;">(unrecognized production method "' .. args[1] .. '" for [[Module:Production method]])</span>[[Category:Pages with unrecognized production methods]]'
    end

    -- translate from game icon list to wiki icon list, could offload to data page
    local icon = "Method "..pmData.icon..".png"

    -- set tooltip display as icon and text, short-circuit tooltip if out of index 
    local displayText = iconify{icon=pmData.loc, image=icon, link="List of production methods#"..pmData.loc}
    if type(index) ~= 'number' or index > 3 then -- any non number or index over 3 returns basic display
        return ttUtils.tooltip(displayText)
    end

    -- set tooltip style, imitates in-game tooltips
    local tooltipStyle = 'color: #c2bdb8; background: linear-gradient(to bottom,#47373a,#211b1c); border: solid #b19656; border-radius: 10px; width: max-content; padding: 5px;'

    -- set up header subtitle, with building for pm, tooltip building if pm called directly
    local subtitle = ''
    if pmData.building == "Multiple" then
        subtitle = "Used by multiple buildings"
    elseif args.from ~= "building" then
        subtitle = p.buildingTT{pmData.building, index=(index+1), from="pm"}
    else
        subtitle = iconify{icon=pmData.building, group="building", mod="Used by", link="List of buildings#"..pmData.building}
    end
    subtitle = '<span>' .. subtitle .. '</span>'

    local header = ttUtils.headerStyle('2LR', {
        leftText=iconify{icon=pmData.loc, image=icon, width="48px", text=0, link="List of production methods#"..pmData.loc},
        rightText=pmData.group,
        mainText=pmData.loc,
        subText=subtitle
    })

    -- start body with reqs and blockers
    local body = '<hr>'
    if pmData.reqs and pmData.blockers then
        body = body .. '<div style="display:grid; grid-template-columns: auto auto; gap:5px">'
        body = body .. '<div>Requires:<div style="margin-left:1em;">' .. ttUtils.ttList(pmData.reqs, {index=(index+1), from="pm"})..'</div></div>'
        body = body .. '<div>Blocked by:<div style="margin-left:1em;">' .. ttUtils.ttList(pmData.blockers, {index=(index+1), from="pm"})..'</div></div>'
        body = body .. '</div><hr>'
    elseif pmData.reqs then
        body = body .. 'Requires:<div style="margin-left:1em;">' .. ttUtils.ttList(pmData.reqs, {index=(index+1), from="pm"})..'</div><hr>'
    elseif pmData.blockers then
        body = body .. 'Blocked by:<div style="margin-left:1em;">' .. ttUtils.ttList(pmData.blockers, {index=(index+1), from="pm"})..'</div><hr>'
    end

    local effects = ''
    -- iterate effects
    if pmData.input then
        effects = effects .. '<div>Input:<div style="margin-left:1em;">' .. ttUtils.effectList(pmData.input,{icon="Goods"}) .. '</div></div>'
    end
    if pmData.output then
        effects = effects .. '<div>Output:<div style="margin-left:1em;">' .. ttUtils.effectList(pmData.output,{icon="Goods"}) .. '</div></div>'
    end
    if pmData.workforce then
        effects = effects .. '<div>Workforce:<div style="margin-left:1em;">' .. ttUtils.effectList(pmData.workforce,{icon="pop",page="Profession"}) .. '</div></div>'
    end
    local modifiers = { ws = { }, ls = { }, us = { } }
    local modList = ''
    local function modConcat(t)
    	for k, _ in pairs(t) do
            for _, v in ipairs(t[k]) do
                table.insert(modifiers[k], v)
            end
    	end
    end
    if pmData.bm then modConcat(pmData.bm) end
    if pmData.sm then modConcat(pmData.sm) end
    if pmData.cm then modConcat(pmData.cm) end

    if modifiers.ws[1] then modList = modList..ttUtils.effectList(modifiers.ws) end
    if modifiers.ls[1] then modList = modList..'<br>Level scaled<br>'..ttUtils.effectList(modifiers.ls) end
    if modifiers.us[1] then modList = modList..'<br>Unscaled<br>'..ttUtils.effectList(modifiers.us) end
    if modList ~= '' then
        effects = effects .. '<div>Modifiers:<div style="margin-left:1em;">' .. modList .. '</div></div>'
    end
    if effects ~= '' then
        body = body .. tostring(mw.html.create('div'):cssText("display:grid; grid-template-columns: auto auto; gap:5px 10px;"):wikitext(effects))
    else
        body = body .. "No effects"
    end
    return ttUtils.tooltip(displayText, header, body, {style=tooltipStyle, index=index})
end
--]=]

-- Temp copied from building module, not usable yet
function p.table(args)
    local pmID = mw.ustring.lower(args[1])
    local pmData = mw.loadData('Module:Production method/List')[pmID] --load building data

    if not pmData then
        return '<span style="color:red; font-size:11px;">(unrecognized building "' .. args[1] .. '" for [[Module:Production method]])</span>[[Category:Pages with unrecognized production methods]]'
    end
    local s = iconify{icon = args[1], group = "method"}

    s = mw.ustring.format("%s || %s || %s || %s || %s || %s", s, buildData.group, iconify{icon = buildData.tech, group = "invention"}, buildData.cost, buildData.urb, buildData.infra)
    s = s.."\n|"

    if buildData.pm then
        for _, v in ipairs(buildData.pm) do
            for _, u in ipairs(v) do
                s = mw.ustring.format("%s\n* %s", s, p.buildingTT{u,index=1})
            end
        end
    end

    return s
end

return p