local p = {}

millennia = {}
millennia[-1] = {-1}
millennia[1] = {1,2,3,4,5,6,7,8,9,10}
millennia[2]= {11,12,13,14,15,16,17,18,19,20} 
millennia[3]= {21,22,23,24,25,26,27,28,29,30}

centries = {}
decades = {}
years = {}
decades_list = {}
decades_list[1] = {'0', '10', '20', '30', '40', '50', '60', '70', '80', '90'}
decades_list[-1] = {'-90', '-80', '-70', '-60', '-50', '-40', '-30', '-20', '-10'}
--===============================
tato = {0,1,2,3,4,5,6,7,8,9}
--===============================
for elff, elff_t in pairs(millennia) do
	--mw.log( " --------------------------------------- " )
	--mw.log( " elff:" .. elff )
	for _, centry in pairs(elff_t) do
		centry2 = tostring(centry - 1)
		if centry2 == "0" then centry2 = "" end
		--mw.log("========")
		--mw.log("centry :" .. centry )
		centries[tostring(centry)] =  { ["millennia"] = elff } 
		de_de = {}
		if decades_list[centry] then
			de_de = decades_list[centry]
		else 
			--de_de = [ "%s%d0" % (centry2,x) for x in range(0,10) ]
			for num, ii in pairs(tato) do
				so = centry2 .. tostring(ii) .. "0"
				table.insert(de_de , so)
			end
		end
		--mw.log("de_de :" .. mw.text.listToText( de_de , "," , "," ) )
		for r , dic in pairs(de_de) do
			if dic == "00" then dic = "0" end
			--mw.log( " ----------- dic : " .. dic  )
			decades[ dic ] = { ["centry"] = centry , ["millennia"] = elff }
			yea = {}
			if tonumber(dic) < 1 then
				--yea = [ tonumber(dic) - x for x in range(0,10) ]
				for no, iii in pairs(tato) do
					se = tonumber(dic) - tonumber(iii)
					table.insert(yea , se)
				end
			else
				--yea = [ tonumber(dic) + x for x in range(0,10) ]
				for no, iii in pairs(tato) do
					se = tonumber(dic) + tonumber(iii)
					table.insert(yea , se)
				end
			end
			for __, year in pairs(yea) do
				--mw.log( " year : " .. year  )
				year2 = tostring(year)
				if years[ year2 ] then
					mw.log( " year2 : " .. year2 " are in "  .. dic .." and " .. years[ year2 ]["decade"] )
				else
					years[ year2 ] = {["decade"] = dic , ["centry"] = centry, ["millennia"] = elff  }
				end
			end
			--mw.log("yea :" .. mw.text.listToText( yea , "," , "," ) )
		end
	end
   --mw.log( " --------------------------------------- " )
end

function p.getmillennia(frame)
	local str =  ""
	local ask = frame.args.ask
	local year = frame.args.y
	local decade = frame.args.d
	local centry = frame.args.c
	if year and years[year] then
		str = years[year]["millennia"]
	elseif decade and decades[decade] then
		str = decades[decade]["millennia"]
	elseif centry and centries[centry] then
		str = centries[centry]["millennia"]
	end
	return str
end

function p.getcentry(frame)
	local str =  ""
	local ask = frame.args.ask
	local year = frame.args.y
	local decade = frame.args.d
	if year and years[year] then
		str = years[year]["centry"]
	elseif decade and decades[decade] then
		str = decades[decade]["centry"]
	end
	return str
end

function p.getdecade(frame)
	local str =  ""
	local ask = frame.args.ask
	local year = frame.args.y
	if year and years[year] then
		str = years[year]["decade"]
	end
	return str
end

return p