local p = {}

function p.alttable(age)
    local alt_map = {
        ["af"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الأمريكية",
            altvar = "American football"
        },
        ["afu"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الأمريكية للناشئين",
            altvar = "American football"
        },
        ["afw"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الأمريكية للسيدات",
            altvar = "American football"
        },
        ["arf"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الأسترالية",
            altvar = "Australian rules football",
            mw = "رجال"
        },
        ["arfw"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الأسترالية للسيدات",
            altvar = "Australian rules football",
            mw = "سيدات"
        },
        ["bandy"] = {
            linkpre = "منتخب",
            altlink = "للباندي",
            altvar = "bandy"
        },
        ["bandyw"] = {
            linkpre = "منتخب",
            altlink = "للباندي للسيدات",
            altvar = "bandy",
            template = "bandyW"
        },
        ["bb"] = {
            linkpre = "منتخب",
            altlink = "لكرة القاعدة",
            altvar = "كرة القاعدة"
        },
        ["bbstate"] = {
            linkpre = "منتخب",
            altlink = "state لكرة القاعدة",
            altvar = "baseball",
            template = "bb-state"
        },
        ["bbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة القاعدة للسيدات",
            altvar = "baseball"
        },
        ["bd"] = {
            linkpre = "منتخب",
            altlink = "لكرة الريشة",
            altvar = "كرة ريشة"
        },
        ["bs"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الشاطئية",
            altvar = "كرة قدم شاطئية",
            template = "beachsoccer"
        },
        ["bsw"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم الشاطئية للسيدات",
            altvar = "كرة قدم شاطئية للسيدات",
            template = "beachsoccerwomen"
        },
        ["bh"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد الشاطئية",
            altvar = "beach handball"
        },
        ["bhw"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد الشاطئية للسيدات",
            altvar = "beach handball",
            mw = "سيدات"
        },
        ["3x3"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة 3x3",
            altvar = "كرة السلة 3x3"
        },
        ["3x3w"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة 3x3 للسيدات",
            altvar = "كرة السلة 3x3 للسيدات",
            mw = "سيدات"
        },
        ["bk"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة",
            altvar = "كرة السلة"
        },
        ["bku"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة السلة",
            altvar = "كرة السلة",
            age = "تحت " .. age
        },
        ["bkw"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة للسيدات",
            altvar = "كرة السلة",
            mw = "سيدات"
        },
        ["bkwu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة السلة للسيدات",
            altvar = "كرة السلة",
            mw = "سيدات",
            age = "تحت " .. age
        },
        ["cr"] = {
            linkpre = "منتخب",
            altlink = "للكريكت",
            altvar = "كريكت"
        },
        ["cr19"] = {
            linkpre = "منتخب",
            altlink = "تحت 19 سنة للكريكت",
            altvar = "cricket",
            age = "تحت 19"
        },
        ["crw"] = {
            linkpre = "منتخب",
            altlink = "للكريكت للسيدات",
            altvar = "كريكت",
            mw = "سيدات"
        },
        ["cycling"] = {
            linkpre = "منتخب",
            altlink = "لسباق الدراجات الهوائية",
            altvar = "سباق الدراجات الهوائية",
            mw = "cycling"
        },
        ["davis"] = {
            linkpre = "منتخب",
            altlink = "لكأس ديفيز",
            altvar = "tennis",
            mw = "Davis"
        },
        ["fb"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم",
            altvar = "كرة قدم"
        },
        ["fba"] = {
            linkpre = "منتخب",
            linkpre = "اتحاد",
            altlink = "لكرة القدم",
            altvar = "football"
        },
        ["fbl"] = {
            linkpre = "منتخب",
            altlink = "المحلي لكرة القدم",
            altvar = "كرة قدم", -- football
            mw = "محلي" -- A'
        },
        ["fbm"] = {
            linkpre = "منتخب",
            altlink = "العسكري لكرة القدم",
            altvar = "football"
        },
        ["fbo"] = {
            linkpre = "منتخب",
            altlink = "الأولمبي لكرة القدم",
            altvar = "football",
            mw = "Olympic"
        },
        ["fbu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة القدم",
            altvar = "كرة قدم",
            age = "تحت " .. age
        },
        ["fbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة القدم للسيدات",
            altvar = "كرة قدم سيدات",
            mw = "سيدات"
        },
        ["fbwm"] = {
            linkpre = "منتخب",
            altlink = "العسكري لكرة القدم للسيدات",
            altvar = "football",
            mw = "سيدات"
        },
        ["fbwu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة القدم للسيدات",
            altvar = "كرة قدم",
            mw = "السيدات",
            age = "تحت " .. age
        },
        ["fed"] = {
            linkpre = "منتخب",
            altlink = "لكأس فيد",
            altvar = "كأس فيد",
            mw = "Fed"
        },
        ["fhm"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الحقل للرجال",
            altvar = "هوكي الحقل",
            mw = "رجال"
        },
        ["fhw"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الحقل للسيدات",
            altvar = "هوكي الحقل سيدات",
            mw = "سيدات"
        },
        ["fl"] = {
            linkpre = "منتخب",
            altlink = "لكرة الأرض للرجال",
            altvar = "كرة أرض",
            mw = "رجال"
        },
        ["flu"] = {
            linkpre = "منتخب",
            altlink = "تحت 19 سنة لكرة الأرض للرجال",
            altvar = "كرة أرض تحت 19 سنة",
            mw = "رجال",
            age = "تحت 19"
        },
        ["flw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الأرض للسيدات",
            altvar = "كرة أرض سيدات",
            mw = "سيدات"
        },
        ["flwu"] = {
            linkpre = "منتخب",
            altlink = "تحت 19 سنة لكرة الأرض للسيدات",
            altvar = "كرة أرض سيدات تحت 19 سنة",
            mw = "سيدات",
            template = "flw19"
        },
        ["fsw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الصالات للسيدات",
            altvar = "كرة صالات سيدات",
            mw = "سيدات"
        },
        ["fs"] = {
            linkpre = "منتخب",
            altlink = "لكرة الصالات",
            altvar = "كرة الصالات",
            template = "futsal"
        },
        ["fsu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة الصالات",
            altvar = "كرة الصالات",
            age = "تحت " .. age
        },
        ["gb"] = {
            linkpre = "منتخب",
            altlink = "لكرة الهدف للرجال",
            altvar = "كرة هدف رجال",
            mw = "رجال"
        },
        ["gbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الهدف للسيدات",
            altvar = "كرة هدف سيدات",
            mw = "سيدات"
        },
        ["hb"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد",
            altvar = "كرة اليد"
        },
        ["hbj"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد للناشئين",
            altvar = "كرة يد ناشئين"
        },
        ["hbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد للسيدات",
            altvar = "كرة يد سيدات",
            mw = "سيدات"
        },
        ["hbwj"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد للناشئات",
            altvar = "كرة يد ناشئات"
        },
        ["hbwy"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد للشابات",
            altvar = "كرة يد شابات",
            mw = "للشابات"
        },
        ["hby"] = {
            linkpre = "منتخب",
            altlink = "لكرة اليد للشباب",
            altvar = "كرة يد شباب"
        },
        ["hopman"] = {
            altlink = "في كأس هوبمان",
            altvar = "tennis",
            mw = "هوبمان"
        },
        ["iflm"] = {
            linkpre = "منتخب",
            altlink = "lacrosse للرجال",
            altvar = "lacrosse",
            mw = "رجال"
        },
        ["iflw"] = {
            linkpre = "منتخب",
            altlink = "lacrosse للسيدات",
            altvar = "lacrosse",
            mw = "سيدات"
        },
        ["iflwu"] = {
            linkpre = "منتخب",
            altlink = "تحت 19 سنة lacrosse للسيدات",
            altvar = "lacrosse",
            mw = "سيدات",
            template = "iflwu19"
        },
        ["ih"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الجليد",
            altvar = "هوكي الجليد",
            mw = "رجال"
        },
        ["ih18"] = {
            linkpre = "منتخب",
            altlink = "تحت 18 سنة لهوكي الجليد للرجال",
            altvar = "هوكي الجليد",
            mw = "رجال",
            age = "تحت 18"
        },
        ["ihj"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الجليد للناشئين",
            altvar = "هوكي الجليد ناشئين",
            mw = "رجال",
            age = "ناشئين"
        },
        ["ihw"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الجليد للسيدات",
            altvar = "هوكي الجليد سيدات",
            mw = "سيدات"
        },
        ["ihw18"] = {
            linkpre = "منتخب",
            altlink = "تحت 18 سنة لهوكي الجليد للسيدات",
            altvar = "هوكي الجليد سيدات تحت 18 سنة",
            mw = "سيدات",
            age = "U-18"
        },
        ["iih"] = {
            linkpre = "منتخب",
            altlink = "inline hockey للرجال",
            altvar = "inline hockey"
        },
        ["iihj"] = {
            linkpre = "منتخب",
            altlink = "inline hockey للناشئين",
            altvar = "inline hockey",
            mw = "junior"
        },
        ["iihw"] = {
            linkpre = "منتخب",
            altlink = "inline hockey للسيدات",
            altvar = "inline hockey",
            mw = "سيدات"
        },
        ["iil"] = {
            linkpre = "منتخب",
            altlink = "indoor lacrosse",
            altvar = "lacrosse",
            mw = "رجال"
        },
        ["ish"] = {
            linkpre = "منتخب",
            altlink = "ice sledge hockey للرجال",
            altvar = "sledge hockey"
        },
        ["kd"] = {
            linkpre = "منتخب",
            altlink = "للكابادي",
            altvar = "Kabaddi"
        },
        ["korf"] = {
            linkpre = "منتخب",
            altlink = "للكورفبال",
            altvar = "korfball"
        },
        ["nb"] = {
            linkpre = "منتخب",
            altlink = "لكرة الشبكة",
            altvar = "netball"
        },
        ["p&p"] = {
            linkpre = "منتخب",
            altlink = "pitch and putt للرجال",
            altvar = "pitch and putt"
        },
        ["rhm"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الدحرجة",
            altvar = "هوكي الدحرجة",
            mw = "رجال"
        },
        ["rhw"] = {
            linkpre = "منتخب",
            altlink = "لهوكي الدحرجة للسيدات",
            altvar = "هوكي الدحرجة",
            mw = "سيدات"
        },
        ["rl"] = {
            linkpre = "منتخب",
            altlink = "لدوري الرغبي",
            altvar = "دوري الرغبي"
        },
        ["rollerderby"] = {
            linkpre = "منتخب",
            altlink = "لدربي الانزلاق",
            altvar = "دربي الانزلاق",
            template = "RollerDerby"
        },
        ["ru"] = {
            linkpre = "منتخب",
            altlink = "لاتحاد الرغبي",
            altvar = "اتحاد الرغبي"
        },
        ["ru7"] = {
            linkpre = "منتخب",
            altlink = "لسباعيات الرغبي",
            altvar = "سباعيات الرغبي"
        },
        ["ru7w"] = {
            linkpre = "منتخب",
            altlink = "لسباعيات الرغبي للسيدات",
            altvar = "سباعيات الرغبي",
            mw = "سيدات"
        },
        ["rua"] = {
            altlink = "A national rugby union team",
            altvar = "rugby union-a",
            template = "ruA"
        },
        ["ruu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لاتحاد الرغبي",
            altvar = "اتحاد الرغبي",
            age = "تحت " .. age
        },
        ["ruw"] = {
            linkpre = "منتخب",
            altlink = "لاتحاد الرغبي للسيدات",
            altvar = "اتحاد الرغبي",
            mw = "سيدات"
        },
        ["sb"] = {
            linkpre = "منتخب",
            altlink = "للكرة اللينة للرجال",
            altvar = "كرة لينة",
            mw = "رجال"
        },
        ["sbw"] = {
            linkpre = "منتخب",
            altlink = "للكرة اللينة للسيدات",
            altvar = "كرة لينة",
            mw = "سيدات"
        },
        ["speedway"] = {
            linkpre = "منتخب",
            altlink = "speedway",
            altvar = "speedway"
        },
        ["speedwaylt"] = {
            linkpre = "منتخب",
            altlink = "long track",
            altvar = "long track",
            template = "speedwayLT"
        },
        ["svb"] = {
            linkpre = "منتخب",
            altlink = "لكرة الطائرة جلوس للرجال",
            altvar = "sitting volleyball",
            mw = "رجال"
        },
        ["svbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الطائرة جلوس للسيدات",
            altvar = "sitting volleyball",
            mw = "سيدات"
        },
        ["vb"] = {
            linkpre = "منتخب",
            altlink = "لكرة الطائرة للرجال",
            altvar = "كرة الطائرة",
            mw = "رجال"
        },
        ["vbu"] = {
            linkpre = "منتخب",
            altlink = "تحت " .. age .. " سنة لكرة الطائرة للرجال",
            altvar = "كرة الطائرة",
            mw = "رجال",
            age = "تحت " .. age
        },
        ["vbw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الطائرة للسيدات",
            altvar = "كرة الطائرة",
            mw = "سيدات"
        },
        ["wbk"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة على الكراسي المتحركة للرجال",
            altvar = "wheelchair basketball"
        },
        ["wbkw"] = {
            linkpre = "منتخب",
            altlink = "لكرة السلة على الكراسي المتحركة للسيدات",
            altvar = "wheelchair basketball",
            mw = "سيدات"
        },
        ["whr"] = {
            linkpre = "منتخب",
            altlink = "للرغبي للكراسي المتحركة",
            altvar = "wheelchair rugby"
        },
        ["wightman"] = {
            linkpre = "منتخب",
            altlink = "لكأس ويتمان",
            altvar = "tennis",
            mw = "Wightman"
        },
        ["wp"] = {
            linkpre = "منتخب",
            altlink = "لكرة الماء",
            altvar = "water polo"
        },
        ["wpw"] = {
            linkpre = "منتخب",
            altlink = "لكرة الماء للسيدات",
            altvar = "water polo",
            mw = "سيدات"
        },
        ["military"] = {
            altlink = "القوات المسلحة",
            altvar = "military",
            template = "قوات مسلحة"
        },
        ["airforce"] = {
            linkpre = "",
            altlink = "القوات الجوية",
            altvar = "air force",
            template = "air force"
        },
        ["army"] = {
            linkpre = "",
            altlink = "الجيش",
            altvar = "army"
        },
        ["navy"] = {
            linkpre = "",
            altlink = "القوات البحرية",
            altvar = "naval"
        }
    }

    local alt_aliases = {
        ["armed forces"] = "military",
        ["a'football"] = "fbl",
        ["americanfootball"] = "af",
        ["australianrulesfootball"] = "arf",
        ["badminton"] = "bd",
        ["baseball"] = "bb",
        ["baseballstate"] = "bbstate",
        ["beachhandball"] = "bh",
        ["beachsoccer"] = "bs",
        ["beachsoccerwomen"] = "bsw",
        ["basketball"] = "bk",
        ["cricket"] = "cr",
        ["daviscup"] = "davis",
        ["Flb"] = "fl",
        ["fedcup"] = "fed",
        ["football"] = "fb",
        ["footballassociation"] = "fba",
        ["fh"] = "fhm",
        ["fieldhockey"] = "fhm",
        ["fl19"] = "flu",
        ["floorball"] = "fl",
        ["flw19"] = "flwu",
        ["flwu19"] = "flwu",
        ["futsal"] = "fs",
        ["goalball"] = "gb",
        ["handball"] = "hb",
        ["hopmancup"] = "hopman",
        ["icehockey"] = "ih",
        ["icesledgehockey"] = "ish",
        ["iflwu19"] = "iflwu",
        ["indoorlacrosse"] = "iil",
        ["inlinehockey"] = "iih",
        ["junioramericanfootball"] = "afu",
        ["juniorhandball"] = "hbj",
        ["junioricehockey"] = "ihj",
        ["juniorinlinehockey"] = "iihj",
        ["kabaddi"] = "kd",
        ["korfball"] = "korf",
        ["lacrosse"] = "iflm",
        ["men'sfieldhockey"] = "fhm",
        ["men'slacrosse"] = "iflm",
        ["militaryfootball"] = "fbm",
        ["netball"] = "nb",
        ["olympicfootball"] = "fbo",
        ["pitchandputt"] = "p&p",
        ["rollerhockey"] = "rhm",
        ["rugbyleague"] = "rl",
        ["rugbysevens"] = "ru7",
        ["rug"] = "ru",
        ["rugbyunion"] = "ru",
        ["rugbyuniona"] = "rua",
        ["rugbyunionsevens"] = "ru7",
        ["softball"] = "sb",
        ["longtrack"] = "speedwaylt",
        ["sittingvolleyvall"] = "svb",
        ["under18icehockey"] = "ih18",
        ["under19cricket"] = "cr19",
        ["volleyball"] = "vb",
        ["waterpolo"] = "wp",
        ["wheelchairbasketball"] = "wbk",
        ["wheelchairrugby"] = "whr",
        ["wightmancup"] = "wightman",
        ["women'samericanfootball"] = "afw",
        ["women'saustralianrulesfootball"] = "arfw",
        ["women'sbandy"] = "bandyw",
        ["women'sbaseball"] = "bbw",
        ["women'sbasketball"] = "bkw",
        ["women'sbeachhandball"] = "bhw",
        ["women'sbeachsoccer"] = "bsw",
        ["women'scricket"] = "crw",
        ["women'sfieldhockey"] = "fhw",
        ["women'sfloorball"] = "flw",
        ["women'sfootball"] = "fbw",
        ["women'sfutsal"] = "fsw",
        ["women'sgoalball"] = "gbw",
        ["women'shandball"] = "hbw",
        ["women'sicehockey"] = "ihw",
        ["women'sinlinehockey"] = "iihw",
        ["women'srollerhockey"] = "rhw",
        ["women'sunder18icehockey"] = "ihw18",
        ["women'sjuniorhandball"] = "hbwj",
        ["women'slacrosse"] = "iflw",
        ["women'smilitaryfootball"] = "fbwm",
        ["women'srugbysevens"] = "ru7w",
        ["women'srugbyunion"] = "ruw",
        ["women'srugbyunionsevens"] = "ru7w",
        ["women'ssittingvolleyvall"] = "svbw",
        ["women'ssoftball"] = "sbw",
        ["women'svolleyball"] = "vbw",
        ["women'swaterpolo"] = "wpw",
        ["women'swheelchairbasketball"] = "wbkw",
        ["women'syouthbasketball"] = "bkwu",
        ["women'syouthfootball"] = "fbwu",
        ["women'syouthhandball"] = "hbwy",
        ["youthbasketball"] = "bku",
        ["youthcricket"] = "cr19",
        ["youthfootball"] = "fbu",
        ["youthhandball"] = "hby",
        ["youthrugbyunion"] = "ruu"
    }

    return alt_map, alt_aliases
end

function p.list_all(frame)
    --Load data
    local alt_map, alt_aliases = p.alttable('\'\'<span style="color:green;">age</span>\'\'')

    --Add aliases to map table
    for k, v in pairs(alt_map) do
        alt_map[k].aliases = {}
    end
    local wrong_aliases = {}
    table.sort(alt_aliases)
    for k, v in pairs(alt_aliases) do
        if alt_map[v] then
            table.insert(alt_map[v].aliases, k)
        else
            table.insert(wrong_aliases, k)
        end
    end
    local template_name = frame.args['template_name'] or 'Flagg'
    --Create html table & header row
    local list = mw.html.create("table")
    list:addClass("wikitable sortable collapsible")
    local head = list:tag("tr")
    head:tag("th"):wikitext("الكود")
    head:tag("th"):wikitext("البدائل")
    head:tag("th"):wikitext("بادئة الوصلة")
    head:tag("th"):wikitext("الرابط البديل (لاحقة)")
    head:tag("th"):wikitext("Altvar")
    head:tag("th"):wikitext("القالب")
    head:tag("th"):wikitext("مثال")
    --Append rows to create table body
    table.sort(alt_map)
    for k, v in pairs(alt_map) do
        --if k ~= "airforce" and k ~= "army" and k ~= "navy" and k ~= "fba" then
            local country = "USA"
            if frame.args[k] and frame.args[k] ~= "" then
                country = frame.args[k]
            end
            local row = list:tag("tr")
            local temp = alt_map[k].template or k
            row:tag("td"):wikitext("<code>" .. k .. "</code>")
            row:tag("td"):wikitext("<code>" .. table.concat(alt_map[k].aliases, "</code><br/><code>") .. "</code>")
            row:tag("td"):wikitext((alt_map[k].linkpre or ""))
            row:tag("td"):wikitext(alt_map[k].altlink)
            row:tag("td"):wikitext(alt_map[k].altvar)
            row:tag("td"):wikitext("[[قالب:" .. temp .. "|" .. temp .. "]] ([[:en:Template:" .. temp .. "|en]])")
            
            row:tag("td"):wikitext(
                mw.getCurrentFrame():expandTemplate {
                    title = template_name,
                    args = {
                        "unf",
                        country,
                        avar = k,
                        pref = "منتخب"
                    }
                }
            )
            mw.log("{{" .. template_name .. "|unf|" .. country .. "|avar=" .. k .. "}}")
        --end
    end
    if #wrong_aliases > 0 then
        local wrong_list =
            "These aliases point to non-existing data: <code>" ..
            table.concat(wrong_aliases, "</code> <code>") .. "</code>"
        list:tag("tr"):tag("td"):attr("colspan", "4"):css("color", "red"):wikitext(wrong_list)
    end

    return list
end

return p