# Module:Infobox television season name

-- This module requires the use of Module:Arguments.  
local getArgs = require('Module:Arguments').getArgs  

local pipedLink = ""  

local p = {}  

-- Local function which is used to create an pipped article link.  
local function createArticleTitleWithPipedLink(article)  
if (pipedLink == nil or pipedLink == "") then  
return ""  
else  
return "" .. pipedLink .. ""  
end  
end  

---
Local function which is used to check if the given article exists.  
The function returns "true" in the following cases:  
-- A season article exists.  
-- A redirect exists to a season section.  

The function returns nil in the following cases:  
-- A season article or redirect do not exist.  
-- A redirect exists, but it is a general redirect and not for any specific season section.  
---
local function checkArticle(articleName)  
local article = mw.title.new(articleName)  
if (article ~= nil and article.exists) then  
local redirectTarget = article.redirectTarget  
if (redirectTarget) then  
local fullLink = redirectTarget.fullText  
local isSection = fullLink:find("#")  
if (isSection) then  
return "true" -- Article is a section redirect; Valid link.  
else  
return nil -- Article is a general redirect; Not a valid link.  
end  
else  
return "true" -- Article exists and is not a redirect; Valid link.  
end  
else  
return nil -- Article or redirect do not exist; Not a valid link.  
end  
end  

---
Local function which is used to create a TV season link.  

The following are the supported season naming styles:  
-- <showName> (<seasonQualifier> <seasonType>)  
Example: Big Brother 1 (American season).  
-- <showName> (<seasonType> <seasonNumber>)  
Example: Lost (season 3).  
-- <showName> (<seasonQualifier> <seasonType> <seasonNumber>)  
Example: The Flash (2014 season 3).  
-- <showName> <preSeasonQualifier> (<seasonType> <seasonNumber>)  
Example: Dallas (1978 TV series) (season 3).  
-- <showName> <preSeasonQualifier> (<seasonQualifier> <seasonType> <seasonNumber>)  
Example: No current example.  
-- <showName> (<preSeasonQualifier>, <seasonType> <seasonNumber>) - TODO  
Example: Teenage Mutant Ninja Turtles (1987 TV series, season 1)  
---
local function createLink(args, prevOrNextSeasonNumber)  
local showName = args[1] or args['show_name']  
if (showName == nil) then  
return ""  
end  

showName = showName .. " "  

local preSeasonQualifier = args[2] or args['pre_season_qualifier']  
if (preSeasonQualifier == nil) then  
preSeasonQualifier = ""  
else  
preSeasonQualifier = preSeasonQualifier .. " "  
end  

local seasonQualifier = args[3] or args['season_qualifier']  
if (seasonQualifier == nil) then  
seasonQualifier = ""  
else  
seasonQualifier = seasonQualifier .. " "  
end  

local seasonType = args[4] or args['season_type']  
if (seasonType == nil) then  
seasonType = "season"  
end  

local realitySeriesStyle = false  
local showNameModified  
local seasonNumber = args[5] or args['season_number']  
if (seasonNumber == nil) then  
if (string.match(showName , "%s+(%d+)")) then  
_, _, showNameModified, seasonNumber = string.find(showName, "(.*)%s+(%d+)")  
realitySeriesStyle = true  
else  
return "" -- Not a valid next/prev season link  
end  
end  

if (tonumber(seasonNumber) == nil) then  
return ""  
else  
seasonNumber = seasonNumber + prevOrNextSeasonNumber  
pipedLink = seasonType:gsub("^%l", string.upper) .. " " .. seasonNumber  

if (realitySeriesStyle) then  
return showNameModified .. " " .. seasonNumber .. " (" .. seasonQualifier .. seasonType .. ")"  
else  
return showName .. preSeasonQualifier .. "(" .. seasonQualifier .. seasonType .. " " .. seasonNumber .. ")"  
end  
end  
end  

-- Local function which is called to create a TV season link for the next season.  
-- Passes the value "1" to increment the current season number.  
local function createNextSeasonArticle(args)  
return createLink(args, 1)  
end  

-- Local function which is called to create a TV season link for the previous season.  
-- Passes the value "-1" to decrement the current season number.  
local function createPreviousSeasonArticle(args)  
return createLink(args, -1)  
end  

--show_name= — required; The title of TV program.  
-- |pre_season_qualifier= — optional; For cases where disambiguation before the season is required  
(e.g. the pre-season qualifier for Dallas (1978 TV series) (season 7) is (1978 TV series)).  
-- |season_qualifier= — optional; For cases where disambiguation (typically of the show's origin country) before the season type is required  
(e.g. the season qualifier for The Apprentice (American season 2) is American).  
-- |season_type= — optional; To determine the usage of the word "season" for American-based shows, or "series" for shows using British-English.  
-- |season_number= — optional; Season number. The N season of this particular show.  
---
local function makeInvokeFunc(funcName)  
return function  
local args = getArgs  
return p[funcName](args)  
end  
end  

p.checkNextSeason = makeInvokeFunc('_checkNextSeason')  

---
Public function which is used to check if the next season has  
a created article or redirect.  

Parameters: See makeInvokeFunc for documentation.  
---
function p._checkNextSeason(args)  
local articleName = createNextSeasonArticle(args)  
return checkArticle(articleName)  
end  

p.checkPrevSeason = makeInvokeFunc('_checkPrevSeason')  

---
Public function which is used to check if the previous season has  
a created article or redirect.  

Parameters: See makeInvokeFunc for documentation.  
---
function p._checkPrevSeason(args)  
local articleName = createPreviousSeasonArticle(args)  
return checkArticle(articleName)  
end  

p.checkAll = makeInvokeFunc('_checkAll')  

---
Public function which is used to check if the next or previous season have  
a created article or redirect.  

Parameters: See makeInvokeFunc for documentation.  
---
function p._checkAll(args)  
if (p._checkPrevSeason(args) == "true") then  
return "true"  
else  
return p._checkNextSeason(args)  
end  
end  

p.getNextSeasonArticle = makeInvokeFunc('_getNextSeasonArticle')  

---
Public function which is used to get the next season article title.  

Parameters: See makeInvokeFunc for documentation.  
---
function p._getNextSeasonArticle(args)  
local articleTitle = createNextSeasonArticle(args)  
return createArticleTitleWithPipedLink(articleTitle)  
end  

p.getPrevSeasonArticle = makeInvokeFunc('_getPrevSeasonArticle')  

---
Public function which is used to get the previous season article title.  

Parameters: See makeInvokeFunc for documentation.  
---
function p._getPrevSeasonArticle(args)  
local articleTitle = createPreviousSeasonArticle(args)  
return createArticleTitleWithPipedLink(articleTitle)  
end  

return p