Module:Media

-- This modules serves as the code for Template:Media. local p = {} local image = require("Module:Image") local tab2 = require("Module:Tab2") local utils = require("Module:Utils")

-- Create the type tabs. function p.CreateTypeTabs(media, mediaTypes) local games = utils.GetGames("all") -- Create the tabs local tabs = {} for mediaTypeKey, mediaTypeValue in pairs(mediaTypes) do		local hasMedia = false for gameKey, gameValue in pairs(games) do if media[mediaTypeValue .. " " .. gameValue] ~= nil then hasMedia = true end end if hasMedia then table.insert(tabs, mediaTypeKey, {tabName = mediaTypeValue, tabContent = p.CreateGameTabs(media, mediaTypeValue)}) end end return tabs end

-- Returns the key of the type tab with the most recent game. function p.GetDefaultTypeTab(media, mediaTypes) local result local games = utils.GetGames("all") local biggestKeys = {} for mediaTypeKey, mediaTypeValue in pairs(mediaTypes) do		for gameKey, gameValue in pairs(games) do if media[mediaTypeValue .. " " .. gameValue] ~= nil then biggestKeys[mediaTypeKey] = gameKey end end end local biggestValue = 1 local biggestKey = 1 for key, value in pairs(biggestKeys) do		if value > biggestValue then biggestValue = value biggestKey = key end end return biggestKey end

-- Creates game tabs for the media type providen. function p.CreateGameTabs(media, mediaType) local games = utils.GetGames("all") local tabs = {} local outputImage local mostRecentGameKey for key, value in pairs(games) do if media[mediaType .. " " .. value] ~= nil then mostRecentGameKey = key if string.match(media[mediaType .. " " .. value], "File:") ~= nil then outputImage = image.PrintImageInfobox(media[mediaType .. " " .. value]) else outputImage = media[mediaType .. " " .. value] end table.insert(tabs, key, {tabName = value, tabContent = outputImage}) end end local result = tab2._Main(tabs, mostRecentGameKey, "bottom", "15%", "300px", "center") return tostring(result) end

-- Wrapper for Module calls. function p._Main(media) -- Media types local mediaTypes = {"Artwork", "Render", "Model", "Sprite"} -- Create "types" tabs. local tabs = p.CreateTypeTabs(media, mediaTypes) -- Get the default type tab local defaultTypeTab = p.GetDefaultTypeTab(media, mediaTypes) local result = tab2._Main(tabs, defaultTypeTab, "top", "20%", "", "center") return tostring(result) end

-- Template:Media function p.Main(frame) local args = frame:getParent.args return p._Main(args) end

return p