پرش به محتوا

پودمان:AlborzSports: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
برچسب: برگردانده‌شده
جزبدون خلاصۀ ویرایش
 
(۱۰ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۲: خط ۲:
-- Module:AlborzSports
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Wiki Alborz Sports Information System
-- Version 3..0
-- Core Engine
-- Version 2.2.2 (Fixed Entity Detection Priority)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local p = {}
local p = {}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LOAD DATA
-- LOAD DATA
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzSports/Data")
local data = mw.loadData("Module:AlborzSports/Data")
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- CONFIGURATION
-- CONFIG
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local DEFAULT_IMAGE_WIDTH = data.meta.imageWidth or "280px"
local INFOBOX_CLASS = data.meta.class or "infobox sport-infobox"
 
local DEFAULT_IMAGE_WIDTH =
    data.defaults.image_width or
    data.meta.imageWidth or
    "250px"
 
 
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- HELPERS
-- UTILITY FUNCTIONS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function clean(value)
local function clean(value)
 
if value == nil then
    if not value then
return nil
        return nil
end
    end
if type(value) == "string" then
 
value = mw.text.trim(value)
    value = mw.text.trim(value)
if value == "" then
 
return nil
    if value == "" then
end
        return nil
end
    end
return value
 
    return value
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ARGUMENT NORMALIZATION
-- GET ARGUMENTS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function getArgs(frame)
local function normalizeArgs(frame)
local args = {}
 
local source = frame
    local args = {}
local parent = frame:getParent()
 
if parent and next(parent.args) then
    for key, value in pairs(frame.args) do
source = parent
 
end
        local cleanKey = mw.text.trim(tostring(key))
for key, value in pairs(source.args) do
 
local cleanKey = mw.text.trim(tostring(key))
        local cleanValue = clean(value)
local cleanValue = clean(value)
 
if cleanKey ~= "" and cleanValue then
        if cleanValue then
args[cleanKey] = cleanValue
 
end
            local field =
end
                data.lookup[cleanKey] or
return args
                cleanKey
 
            args[field] = cleanValue
 
        end
 
    end
 
    return args
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ENTITY DETECTION
-- NORMALIZE INPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function normalize(args)
local function detectEntity(args)
local result = {}
 
for key, value in pairs(args) do
    local entity = args.entity_type
local field = data.lookup[key] or key
 
result[field] = value
 
end
    if entity then
return result
 
        if data.entities[entity] then
            return entity
        end
 
    end
 
 
    local typeValue = args.type
 
 
    if typeValue then
 
        for key, value in pairs(data.lookup) do
 
            if key == typeValue then
 
                if data.entities[value] then
                    return value
                end
 
            end
 
        end
 
    end
 
 
    return data.defaults.entity_type or "player"
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GET ENTITY DATA
-- LOCAL ENTITY ALIASES (تکمیل‌کننده Data برای پشتیبانی کامل فارسی)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local localEntityAliases = {
-- People
player = "player",
athlete = "player",
["بازیکن"] = "player",
["ورزشکار"] = "player",
coach = "coach",
["مربی"] = "coach",
referee = "referee",
["داور"] = "referee",
manager = "manager",
["مدیر ورزشی"] = "manager",
["مدیر"] = "manager",


local function getEntity(entityType)
-- Clubs
club = "club",
sports_club = "club",
sport_club = "club",
["باشگاه"] = "club",
["باشگاه ورزشی"] = "club",


    if data.entities[entityType] then
-- Academy
academy = "academy",
["آکادمی"] = "academy",
["آکادمی ورزشی"] = "academy",


        return data.entities[entityType]
-- Facilities
stadium = "stadium",
["ورزشگاه"] = "stadium",
gym = "gym",
gymnasium = "gym",
["سالن"] = "gym",
["سالن ورزشی"] = "gym",
pool = "pool",
swimming_pool = "pool",
["استخر"] = "pool",
zurkhaneh = "zurkhaneh",
["زورخانه"] = "zurkhaneh",
sports_school = "sports_school",
["مدرسه ورزشی"] = "sports_school",
sports_complex = "sports_complex",
["مجموعه ورزشی"] = "sports_complex",
facility = "facility",
["مجموعه"] = "facility",


    end
-- Organization / Board
board = "board",
sports_board = "board",
["هیئت"] = "board",
["هیئت ورزشی"] = "board",


-- Competition
competition = "competition",
["مسابقه"] = "competition",
["مسابقات"] = "competition"
}
--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------
local function getEntity(args)
local entity = clean(args.entity_type)


    return data.entities.player
----------------------------------------------------------------
 
-- EXPLICIT ENTITY TYPE
end
--
 
-- اگر کاربر صریحاً entity_type داده باشد،
-- بالاترین اولویت را دارد.
----------------------------------------------------------------
if entity then
entity = (data.entityAliases and data.entityAliases[entity])
or localEntityAliases[entity]
or entity


--------------------------------------------------------------------------------
if data.entities[entity] then
-- FIELD VALUE
return entity
--------------------------------------------------------------------------------
end
end


local function getValue(args, field)
----------------------------------------------------------------
-- INFERENCE FROM SUBTYPE (نوع)
----------------------------------------------------------------
local subtype = clean(args.subtype)


    local value = args[field]
if subtype then
local lowerSubtype = mw.ustring.lower(subtype)


------------------------------------------------------------
-- CLUB
------------------------------------------------------------
if mw.ustring.find(subtype, "باشگاه")
or mw.ustring.find(lowerSubtype, "club") then
return "club"
end


    if value then
------------------------------------------------------------
-- ACADEMY
------------------------------------------------------------
if mw.ustring.find(subtype, "آکادمی")
or mw.ustring.find(lowerSubtype, "academy") then
return "academy"
end


        return value
------------------------------------------------------------
-- STADIUM
------------------------------------------------------------
if mw.ustring.find(subtype, "ورزشگاه")
or mw.ustring.find(lowerSubtype, "stadium") then
return "stadium"
end


    end
------------------------------------------------------------
-- GYM
------------------------------------------------------------
if mw.ustring.find(subtype, "سالن")
or mw.ustring.find(lowerSubtype, "gym") then
return "gym"
end


------------------------------------------------------------
-- SWIMMING POOL
------------------------------------------------------------
if mw.ustring.find(subtype, "استخر")
or mw.ustring.find(lowerSubtype, "pool") then
return "pool"
end


    return nil
------------------------------------------------------------
-- ZURKHANEH
------------------------------------------------------------
if mw.ustring.find(subtype, "زورخانه")
or mw.ustring.find(lowerSubtype, "zurkhaneh") then
return "zurkhaneh"
end


end
------------------------------------------------------------
-- SPORTS SCHOOL
------------------------------------------------------------
if mw.ustring.find(subtype, "مدرسه")
or mw.ustring.find(lowerSubtype, "school") then
return "sports_school"
end


------------------------------------------------------------
-- SPORTS COMPLEX
------------------------------------------------------------
if mw.ustring.find(subtype, "مجموعه")
or mw.ustring.find(lowerSubtype, "complex") then
return "sports_complex"
end


--------------------------------------------------------------------------------
------------------------------------------------------------
-- FIELD LABEL
-- SPORTS BOARD
--------------------------------------------------------------------------------
------------------------------------------------------------
if mw.ustring.find(subtype, "هیئت")
or mw.ustring.find(lowerSubtype, "board") then
return "board"
end
end


local function getLabel(field)
----------------------------------------------------------------
-- INFERENCE FROM FIELDS
----------------------------------------------------------------
if args.established
or args.manager
or args.owner
or args.president
or args.ceo
or args.stadium
or args.capacity
or args.facilities
or args.services
or (args.address and args.city) then


    if data.fieldDefinitions[field] then
------------------------------------------------------------
-- STADIUM
------------------------------------------------------------
if args.stadium or args.capacity then
return "stadium"
end


        return data.fieldDefinitions[field].label
------------------------------------------------------------
-- CLUB
------------------------------------------------------------
return "club"
end


    end
----------------------------------------------------------------
-- DEFAULT ENTITY
----------------------------------------------------------------
local defaultEntity = data.defaults.entity_type or "player"


defaultEntity = (data.entityAliases and data.entityAliases[defaultEntity])
or localEntityAliases[defaultEntity]
or defaultEntity


    return field
if data.entities[defaultEntity] then
return defaultEntity
end


----------------------------------------------------------------
-- FINAL SAFE FALLBACK
----------------------------------------------------------------
return "player"
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TITLE RENDERER
-- TITLE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function getTitle(entity, args)
local function renderTitle(box, entityType, args)
if args.name then
 
return args.name
    local title =
end
        args.name or
local entityData = data.entities[entity]
        data.entities[entityType].title or
if entityData and entityData.title then
        data.meta.title
return entityData.title
 
end
 
return data.meta.title
    box
        :tag("th")
        :attr("colspan", "2")
        :addClass("infobox-title")
        :wikitext(title)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE RENDERER
-- IMAGE HANDLING
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function buildImage(args)
local function renderImage(box, args)
if not args.image then
 
return nil
    local image = args.image
end
 
local width = args.image_width or DEFAULT_IMAGE_WIDTH
 
local caption = args.caption or ""
    if not image then
return {
        return
type = "image",
    end
image = args.image,
 
size = width,
 
caption = caption
    local width =
}
        args.image_width or
        DEFAULT_IMAGE_WIDTH
 
 
    local imageHtml =
 
        mw.getCurrentFrame():extensionTag(
            "imagemap",
            ""
        )
 
 
    local file =
 
        mw.html.create("div")
        :addClass("infobox-image")
 
 
    file
        :wikitext(
            string.format(
                "[[پرونده:%s|%s]]",
                image,
                width
            )
        )
 
 
    if args.caption then
 
        file
            :tag("div")
            :addClass("infobox-caption")
            :wikitext(args.caption)
 
    end
 
 
    box
        :tag("tr")
        :tag("td")
        :attr("colspan", "2")
        :node(file)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- SECTION HEADER RENDERER
-- FIELD BUILDER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function buildFields(layout, args)
local function renderSectionHeader(box, layout)
local fields = {}
 
for _, item in ipairs(layout) do
    local title =
local label = item[1]
        data.layoutTitles[layout]
local key = item[2]
 
local value = clean(args[key])
 
if value then
    if not title then
table.insert(fields, {
        return
label = label,
    end
value = value
 
})
 
end
    box
end
        :tag("tr")
return fields
        :tag("th")
        :attr("colspan", "2")
        :addClass("infobox-header")
        :wikitext(title)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD RENDERER
-- GROUP BUILDER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function buildGroups(entity, args)
local groups = {}
local entityData = data.entities[entity]
if not entityData then
return groups
end


local function renderField(box, field, value)
for _, layoutName in ipairs(entityData.layouts or {}) do
 
local layout = data.layouts[layoutName]
 
if layout then
    if not value then
local fields = buildFields(layout, args)
        return
if #fields > 0 then
    end
local title = data.layoutTitles[layoutName] or layoutName
 
table.insert(groups, {
 
name = title,
    local label =
fields = fields
        getLabel(field)
})
 
end
 
end
    local row =
end
        box
return groups
            :tag("tr")
 
 
    row
        :tag("th")
        :wikitext(label)
 
 
    row
        :tag("td")
        :wikitext(value)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- SPECIAL FIELD RENDERER
-- MEDAL BUILDER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function buildMedals(args)
local medals = clean(args.medaltemplates)
if not medals then
medals = clean(args.medals)
end
if not medals then
return nil
end


local function renderSpecialField(box, field, value)
local frame = mw.getCurrentFrame()
 
local content = frame:expandTemplate{
 
title = "Infobox medal templates",
    if field == "website" then
args = {
 
medals = medals,
 
title = args.medaltemplates_title or "",
        value =
expand = "yes"
            string.format(
}
                "[%s %s]",
}
                value,
content = clean(content)
                "وبگاه"
if not content then
            )
return nil
 
end
 
return content
    elseif field == "instagram" then
 
 
        value =
            string.format(
                "[%s اینستاگرام]",
                value
            )
 
 
    elseif field == "telegram" then
 
 
        value =
            string.format(
                "[%s تلگرام]",
                value
            )
 
 
    elseif field == "coordinates" then
 
 
        value =
            string.format(
                "{{مختصات|%s}}",
                value
            )
 
 
    end
 
 
    renderField(
        box,
        field,
        value
    )
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LAYOUT PROCESSOR
-- INFOBOX BUILDER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function buildInfobox(entity, args)
local box = {}
box.title = getTitle(entity, args)


local function renderLayout(box, args, layout)
local image = buildImage(args)
 
if image then
 
box.image = image
    local fields =
end
        data.layouts[layout]
 
 
    if not fields then
        return
    end
 
 
    local hasValue = false
 
 
    for _, field in ipairs(fields) do
 
 
        local value =
            getValue(
                args,
                field
            )
 
 
        if value then
 
            hasValue = true
 
            break
 
        end
 
    end
 
 
    if not hasValue then
 
        return
 
    end
 
 
    renderSectionHeader(
        box,
        layout
    )
 
 
    for _, field in ipairs(fields) do
 
 
        local value =
            getValue(
                args,
                field
            )
 
 
        if value then
 
 
            renderSpecialField(
                box,
                field,
                value
            )
 
 
        end
 
    end


box.groups = buildGroups(entity, args)
box.medals = buildMedals(args)


return box
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ENTITY RENDERER
-- RENDER VALUE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
local function renderValue(value)
local function renderEntity(box, args, entityType)
if type(value) == "table" then
 
return table.concat(value, "، ")
 
end
    local entity =
return tostring(value)
        getEntity(entityType)
 
 
    if not entity then
 
        return
 
    end
 
 
    for _, layout in ipairs(entity.layouts) do
 
 
        renderLayout(
            box,
            args,
            layout
        )
 
 
    end
 
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
-- RENDER INFOBOX
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function renderInfobox(box)
local result = {}


local function buildInfobox(args)
table.insert(result, '<table class="sport-infobox">')
 
 
    local entityType =
        detectEntity(args)
 
 
    local box =
        mw.html.create("table")
 
 
    box
        :addClass(
            INFOBOX_CLASS
        )
 
 
    renderTitle(
        box,
        entityType,
        args
    )
 


    renderImage(
-- Title
        box,
if box.title then
        args
table.insert(result,
    )
'<tr class="sport-infobox-title">' ..
'<th colspan="2">' .. box.title .. '</th></tr>'
)
end


-- Image
if box.image then
local image = box.image
local file = image.image
local size = image.size or DEFAULT_IMAGE_WIDTH
local caption = image.caption or ""


    renderEntity(
table.insert(result,
        box,
'<tr><td colspan="2" class="sport-infobox-image">' ..
        args,
'[[File:' .. file .. '|' .. size .. ']]'
        entityType
)
    )
if caption ~= "" then
table.insert(result,
'<div class="sport-infobox-caption">' .. caption .. '</div>'
)
end
table.insert(result, '</td></tr>')
end


-- Groups
for _, group in ipairs(box.groups) do
table.insert(result,
'<tr class="sport-infobox-header">' ..
'<th colspan="2">' .. group.name .. '</th></tr>'
)
for _, field in ipairs(group.fields) do
table.insert(result,
'<tr>' ..
'<th class="sport-infobox-label">' .. field.label .. '</th>' ..
'<td class="sport-infobox-value">' .. renderValue(field.value) .. '</td>' ..
'</tr>'
)
end
end


    return tostring(box)
-- Medals
if box.medals then
table.insert(result,
'<tr><td colspan="2" class="sport-infobox-medals">' ..
box.medals .. '</td></tr>'
)
end


table.insert(result, '</table>')
return table.concat(result, "\n")
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- MAIN FUNCTION
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.main(frame)
function p.main(frame)
 
local args = getArgs(frame)
 
args = normalize(args)
    local args =
local entity = getEntity(args)
        normalizeArgs(
local box = buildInfobox(entity, args)
            frame:getParent()
return renderInfobox(box)
        )
 
 
    return buildInfobox(
        args
    )
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INVOKE SUPPORT
-- TEMPLATE ENTRY
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.infobox(frame)
function p.infobox(frame)
 
return p.main(frame)
    return p.main(frame)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TEMPLESTYLE SUPPORT
-- DEBUG FUNCTION
--------------------------------------------------------------------------------
 
local function addStyles()
 
    local frame =
        mw.getCurrentFrame()
 
 
    return frame:extensionTag(
        "templatestyles",
        "",
        {
            src = "Module:AlborzSports/styles.css"
        }
    )
 
end
 
 
--------------------------------------------------------------------------------
-- FINAL WRAPPER
--------------------------------------------------------------------------------
 
local function finalize(html)
 
    return
 
        addStyles()
 
        .. html
 
end
 
 
--------------------------------------------------------------------------------
-- PUBLIC RENDER FUNCTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.test(frame)
local args = getArgs(frame)
args = normalize(args)
local entity = getEntity(args)
local entityData = data.entities[entity]


function p.render(frame)
if not entityData then
return "Entity not found: " .. tostring(entity)
end


local result = {}
table.insert(result, "Entity: " .. entity)
table.insert(result, "Title: " .. tostring(entityData.title))
table.insert(result, "Layouts: " .. table.concat(entityData.layouts or {}, ", "))
table.insert(result, "Subtype: " .. tostring(args.subtype or "NOT FOUND"))
table.insert(result, "Coach: " .. tostring(args.coach or "NOT FOUND"))
table.insert(result, "Medals: " .. tostring(args.medaltemplates or args.medals or "NOT FOUND"))


    local args =
return table.concat(result, "\n")
        normalizeArgs(
            frame:getParent()
        )
 
 
    local output =
        buildInfobox(
            args
        )
 
 
    return finalize(output)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
-- GET ENTITY LIST
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
function p.entities()
function p.invoke(frame)
local result = {}
 
for key, value in pairs(data.entities) do
    return p.render(frame)
table.insert(result, key .. " : " .. tostring(value.title))
 
end
table.sort(result)
return table.concat(result, "\n")
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TEST SUPPORT
-- GET VERSION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
function p.version()
function p.test(frame)
return data.version.name .. " v" .. data.version.major .. "." .. data.version.minor
 
    local args = {
 
        name = "نمونه ورزشکار",
 
        entity_type = "player",
 
        nationality = "ایرانی",
 
        sport = "فوتبال",
 
        discipline = "فوتبال",
 
        position = "مهاجم",
 
        current_club = "باشگاه نمونه",
 
        birth_date = "۱۳۷۰",
 
        residence = "کرج"
 
    }
 
 
    return buildInfobox(args)
 
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- RETURN MODULE
-- EXPORT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
return p
return p

نسخهٔ کنونی تا ‏۱۰ اوت ۲۰۲۶، ساعت ۱۸:۲۹

توضیحات این پودمان می‌تواند در پودمان:AlborzSports/توضیحات قرار گیرد.

--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Core Engine
-- Version 2.2.2 (Fixed Entity Detection Priority)
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzSports/Data")
--------------------------------------------------------------------------------
-- CONFIG
--------------------------------------------------------------------------------
local DEFAULT_IMAGE_WIDTH = data.meta.imageWidth or "280px"
--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
--------------------------------------------------------------------------------
local function clean(value)
	if value == nil then
		return nil
	end
	if type(value) == "string" then
		value = mw.text.trim(value)
		if value == "" then
			return nil
		end
	end
	return value
end
--------------------------------------------------------------------------------
-- GET ARGUMENTS
--------------------------------------------------------------------------------
local function getArgs(frame)
	local args = {}
	local source = frame
	local parent = frame:getParent()
	if parent and next(parent.args) then
		source = parent
	end
	for key, value in pairs(source.args) do
		local cleanKey = mw.text.trim(tostring(key))
		local cleanValue = clean(value)
		if cleanKey ~= "" and cleanValue then
			args[cleanKey] = cleanValue
		end
	end
	return args
end
--------------------------------------------------------------------------------
-- NORMALIZE INPUT
--------------------------------------------------------------------------------
local function normalize(args)
	local result = {}
	for key, value in pairs(args) do
		local field = data.lookup[key] or key
		result[field] = value
	end
	return result
end
--------------------------------------------------------------------------------
-- LOCAL ENTITY ALIASES (تکمیل‌کننده Data برای پشتیبانی کامل فارسی)
--------------------------------------------------------------------------------
local localEntityAliases = {
	-- People
	player = "player",
	athlete = "player",
	["بازیکن"] = "player",
	["ورزشکار"] = "player",
	coach = "coach",
	["مربی"] = "coach",
	referee = "referee",
	["داور"] = "referee",
	manager = "manager",
	["مدیر ورزشی"] = "manager",
	["مدیر"] = "manager",

	-- Clubs
	club = "club",
	sports_club = "club",
	sport_club = "club",
	["باشگاه"] = "club",
	["باشگاه ورزشی"] = "club",

	-- Academy
	academy = "academy",
	["آکادمی"] = "academy",
	["آکادمی ورزشی"] = "academy",

	-- Facilities
	stadium = "stadium",
	["ورزشگاه"] = "stadium",
	gym = "gym",
	gymnasium = "gym",
	["سالن"] = "gym",
	["سالن ورزشی"] = "gym",
	pool = "pool",
	swimming_pool = "pool",
	["استخر"] = "pool",
	zurkhaneh = "zurkhaneh",
	["زورخانه"] = "zurkhaneh",
	sports_school = "sports_school",
	["مدرسه ورزشی"] = "sports_school",
	sports_complex = "sports_complex",
	["مجموعه ورزشی"] = "sports_complex",
	facility = "facility",
	["مجموعه"] = "facility",

	-- Organization / Board
	board = "board",
	sports_board = "board",
	["هیئت"] = "board",
	["هیئت ورزشی"] = "board",

	-- Competition
	competition = "competition",
	["مسابقه"] = "competition",
	["مسابقات"] = "competition"
}
--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------
local function getEntity(args)
	local entity = clean(args.entity_type)

	----------------------------------------------------------------
	-- EXPLICIT ENTITY TYPE
	--
	-- اگر کاربر صریحاً entity_type داده باشد،
	-- بالاترین اولویت را دارد.
	----------------------------------------------------------------
	if entity then
		entity = (data.entityAliases and data.entityAliases[entity])
			or localEntityAliases[entity]
			or entity

		if data.entities[entity] then
			return entity
		end
	end

	----------------------------------------------------------------
	-- INFERENCE FROM SUBTYPE (نوع)
	----------------------------------------------------------------
	local subtype = clean(args.subtype)

	if subtype then
		local lowerSubtype = mw.ustring.lower(subtype)

		------------------------------------------------------------
		-- CLUB
		------------------------------------------------------------
		if mw.ustring.find(subtype, "باشگاه")
			or mw.ustring.find(lowerSubtype, "club") then
			return "club"
		end

		------------------------------------------------------------
		-- ACADEMY
		------------------------------------------------------------
		if mw.ustring.find(subtype, "آکادمی")
			or mw.ustring.find(lowerSubtype, "academy") then
			return "academy"
		end

		------------------------------------------------------------
		-- STADIUM
		------------------------------------------------------------
		if mw.ustring.find(subtype, "ورزشگاه")
			or mw.ustring.find(lowerSubtype, "stadium") then
			return "stadium"
		end

		------------------------------------------------------------
		-- GYM
		------------------------------------------------------------
		if mw.ustring.find(subtype, "سالن")
			or mw.ustring.find(lowerSubtype, "gym") then
			return "gym"
		end

		------------------------------------------------------------
		-- SWIMMING POOL
		------------------------------------------------------------
		if mw.ustring.find(subtype, "استخر")
			or mw.ustring.find(lowerSubtype, "pool") then
			return "pool"
		end

		------------------------------------------------------------
		-- ZURKHANEH
		------------------------------------------------------------
		if mw.ustring.find(subtype, "زورخانه")
			or mw.ustring.find(lowerSubtype, "zurkhaneh") then
			return "zurkhaneh"
		end

		------------------------------------------------------------
		-- SPORTS SCHOOL
		------------------------------------------------------------
		if mw.ustring.find(subtype, "مدرسه")
			or mw.ustring.find(lowerSubtype, "school") then
			return "sports_school"
		end

		------------------------------------------------------------
		-- SPORTS COMPLEX
		------------------------------------------------------------
		if mw.ustring.find(subtype, "مجموعه")
			or mw.ustring.find(lowerSubtype, "complex") then
			return "sports_complex"
		end

		------------------------------------------------------------
		-- SPORTS BOARD
		------------------------------------------------------------
		if mw.ustring.find(subtype, "هیئت")
			or mw.ustring.find(lowerSubtype, "board") then
			return "board"
		end
	end

	----------------------------------------------------------------
	-- INFERENCE FROM FIELDS
	----------------------------------------------------------------
	if args.established
		or args.manager
		or args.owner
		or args.president
		or args.ceo
		or args.stadium
		or args.capacity
		or args.facilities
		or args.services
		or (args.address and args.city) then

		------------------------------------------------------------
		-- STADIUM
		------------------------------------------------------------
		if args.stadium or args.capacity then
			return "stadium"
		end

		------------------------------------------------------------
		-- CLUB
		------------------------------------------------------------
		return "club"
	end

	----------------------------------------------------------------
	-- DEFAULT ENTITY
	----------------------------------------------------------------
	local defaultEntity = data.defaults.entity_type or "player"

	defaultEntity = (data.entityAliases and data.entityAliases[defaultEntity])
		or localEntityAliases[defaultEntity]
		or defaultEntity

	if data.entities[defaultEntity] then
		return defaultEntity
	end

	----------------------------------------------------------------
	-- FINAL SAFE FALLBACK
	----------------------------------------------------------------
	return "player"
end
--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------
local function getTitle(entity, args)
	if args.name then
		return args.name
	end
	local entityData = data.entities[entity]
	if entityData and entityData.title then
		return entityData.title
	end
	return data.meta.title
end
--------------------------------------------------------------------------------
-- IMAGE HANDLING
--------------------------------------------------------------------------------
local function buildImage(args)
	if not args.image then
		return nil
	end
	local width = args.image_width or DEFAULT_IMAGE_WIDTH
	local caption = args.caption or ""
	return {
		type = "image",
		image = args.image,
		size = width,
		caption = caption
	}
end
--------------------------------------------------------------------------------
-- FIELD BUILDER
--------------------------------------------------------------------------------
local function buildFields(layout, args)
	local fields = {}
	for _, item in ipairs(layout) do
		local label = item[1]
		local key = item[2]
		local value = clean(args[key])
		if value then
			table.insert(fields, {
				label = label,
				value = value
			})
		end
	end
	return fields
end
--------------------------------------------------------------------------------
-- GROUP BUILDER
--------------------------------------------------------------------------------
local function buildGroups(entity, args)
	local groups = {}
	local entityData = data.entities[entity]
	if not entityData then
		return groups
	end

	for _, layoutName in ipairs(entityData.layouts or {}) do
		local layout = data.layouts[layoutName]
		if layout then
			local fields = buildFields(layout, args)
			if #fields > 0 then
				local title = data.layoutTitles[layoutName] or layoutName
				table.insert(groups, {
					name = title,
					fields = fields
				})
			end
		end
	end
	return groups
end
--------------------------------------------------------------------------------
-- MEDAL BUILDER
--------------------------------------------------------------------------------
local function buildMedals(args)
	local medals = clean(args.medaltemplates)
	if not medals then
		medals = clean(args.medals)
	end
	if not medals then
		return nil
	end

	local frame = mw.getCurrentFrame()
	local content = frame:expandTemplate{
		title = "Infobox medal templates",
		args = {
			medals = medals,
			title = args.medaltemplates_title or "",
			expand = "yes"
		}
	}
	content = clean(content)
	if not content then
		return nil
	end
	return content
end
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------
local function buildInfobox(entity, args)
	local box = {}
	box.title = getTitle(entity, args)

	local image = buildImage(args)
	if image then
		box.image = image
	end

	box.groups = buildGroups(entity, args)
	box.medals = buildMedals(args)

	return box
end
--------------------------------------------------------------------------------
-- RENDER VALUE
--------------------------------------------------------------------------------
local function renderValue(value)
	if type(value) == "table" then
		return table.concat(value, "، ")
	end
	return tostring(value)
end
--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------
local function renderInfobox(box)
	local result = {}

	table.insert(result, '<table class="sport-infobox">')

	-- Title
	if box.title then
		table.insert(result,
			'<tr class="sport-infobox-title">' ..
			'<th colspan="2">' .. box.title .. '</th></tr>'
		)
	end

	-- Image
	if box.image then
		local image = box.image
		local file = image.image
		local size = image.size or DEFAULT_IMAGE_WIDTH
		local caption = image.caption or ""

		table.insert(result,
			'<tr><td colspan="2" class="sport-infobox-image">' ..
			'[[File:' .. file .. '|' .. size .. ']]'
		)
		if caption ~= "" then
			table.insert(result,
				'<div class="sport-infobox-caption">' .. caption .. '</div>'
			)
		end
		table.insert(result, '</td></tr>')
	end

	-- Groups
	for _, group in ipairs(box.groups) do
		table.insert(result,
			'<tr class="sport-infobox-header">' ..
			'<th colspan="2">' .. group.name .. '</th></tr>'
		)
		for _, field in ipairs(group.fields) do
			table.insert(result,
				'<tr>' ..
				'<th class="sport-infobox-label">' .. field.label .. '</th>' ..
				'<td class="sport-infobox-value">' .. renderValue(field.value) .. '</td>' ..
				'</tr>'
			)
		end
	end

	-- Medals
	if box.medals then
		table.insert(result,
			'<tr><td colspan="2" class="sport-infobox-medals">' ..
			box.medals .. '</td></tr>'
		)
	end

	table.insert(result, '</table>')
	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame)
	args = normalize(args)
	local entity = getEntity(args)
	local box = buildInfobox(entity, args)
	return renderInfobox(box)
end
--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
--------------------------------------------------------------------------------
function p.infobox(frame)
	return p.main(frame)
end
--------------------------------------------------------------------------------
-- DEBUG FUNCTION
--------------------------------------------------------------------------------
function p.test(frame)
	local args = getArgs(frame)
	args = normalize(args)
	local entity = getEntity(args)
	local entityData = data.entities[entity]

	if not entityData then
		return "Entity not found: " .. tostring(entity)
	end

	local result = {}
	table.insert(result, "Entity: " .. entity)
	table.insert(result, "Title: " .. tostring(entityData.title))
	table.insert(result, "Layouts: " .. table.concat(entityData.layouts or {}, ", "))
	table.insert(result, "Subtype: " .. tostring(args.subtype or "NOT FOUND"))
	table.insert(result, "Coach: " .. tostring(args.coach or "NOT FOUND"))
	table.insert(result, "Medals: " .. tostring(args.medaltemplates or args.medals or "NOT FOUND"))

	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- GET ENTITY LIST
--------------------------------------------------------------------------------
function p.entities()
	local result = {}
	for key, value in pairs(data.entities) do
		table.insert(result, key .. " : " .. tostring(value.title))
	end
	table.sort(result)
	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- GET VERSION
--------------------------------------------------------------------------------
function p.version()
	return data.version.name .. " v" .. data.version.major .. "." .. data.version.minor
end
--------------------------------------------------------------------------------
-- EXPORT
--------------------------------------------------------------------------------
return p