--[=[
Tests at [[Template:sarritest]] = [[Πρότυπο:sarritest]] {{#invoke:sarritest|main}}
]=]--
local export = {}

local languages = mw.loadData("Module:Languages")
--local m_data = require("Module:quote/data") -- names of authors and works
--local m_alias = require("Module:quote/alias") 
local tinti = require("Module:tin") --tinti function:tin -- chooses the correct acc.singular feminine article την (tin) or τη (ti)


function export.main(frame)
	local args = frame:getParent().args	-- for Templates
--	local args = frame.args		-- invoke
--	local arg1 = frame.args[1] or frame:getParent().args[1] or ''  -- for BOTH

-- parameters
--local lang_iso = args['iso'] or ''
--local lang_name = args['langname'] or ''

local author = args['author']  or ''
	-- or, at [[Module:quote/alias]]
--	local alias = ''
--		if m_alias[author] then
--			alias = author
--			author= m_alias[alias]['al']
--		end
--local author_link = m_data[author]['authorlink'] or ''
--local show_author_link = m_data[author]['showauthorlink'] or ''
local work = args['work'] or ''
--local work_link = m_data[author]['worklink'] or ''
--local show_work_link = m_data[author]['showworklink'] or ''
local chapter = args['chapter'] or ''
--local chapter_link = m_data[author]['chapterlink'] or ''
--local show_chapter_link = m_data[author]['showchaoterlink'] or ''
local verse = args['verse'] or ''
local verse_link = args['verselink'] or ''
-- ?????????? local verse_link = tostring(args['verselink']) or ''
local verse_link2 = ''
local show = ''
local show_verse = args['showverse'] or ''
local show_verse_link = args['showverselink'] or ''




if args['author'] == '' or args['author'] == nil then		-- use OR, not AND
	mw.addWarning('<span style="color:#b22222; font-size:16px;">Παρακαλούμε, επιλέξτε έναν συγγραφέα με |author=</span>')
else

-- HOMER
	if args['author'] == 'όμηρος' or args['author'] == 'Όμηρος' then
		author = '[[:el:w:Όμηρος|<span style="background:#f7f7f7;" title="Όμηρος στη Βικιπαίδεια">Ὅμηρος</span>]]'

		if args['work'] == 'ιλ' or args['work'] == 'Ιλιάδα' or args['work'] == 'Ιλιάς' then
			output1 = '. [[:el:w:Ιλιάδα|<span style="background:#f7f7f7;" title="η Ιλιάδα στη Βικιπαίδεια"><i>Ἰλιάς</i></span>]],  [[:el:s:Ιλιάς'
			if args['chapter'] ~= '' and args['chapter'] ~= nil then
				if chapter ~= mw.ustring.match(chapter, '[αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]') then
					output2 = ''
					output3 = ''
					show = '<span style="color:#b22222; font-size:17px;">Δεν υπάρχει τέτοιος τίτλος ραψωδίας. Επιλέξτε ένα γράμμα του ελληνικού αλφαβήτου.</span> [[Κατηγορία:Λάθη στο Module:quote]]'
				else
				chapter = mw.ustring.upper(chapter, '[αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]')
				output2 =  '/' .. chapter
				if args['verselink'] ~= '' and args['verselink'] ~= nil then
-- how do I change the last number of verse_link (the achors are only 0 or 5)
-- find or match?

					if verse_link == mw.ustring.find(args['verselink'], '[1-4]$') then
						output3 = '#v' .. mw.ustring.sub(args['verselink'], '[1-4]$', '[0]$')

--					if verse_link:match(-1, -1) == '[1234]' then
--						output3 = '#v' .. verse_link:sub(-1, -1) == '[0]'

--					if mw.ustring.find(args['verselink'], -1, -1) == '2' then
--						output3 = '#v' .. mw.ustring.sub(args['verselink'], '2$', '0$')		

-- try with verselink = 30x and chand the x
--elseif mw.ustring.match(verse_link, -1, -1) == 'x' then
--	verse_link2 = mw.ustring.sub(verse_link, 'x$', '0$')	
--	output = '#v' .. verse_link2

-- try with letters hij and substitle the first
--attempt to concatenate global 'output3' (a nil value).
--elseif mw.ustring.find(verse_link, 1, 1) == 'h' then
--	output3 = '#v' .. mw.ustring.sub(verse_link, '[^h]', '[^x]')
elseif verse_link:match(1, 1) == 'h' then
	output3 = '#v' .. verse_link:sub('^[h]', '^[x]')
					
					elseif mw.ustring.match(args['verselink'], '[6789$]') then
--						verse_link2 = mw.ustring.sub(args['verselink'], '[5$]')
						output3 = '#v' .. mw.ustring.sub(args['verselink'], '[6789]$', '[5]$')
					elseif mw.ustring.match(args['verselink'], '[05$]') then
						output3 = '#v' .. verse_link
					end
					if args['showverselink'] ~= '' and args['showverselink'] ~= nil then
						show_verse_link = args['showverselink']
						show = chapter .. ', ' .. show_verse_link
					else
						show = chapter .. ', ' .. verse_link
					end
				else -- if no verselink
					output3 = ''
					if args['showverselink'] ~= '' and args['showverselink'] ~= nil then
						show_verse_link = ''
						show = chapter
					else
						show = chapter
					end
				end
			end -- clofse if chapter is NOT αβγδ...	
			else -- if there is no chapter
				output2 = ''
				output3 = ''
				show = 'στη Βικιθήκη'
			end

			output = author .. 
			output1 .. output2 .. output3 .. '|<span title="αρχαίο κείμενο στη Βικιθήκη" style="border-bottom: 1px dotted #ddd;">' .. show .. '</span>]]'



		elseif args['work'] == 'οδ' or args['work'] == 'Οδύσσεια' then
			output1 = output0 .. '. [[:el:w:Οδύσσεια|<span style="background:#f7f7f7;" title="η Οδύσσεια στη Βικιπαίδεια"><i>Ὀδύσσεια</i></span>]],  [[:el:s:Οδύσσεια'
			if args['chapter'] == mw.ustring.find(chapter, '[αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]') then
				output2 = output1 .. '/' .. mw.ustring.lower(chapter) .. '|' .. mw.ustring.lower(chapter) .. ']]'
			else
				output2 = output1 .. '|στη Βικιθήκη]]'
			end
		elseif args['work'] ~= 'ιλ' and args['work'] ~= 'οδ' and args['work'] ~= '' and args['work'] ~= nil then
			output = '<span style="color:#b22222; font-size:17px;">Δεν υπάρχει τέτοιος τίτλος έργου για τον Όμηρο! Επιλέξτε work=ιλ ή οδ</span> [[Κατηγορία:Λάθη στο Module:quote]]'
		elseif args['work'] == '' or args['work'] == nil then
			output = author
		end -- close work at Homer

	end -- close Homer



end -- close if author



-- =================CLOSE MAIN FUNCTION:
return output
end

return export