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

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function tog(id, i, j)
	local s = ''
	for _, v in ipairs({i, j}) do
		s = s .. ' mw-customtoggle-' .. id .. v .. ' mw-customtoggle-' .. id .. 'c' .. v
	end
	return s
end

local function build(args, key, label, runID, size, stack, bar)
	local n = 0
	while args[key .. (n + 1)] do n = n + 1 end

	local id = runID .. key
	for i = 1, n do
		local hide = i > 1 and ' mw-collapsed' or ''

		stack:tag('div')
			:attr('id', 'mw-customcollapsible-' .. id .. i)
			:addClass('mw-collapsible dface-layer' .. hide)
			:wikitext('[[File:' .. args[key .. i] .. '|' .. size .. 'px|link=]]')

		if n > 1 then
			bar:tag('div')
				:attr('id', 'mw-customcollapsible-' .. id .. 'c' .. i)
				:addClass('mw-collapsible dface-ctrl' .. hide)
				:tag('span'):addClass('dface-label'):wikitext(label):done()
				:tag('span')
					:addClass('dface-btn' .. tog(id, i, (i - 2) % n + 1))
					:wikitext('<i class="ego-i i-arrow-left-short"></i>')
				:done()
				:tag('span'):addClass('dface-count'):wikitext(i .. '/' .. n):done()
				:tag('span')
					:addClass('dface-btn' .. tog(id, i, i % n + 1))
					:wikitext('<i class="ego-i i-arrow-right-short"></i>')
		end
	end
end

function p.heeho(frame)
	local args = getArgs(frame)
	local size = tonumber(args.size) or 150

	math.randomseed(os.clock() * 777777777)
	local runID = 'dface-' .. math.random(100000, 999999) .. '-'

	local root  = mw.html.create('div'):addClass('dface')
	local stack = root:tag('div'):addClass('dface-stack')
		:css({width = size .. 'px', height = size .. 'px'})
	local bar   = root:tag('div'):addClass('dface-bar')

	build(args, 'mouth', 'Mouth', runID, size, stack, bar)
	build(args, 'eyes',  'Eyes',  runID, size, stack, bar)

	return tostring(root)
end

return p