local entity = mw.wikibase.getEntityObject

function p.getLabel(frame)
local lang = frame.args[1] or 'ko'
	local id = mw.wikibase.getEntityIdForCurrentPage
	local label = mw.wikibase.getLabelByLang
	return label
end

function p.startTime
	local valueTable = entity.claims[qid]
	local startTime = valueTable[key].qualifiers.P580[1].datavalue.value.time
	startTime = string.match(startTime, '+(%d+)-')
	return startTime
end

function p.endTime
	local valueTable = entity.claims[qid]
	local endTime = valueTable[key].qualifiers.P582[1].datavalue.value.time
	endTime = string.match(endTime, '+(%d+)-')
	return endTime
end

function p.pointInTime
	local valueTable = entity.claims[qid]
	
	if valueTable[key].qualifiers == nil then
		return nil
	elseif valueTable[key].qualifiers['P585'] == nil then
		return nil
	end
	
	local pointInTime = valueTable[key].qualifiers.P585[1].datavalue.value.time
	
	pointInTime = string.match(pointInTime, '+(%d+)-')
	return pointInTime
end

function p.listOfOwners(frame)

	local qid = frame.args[1]

	local valueTable = entity.claims[qid]
	local str
	local i = 1

local startTime, endTime

	if valueTable == nil then
		return nil
	end

	for key in ipairs(valueTable) do
		
		if valueTable[key].mainsnak.datavalue == nil then
			return nil
		end		
		
		local id = valueTable[key].mainsnak.datavalue.value.id
		local koCheck, item = mw.wikibase.getLabelByLang
		local koPage = mw.wikibase.getSitelink(id)
		local pointInTime = p.pointInTime

		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if item ~= nil then

		if i == 1 then 
 if valueTable[key].qualifiers then			
			 if (valueTable[key].qualifiers.P580 and valueTable[key].qualifiers.P582) then 
					startTime = p.startTime
					endTime = p.endTime
					if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
					else
						str = string.format
					end
			 elseif valueTable[key].qualifiers.P580 then
					startTime = p.startTime
					if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
					else
						str = string.format
					end
				elseif valueTable[key].qualifiers.P582 then
					endTime = p.endTime
					if (koCheck ~= nil) and (koPage ~= nil) then
				 	str = string.format
				 else
				 	str = string.format
				 end
				else
					if (koCheck ~= nil) and (koPage ~= nil) then
						str = "' .. item .. ''
					else
						str = "' .. item .. ''
					end
 	end
			elseif pointInTime ~= nil then
				if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
					else
						str = string.format
					end
			else 
				if (koCheck ~= nil) and (koPage ~= nil) then
					str = "' .. item .. ''
				else
					str = "' .. item .. ''
				end
			end
		else
	 if valueTable[key].qualifiers then	
				if (valueTable[key].qualifiers.P580 and valueTable[key].qualifiers.P582) then 
					startTime = p.startTime
					endTime = p.endTime
					if (koCheck ~= nil) and (koPage ~= nil) then
						item = string.format
					else
						item = string.format
					end
					str = string.format
				elseif valueTable[key].qualifiers.P580 then
					startTime = p.startTime
					if (koCheck ~= nil) and (koPage ~= nil) then
						item = string.format
					else
						item = string.format
					end
					str = string.format
				elseif valueTable[key].qualifiers.P582 then
					endTime = p.endTime
					if (koCheck ~= nil) and (koPage ~= nil) then
						item = string.format
					else
						item = string.format
					end
					str = string.format
				else
					if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
					else
						str = string.format
					end
				end
			elseif pointInTime ~= nil then
				if (koCheck ~= nil) and (koPage ~= nil) then
					item = string.format
				else
					item = string.format
				end
				str = string.format
			else
				if (koCheck ~= nil) and (koPage ~= nil) then
					str = string.format
				else
					str = string.format
				end
			end
		end
		
		i = i + 1
		
		end
	end

return str

end

function p.meterialUsed

	local valueTable = entity.claims['P186']
	local str = ''
	local str1 = ''
	local str2 = ''

	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id
		local label = mw.wikibase.getLabel(id)

		if valueTable[key].qualifiers then

			local qualifiers = valueTable[key].qualifiers

			for qkey in pairs(qualifiers) do
				if qualifiers[qkey][1].datavalue.value.id == 'Q861259' then 
					if str1 == '' then
						str1 = label
					else
					str1 = string.format
					end
				end
			end
		else

			if str1 == '' then
				str2 = label
			else
				str2 = string.format
			end
		end
	end

	if str1 == '' then
		str = str2
	else
		str = string.format
	end

return str

end

function p.getFilename(frame)
	
	local property
	
	if frame.args[1] == nil then
		property = 'P18'
	else
		property = frame.args[1]
	end

	local valueTable = entity.claims[property]
	local name

	name = valueTable[1].mainsnak.datavalue.value

return name

end

function p.getLists(frame)
	local property = frame.args[1]
	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	local num = frame.args['num']
	
	if valueTable == nil then
		return nil
	end

	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id
		local check, item = mw.wikibase.getLabelByLang
		local page = mw.wikibase.getSitelink(id)
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if item ~= nil then 
		if i == 1 then
			if (check ~= nil) and (page ~= nil) then
				str = '' .. item .. ''
			else
				str = '' .. item .. ''
			end
		else
			if (check ~= nil) and (page ~= nil) then
				str = str .. ', ' .. item .. ''
			else
				str = str .. ', ' .. item .. ''
			end
		end
		
		i = i + 1
		
		if num ~= nil and num == (i - 1) then
			break	
		end
		
		end

	end

	return str

end

function p.getNotableWorks
	local property = 'P800'
	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	
	if valueTable == nil then
		return nil
	end

	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id
		local koCheck, item = mw.wikibase.getLabelByLang
		local koPage = mw.wikibase.getSitelink(id)
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if item ~= nil then 
		if i == 1 then
			if (koCheck ~= nil) and (koPage ~= nil) then
				str = '' .. item .. ''
			else
				str = '' .. item .. ''
			end
		else
			if (koCheck ~= nil) and (koPage ~= nil) then
				str = str .. ', ' .. item .. ''
			else
				str = str .. ', ' .. item .. ''
			end
		end
		
		i = i+1
		
		end

	end

	return str

end

function p.getAwords
	local property = 'P166'
	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	
	if valueTable == nil then
		return nil
	end
	
	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id
		local koCheck, item = mw.wikibase.getLabelByLang
		local koPage = mw.wikibase.getSitelink(id)
		local pointInTime = p.pointInTime
		
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if item ~= nil then 
		if i == 1 then
			if pointInTime == nil then
				if (koCheck ~= nil) and (koPage ~= nil) then
				str = '' .. item .. ''
				else
				str = '' .. item .. ''
				end
				
			else 
				if (koCheck ~= nil) and (koPage ~= nil) then
				str = '' .. item .. ' '
				else
				str = '' .. item .. ' '
				end
			end
		else
			if pointInTime == nil then
				if (koCheck ~= nil) and (koPage ~= nil) then
				str = str .. '' .. item .. ''
				else
				str = str .. '' .. item .. ''
				end
			else
				if (koCheck ~= nil) and (koPage ~= nil) then
				str = str .. ''
				else
				str = str .. ''
				end
			end
		end
		
		i = i+1
		
		end
	
		
	end

	return str

end

function p.getParents
	local valueTable = entity.claims['P22']
	local valueTable2 = entity.claims['P25']
	local str = ''
	
	if valueTable ~= nil then
		if not valueTable[1].mainsnak.datavalue then
			return nil
		end
		
		local id = valueTable[1].mainsnak.datavalue.value.id
		local koCheck, item = mw.wikibase.getLabelByLang
		local koPage = mw.wikibase.getSitelink(id)
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if (item ~= nil) and (koCheck ~= nil) and (koPage ~= nil) then
			str = string.format("%s(부)", koPage, item)
		elseif item ~= nil then
			str = string.format("%s(부)", id, item)
		end
		
		if valueTable2 ~= nil then
			id = valueTable2[1].mainsnak.datavalue.value.id
			koCheck, item = mw.wikibase.getLabelByLang
			koPage = mw.wikibase.getSitelink(id)
			
			if item == nil then
			item = mw.wikibase.getLabel( id )
			end
			
			if item ~= nil then
				if (koCheck ~= nil) and (koPage ~= nil) then
					item = string.format("%s(모)", koPage, item)
					str = string.format
				elseif item ~= nil then
					item = string.format("%s(모)", id, item)
					str = string.format
				end	
			end
		end
	elseif valueTable2 ~= nil then
		id = valueTable2[1].mainsnak.datavalue.value.id
		koCheck, item = mw.wikibase.getLabelByLang
		koPage = mw.wikibase.getSitelink(id)
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		if (item ~= nil) and (koCheck ~= nil) and (koPage ~= nil) then
			str = string.format("%s(모)", koPage, item)
		elseif item ~= nil then
			str = string.format("%s(모)", id, item)
		end	
	end
	
	return str
	
end

function p.getRelatives(frame)
	
	local property
	if frame.args[1] ~= nil then
		property = frame.args[1]
	else
		property = 'P1038'
	end

	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	
	if valueTable ~= nil then
	
		for key in ipairs(valueTable) do
	
			local id = valueTable[key].mainsnak.datavalue.value.id
			local koCheck, item = mw.wikibase.getLabelByLang
			local koPage = mw.wikibase.getSitelink(id)
			local id2
			local typeOfKinship
			
			if valueTable[key].qualifiers ~= nil then 
				id2 = valueTable[key].qualifiers.P1039[1].datavalue.value.id
				typeOfKinship = mw.wikibase.getLabelByLang
			else
				typeOfKinship = nil
			end
		
			if item == nil then
			item = mw.wikibase.getLabel( id )
			end
		
			if item ~= nil then
		
				if i == 1 then
					
					if typeOfKinship ~= nil then
						if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
						else
						str = string.format
						end
					else
						if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
						else
						str = string.format
						end
					end
				else
					if typeOfKinship ~= nil then
						if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
						else
						str = string.format
						end
					else
						if (koCheck ~= nil) and (koPage ~= nil) then
						str = string.format
						else
						str = string.format
						end
					end
				end
			end
			
			i = i + 1
			
		end
	end
	
	return str
	
end

function p.getGallery
	
	local property = 'P800'
	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	
	if valueTable == nil then
		return nil
	end
	
	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id
		local koCheck, item = mw.wikibase.getLabelByLang
		local koPage = mw.wikibase.getSitelink(id)
		
		if item == nil then
			item = mw.wikibase.getLabel( id )
		end
		
		local rawname = mw.wikibase.getEntityObject(id).claims['P18']

		if rawname ~= nil then
		
			local fileName = rawname[1].mainsnak.datavalue.value
		
			if i == 1 then
				if (koCheck ~= nil) and (koPage ~= nil) then
					str = string.format
				else
					if item ~= nil then
						str = string.format
					else
						str = string.format(""
	end

	return str

end

function p.getGalleryTest
	
	local property = 'P800'
	local valueTable = entity.claims[property]
	local str = ''
	local i = 1
	
	if valueTable == nil then
		return nil
	end
	
	for key in ipairs(valueTable) do

		local id = valueTable[key].mainsnak.datavalue.value.id

		
		local rawname = mw.wikibase.getEntityObject(id).claims['P18']

		if rawname ~= nil then
		
			local fileName = rawname[1].mainsnak.datavalue.value
		
		
			if item == nil then
			item = mw.wikibase.getLabel( id )
			end
		
			if i == 1 then
			
					str = fileName
			else
				
					str = str .. '' .. fileName

			end
		
			i = i+1
		
		end
		
	end

	return str

end