Module:Ideology

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

Implements {{Ideology}}.

------------------------------------------------------------------------------
-- 
--                                 Module:Ideology
-- 
--        This module implements Template:Ideology.
------------------------------------------------------------------------------

--local iconify = require('Module:Iconify').iconify
local getArgs = require('Module:Arguments').getArgs
local lawTT = require('Module:Law').law
local ttUtils = require('Module:Tooltip')
local size = require('Module:TableTools').size
local uString = mw.ustring
local p = {};
local gameTT = 'color: #c2bdb8; font-style: normal; background: linear-gradient(to bottom,#47373a,#211b1c); border: solid #b19656; border-radius: 10px; width: max-content; padding: 5px;'

local function formatStances(t)
    local stanceText = {
        '[[File:Political disapproval.png|20px|link=|Oppose]] <span style="color: rgb(88% 34% 34%); font-weight: bold;">Strongly Oppose</span> ',
        '[[File:Political disapproval.png|20px|link=|Oppose]] <span style="color: rgb(88% 34% 34%); font-weight: bold;">Oppose</span> ',
        '[[File:Political undecided.png|20px|link=|Neutral]] <span style="color: rgb(94% 95% 88%); font-weight: bold;">Neutral</span> towards ',
        '[[File:Political approval.png|20px|link=|Endorse]] <span style="color: rgb(39% 67% 33%); font-weight: bold;">Endorse</span> ',
        '[[File:Political approval.png|20px|link=|Endorse]] <span style="color: rgb(39% 67% 33%); font-weight: bold;">Strongly Endorse</span> ',
    }
    local t3 = { }
    local t2 = {  }

    local j = 1
    for i, laws in ipairs(t) do
        t2.laws = { }
        if laws[1] then
            for m, n in ipairs(laws) do
                t2.laws[m] = lawTT{n}
            end
            if t2.laws[4] then --split long lines of laws
                t2.laws[3] = '-<br><span style="margin-left:5.7em;">'..t2.laws[3]
                t2.laws[#t2.laws] = t2.laws[#t2.laws]..'</span>'
            end
            t2[i] = mw.text.listToText(t2.laws)
            t3[j] = stanceText[i]..t2[i]; j = j+1
        end
    end
    return t3
end

function p.main(frame)
    local args = getArgs(frame)

    local ideoID = uString.lower(args[1])
    args.page = "Ideology"
    args.dataPage = "List"
    if args.char then
        ideoID = ideoID .. " leader"
    elseif args.move then
        ideoID = ideoID .. " movement"
        args.dataPage = "Movement"
    end
    if uString.find(ideoID, "movement") then
        args.dataPage = "Movement"
    end
    args.ideoData = mw.loadData('Module:Ideology/'..args.dataPage)[ideoID] --load ideology data

    if not args.ideoData then
        return '<span style="color:red; font-size:11px;">(unrecognized ideology "' .. args[1] .. '" for [[Module:Ideology]])</span>[[Category:Pages with unrecognized ideologies]]'
    end

    if args.table then
        return p.table(args)
    else
        return p.ideology(args)
    end
end

function p.ideology(args)
    local index = tonumber(args.index)
    local ideoData = args.ideoData
    if not ideoData then --handle direct calls from other modules
        local ideoID = uString.lower(args[1])
        local page = "Ideology"
        local dataPage = "List"
        if args.char then
            ideoID = ideoID .. " leader"
        elseif args.move then
            ideoID = ideoID .. " movement"
            dataPage = "Movement"
        end
        if uString.find(ideoID, "movement") then
            dataPage = "Movement"
        end
        ideoData = mw.loadData('Module:Ideology/'..dataPage)[ideoID] --load ideology data

        if not ideoData then
            return '<span style="color:red; font-size:11px;">(unrecognized ideology "' .. args[1] .. '" for [[Module:Ideology]])</span>[[Category:Pages with unrecognized ideologies]]'
        end
    end
    if ideoData.char == true then
        args.page = "Character ideology"
    end
    if ideoData.move == true then
        args.page = "Movement ideology"
    end
    local loc = ideoData.loc
    -- set tooltip display as icon and text, short-circuit tooltip if out of index 
    local displayText = uString.format("[[File:%s|%s|link=%s#%s|%s]] %s", ideoData.icon, args.width or "24px", args.page, loc, loc, loc)
    --local displayText = iconify{icon=loc, image=ideoData.icon, width=args.width or "24px", link=page.."#"..loc, extra=loc}
    if type(index) ~= 'number' or index > 3 then -- any non-number or index over 3 returns basic display
        return ( args.from and ttUtils.tooltip(displayText) ) or displayText
--[[        if not args.from then
            return displayText
        else
            return ttUtils.tooltip(displayText)
        end--]]
    end
    local subtitle
    if ideoData.desc then subtitle = '<div style="font-size:smaller; font-style:italic; line-height:1.2em; max-width:500px">'..ideoData.desc..'</div>' end
    local header = ttUtils.headerStyle('2LR', {
        leftText=uString.format("[[File:%s|48px|link=%s#%s|%s]]", ideoData.icon, args.page, loc, loc),
        --leftText=iconify{icon=loc, image=ideoData.icon, width="48px", text=0, link=page.."#"..loc, extra=loc},
        rightText=args.page,
        mainText=loc,
        subText=subtitle
    })
    
    body = '<hr>'
    if ideoData.stances and size(ideoData.stances) > 0 then
        local stances = ''
        for group, stance in pairs(ideoData.stances) do
            local s = ''
            for _, laws in ipairs(formatStances(stance)) do
                s = s .. laws .. '<br>'
            end
            stances = stances.."<div>Stance on '''".. group ..[['''</div><div style="margin-left:1em">]]..s..'</div>'
        end
        body = body .. stances
    else
        body = body .. "No stances"
    end
    return ttUtils.tooltip(displayText, header, body, {style=gameTT, index=index})
end

function p.table(args)
--[=[    local ideoID = uString.lower(args[1])
    if args.char then
        ideoID = ideoID .. " leader"
    elseif args.move then
        ideoID = ideoID .. " movement"
    end
    local ideoData = mw.loadData('Module:Ideology/List')[ideoID] --load ideology data

    if not ideoData then
        return '<span style="color:red; font-size:11px;">(unrecognized ideology "' .. args[1] .. '" for [[Module:Ideology]])</span>[[Category:Pages with unrecognized ideologies]]'
    end
--]=]local ideoData = args.ideoData
    local loc = ideoData.loc

    local s = uString.format("[[File:%s|48px|%s]] %s%s", ideoData.icon, loc, loc, args[2] or '')

    if ideoData.stances and size(ideoData.stances) > 0 then
        s = uString.format("rowspan=%s | %s", size(ideoData.stances), s)
        for group, stances in pairs(ideoData.stances) do
            s = s.."\n| ".. group
            for _, laws in ipairs(stances) do
                s = s .."\n|"
                for _, law in ipairs(laws) do
                    s = s .. "\n* ".. lawTT{law,width='30px'}
                end
            end
            s = s .. "\n|-"
        end
        s = s:sub(1, -2)
    else
        s = s.." || ''No law stances'' || || || || ||"
    end

    return s
end

return p