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

--[[
For translation of this module, you must customize:
Module:Global infobox tools/i18n
Module:Infobox disease/i18n
--]]
local p = {}

local SA	= require "Module:SimpleArgs"
local SD	= require "Module:SimpleDebug"
local WD	= require "Module:Wikidades"
local MLMT	= require "Module:Multilang module tools"
local GIBTi	= require "Module:Global infobox tools/items"
local GIBT	= require "Module:Global infobox tools"
local MIIi	= require "Module:Medical infobox items/items"
local MII	= require "Module:Medical infobox items"
local IDi	= require "Module:Infobox disease/items"
local GIBTi18n
local IDi18n

local ModuleName = 'Infobox disease'
local rs = { --reserved keys and their modified values for infobox configuration
	--for default stored values of the other options look at list with allitems=list
	[GIBTi.rk.rs_image_max_num]		= 2,
	[GIBTi.rk.rs_changeable_lbls]	= false,
}
local cat = { --tracking categories
	[GIBTi.rk.rs_cat_arg_error]		= "_Q90498582", -- specific category: "_Q90498582",
	[GIBTi.rk.rs_cat_wds_untranslat]= "_Q90778219",
}
local items = { --used key names 
	[MLMT.k.Args] = { --parameter key, its name/s (as parameter) and optional Wikidata qualifiers/properties
		[GIBTi.rk.name]			= {{"name","Name"},         ""}, --allways required
		[IDi.k.type]			= {"type",                  "_P279 OR P31"},
		[IDi.k.synonym]			= {{"Synonym","synonym",
                                    "Synonyms","synonyms"}, ""},
		[IDi.k.eponym]			= {"eponym",                "_P138"},
		[IDi.k.specialty]		= {{"field","Field",
                                   "specialty","Specialty",
                                 "Speciality","speciality"},"_P1995"},
		
		[IDi.k.symptoms]		= {"symptoms",              "_P780"},
		[IDi.k.complication]	= {"complications",         ""},
		[IDi.k.onset]			= {"onset",                 ""},
		[IDi.k.duration]		= {"duration",              ""},
		[IDi.k.types]			= {"types",                 ""},
		[IDi.k.causes]			= {{"causes","cause"},      ""},
		[IDi.k.risks]			= {{"risks","risk"},        ""},
		[IDi.k.diagnosis]		= {"diagnosis",             ""},
		[IDi.k.exams]			= {"examinations",          "_P923"},
		[IDi.k.diff_diag]		= {{"differential",
                                    "differential diagnosis",
                                    "diff"},                ""},
		[IDi.k.prevention]		= {"prevention",            ""},
		[IDi.k.treatment]		= {"treatment",             "_P924"},
		[IDi.k.drug_used]		= {"drug used",             "_P2176"},
		[IDi.k.prognosis]		= {"prognosis",             ""},
		[IDi.k.frequency]		= {"frequency",             ""},
		[IDi.k.deaths]			= {"deaths",                ""},
		[IDi.k.d_burden]		= {"disease burden",        "_P2854"},
		[IDi.k.measured_by]		= {"measured by",           "_P1880"},
		
		[IDi.k.location]		= {"location",              "_P927"},
		[IDi.k.genetic]			= {"genetic association",   "_P2293"},
		[IDi.k.afflicts]		= {"afflicts",              "_P689"},
		[IDi.k.transmitted]		= {"transmitted by",        "_P1060"},
		[IDi.k.has_cause]		= {"has cause",             "_P828"},
		[IDi.k.has_effec]		= {"has effect",            "_P1542"},

		--Old 32, 33, 34, etc.
		[IDi.k.ICD10]			= {"ICD10",                 {property="P494 OR P4229",
                                                             formatting='[http://apps.who.int/classifications/icd10/browse/2010/en#/$1 $1]',
                                                             list='firstrank'}},
		[IDi.k.ICD9]			= {"ICD9",                  {property="P493 OR P1692", 
                                                             formatting='[http://www.saniwiki.cat/ICD/php-09/$1.php $1]',
                                                             list='firstrank'}},
		[IDi.k.ICD_O]			= {{"ICD-O","ICDO"},		{property="P563",
                                                             formatting='[http://www.progenetix.org/cgi-bin/subsets.cgi?project=progenetix&subsetType=ICDM&ICDMcodes_m=$1 $1]'}},
		[IDi.k.ICPC]			= {"ICPC",                  {property="P667",
                                                             formatting='[https://www.hetop.eu/hetop/3CGP/en/?rr=CIP_D_$1 $1]'}},
		[IDi.k.DSM]				= {"DSM",                   "_P1930"},

		[IDi.k.OMIM]			= {"OMIM",                  {property="P492",
                                                             formatting='[https://omim.org/entry/$1 $1]'}},
		[IDi.k.OMIM_mult]		= {"OMIM_mult",             ""},
		[IDi.k.DiseasesDB]		= {"DiseasesDB",            {property="P557",
                                                             formatting='[http://www.diseasesdatabase.com/ddb$1.htm $1]'}},
		[IDi.k.DiseasesDB_mult]	= {"DiseasesDB_mult",		""},
		[IDi.k.MedlineP]		= {"MedlinePlus",           {property="P604",
                                                             formatting='[http://www.nlm.nih.gov/medlineplus/ency/article/$1.htm $1]'}},
		[IDi.k.MedlineP_mult]	= {"MedlinePlus_mult",      ""},
		[IDi.k.NCI]				= {"NCI",                   {property="P1395",
                                                             formatting='[http://www.cancer.gov/$1 $1]'}},
		[IDi.k.P_UK]			= {"Patient UK",            {property="P1461",
                                                             formatting='[http://patient.info/doctor/$1 $1]'}},
		[IDi.k.Orphanet]		= {"Orphanet",              {property="P1550",
															 formatting='[https://www.orpha.net/ORDO/Orphanet_$1 $1]'}},
		[IDi.k.umls_cui]		= {"umls_cui",              {property="P2892",
                                                             formatting='[https://ncim-stage.nci.nih.gov/ncimbrowser/ConceptReport.jsp?dictionary=NCI%20Metathesaurus&code=$1 $1]'}},
		[IDi.k.doid]			= {"doid",                  {property="P699",
                                                             formatting='[http://www.disease-ontology.org/?id=$1 $1]'}},
	},
	[MLMT.k.Labels] = { --label or header (hd_) key, and its value as Wikidata qualifier or property to read (or text)
		[IDi.k.type]			= "_P279",
		[IDi.k.synonym]			= "_Q42106",
		[IDi.k.eponym]			= "_Q204335",
		[IDi.k.specialty]		= "_Q930752",
		
		[IDi.k.hd_Clinical]		= "Clinical manifestations",
		[IDi.k.hd_ClinicManag]	= "Clinical manifestations & management",
		[IDi.k.symptoms]		= "_P780",
		[IDi.k.complication]	= "_Q1346149",
		[IDi.k.onset]			= "Usual onset",
		[IDi.k.duration]		= "_Q2199864",
		[IDi.k.types]			= "_Q21146257",
		[IDi.k.causes]			= "_Q5850078",
		[IDi.k.risks]			= "_Q104493",
		[IDi.k.diagnosis]		= "_Q177719",
		[IDi.k.exams]			= "_Q1355298", 
		[IDi.k.diff_diag]		= "_Q837512",
		[IDi.k.prevention]		= "_Q1717246",	
		[IDi.k.treatment]		= "_Q179661",
		[IDi.k.drug_used]		= "_P2176",
		[IDi.k.prognosis]		= "_Q592442",
		[IDi.k.frequency]		= "_Q11652",
		[IDi.k.deaths]			= "_P1120",
		[IDi.k.d_burden]		= "_P2854",
		[IDi.k.measured_by]		= "_P1880",
	
		[IDi.k.hd_Pathos]		= "_Q372016",
		[IDi.k.location]		= "_Q17334923",
		[IDi.k.genetic]			= "_P2293",
		[IDi.k.afflicts]		= "_P689",
		[IDi.k.transmitted]		= "_P1060",
		[IDi.k.has_cause]		= "_P828",
		[IDi.k.has_effec]		= "_P1542", 
		
		[IDi.k.hd_Classif]		= "_Q5962346",
		[IDi.k.ICD10]			= "_Q45127",
		[IDi.k.ICD9]			= "_Q14067712",
		[IDi.k.ICD_O]			= "_Q1430635",
		[IDi.k.ICPC]			= "_Q723530",
		[IDi.k.DSM]				= "_Q201561",
		
		[IDi.k.hd_ExtRes]		= "External resources", 
		[IDi.k.OMIM]			= "_Q241953",
		[IDi.k.DiseasesDB]		= "_Q213103",
		[IDi.k.MedlineP]		= "_Q557919",
		[IDi.k.NCI]				= "_Q664846",
		[IDi.k.P_UK]			= "_Q17017504",
		[IDi.k.Orphanet]		= "_Q1515833",
		[IDi.k.umls_cui]		= "_Q455338",
		[IDi.k.doid]			= "_Q5282129",
	},	
}

function p.infobox(frame)
	local args, NArgs = SA.GetArgs (frame)
	MLMT.SetLang (args)
	GIBTi18n = require (SA.I18nName ("Global infobox tools"))
	IDi18n   = require (SA.I18nName ("Infobox disease"))
	GIBT.SetColorsAndIcon (ModuleName,
		GIBTi18n.lcol.science, --group color
		GIBTi18n.licon.medical_condition) --icon key, and optional hint
	items, IDi18n.items = GIBT.CheckArgLabLua (ModuleName, items, IDi18n.items, IDi18n.new_items, IDi18n.omit_params, IDi18n.preset_params)
	items = GIBT.AddLocalAdminItems (items, IDi18n.new_items)
	IDi.idx = GIBT.CheckIdx_arglabLua (ModuleName, IDi.idx, IDi18n.new_idx)
	IDi.arg_lims = GIBT.CheckAddLocalAdminLims (ModuleName, IDi.arg_lims, IDi18n.new_lims)
	args = GIBT.SetRsv_1ToArgs (ModuleName, frame, args, rs, IDi18n.rs_val, cat)
	local itemsM = GIBT.tableMerge (ModuleName, items, IDi18n.items)
	GIBT.i_LoadI18n ()
	MII.LoadI18n ()
	MLMT.IniLang (frame, args)
	SA.Error.handle = true
	--END INITIALIZATION

	function load_key (key)
		return GIBT.load_key (args, itemsM, key, IDi.arg_lims, IDi18n.untranslat_args)
	end	
	function load_show_key (type_al, key)
		if string.sub (type_al,1,1) == 'c' then
			return GIBT.AddTitCollapsibleText (args, itemsM, key, load_key(key))
		else	
			return GIBT.load_show_key (args, itemsM, key, IDi.arg_lims, IDi18n.untranslat_args)
		end	
	end --load_show_key

	function ArgMMI (option, key)
		return MII.arg_items (option, key)
	end
	--BEGIN--
	local res = GIBT.ItemList_or_TempData (args, 'ID', ModuleName, items, IDi18n.items, IDi.idx, IDi.arg_lims, 
		{{'',MII.arglab_items}}, {{'',ArgMMI}})
	if res == nil then
		GIBT.IniLua (args, itemsM)

		local management
		function Treat (type_al, key)
			 local Exists = load_show_key (type_al, key)
			 if Exists then
			 	management = true
			 end	
		end --Treat
		
		local ThePairs = {
			{IDi.k.OMIM,		IDi.k.OMIM_mult},
			{IDi.k.DiseasesDB,	IDi.k.DiseasesDB_mult},
			{IDi.k.MedlineP,	IDi.k.MedlineP_mult},
		}
		function IsPair (key)
			for _, i in ipairs(ThePairs) do 
				if key == i[1] then
					return true
				end	
			end	
			return false
		end	--IsPair
		function IsNoPair (key)
			for _, i in ipairs(ThePairs) do 
				if key == i[2] then
					return true
				end	
			end	
			return false
		end	--IsNoPair
		function WhenPair (type_al, key)
			for _, i in ipairs(ThePairs) do 
				if key == i[1] then
					local val = load_key (key)
					if val ~= nil then
						local val2 = load_key (i[2])
						if val2 ~= nil then
							val = val..' '..val2
						end
						GIBT.show (args, itemsM, key, val)
					end	
					break
				end	
			end	
		end	--WhenPair
		
		function eMed ()
			MII.eMedicine (args)
		end	
		function Mesh ()
			MII.mesh (frame, args)
		end
		function Gene ()
			MII.gene (frame, args)
		end	
		
		local n_last_capt = GIBT.i_main (frame, args)
		local lab_arg = {}
		for _, i in ipairs(IDi.idx) do
			local key = i[2]
			if MLMT.IsReserv (key) or IsNoPair (key) or (key == GIBTi.rk.name) or (key == 'Reserved') or (key == GIBT.images) or (key == IDi.k.hd_ClinicManag) then
			else
				local IsH = false
				if MLMT.IsHeader (key) then
					IsH = true
				elseif (key == IDi.k.drug_used) or (key == IDi.k.treatment) then
					f = Treat
				elseif key == MIIi.k.eMed then
					f = eMed
				elseif key == MIIi.k.Mesh then
					f = Mesh
				elseif key == MIIi.k.Gene then
					f = Gene
				elseif IsPair (key) then
					f = WhenPair
				else
					f = ''
				end
				table.insert (lab_arg, {IsH, i[1], key, f})
			end	
		end	
		for _, i in ipairs(lab_arg) do
			local type_al = i[2]
			local key = i[3]
			if i[1] then
				local t = string.sub (type_al,1,1)
				if (t == 'a') or (t == 'c') then
					local val = load_key (key)
					if val ~= nil then
						GIBT.AddHeaderArg (args, itemsM, key, val, frame, t=='c')
					end	
				else	
					GIBT.AddHeader (args, itemsM, key)
				end	
			else
				if i[4] == '' then
					load_show_key (type_al, key)
				else
					i[4](type_al, key)
				end	
			end	
		end
		GIBT.ExecFuncs_SetHeaders (frame, itemsM, IDi18n.local_func, n_last_capt)
		if GIBT.HasAnyValue ({IDi.k.treatment,IDi.k.drug_used}) then
			GIBT.SetOtherHeader (IDi.k.hd_Clinical, MLMT.GetLabel (itemsM, IDi.k.hd_ClinicManag))
		end	
		return GIBT.CheckShowParams ( --it contents, perhaps, the error messages and maintenance categories
					frame, args, 
					--allowed parameters:
					{itemsM, --current module specific arguments
					GIBT.i_itemsM, --image module specific arguments
					MII.itemsM[MIIi.k.Mesh], MII.itemsM[MIIi.k.eMed], MII.itemsM[MIIi.k.Gene]} --Medical infotable items arguments
					)..
		       GIBT.InfoboxWithItsValues () 
	else
		return res
	end	
end --infobox

return p