require('strict')

local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style')
local templatestyles = 'Module:Infobox military conflict/styles.css'
local cfg = mw.loadData('Module:Infobox military conflict/configuration')

local IMC = {}
IMC.__index = IMC

-- mw.message.newRawMessage (msg, args)

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	local colspans = {}
	
	-- This may result in colspans[1] getting set twice, but
	-- this is no big deal. The second set will be correct.
	local lastCombatant = 1
	
	for i = 1,self.combatants do
		if self.args[mw.message.newRawMessage(cfg.arg.arg_order, prefix, i, suffix)] then
			colspans[lastCombatant] = i - lastCombatant
			lastCombatant = i
		end
	end

	local jointText = self.args[mw.message.newRawMessage(cfg.arg.arg_order, prefix, self.combatants + 1, suffix)]
	
	if headerText and (colspans[1] or jointText) then
		builder:tag('tr')
			:tag('th')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.header_raw)
				:wikitext(headerText)
	end

	-- The only time colspans[1] wouldn't be set is if no
	-- combatant has a field with the given prefix and suffix.
	if colspans[1] then
		-- Since each found argument set the colspan for the previous
		-- one, the final one wasn't set above, so set it now.
		colspans[lastCombatant] = self.combatants - lastCombatant + 1
		builder = builder:tag('tr')
		for i = 1,self.combatants do
			-- At this point, colspans[i] will be set for i=1 unconditionally, and for
			-- any other value of i where self.args[prefix .. i .. suffix] is set.
			if colspans[i] then
				builder:tag('td')
					-- don't bother emitting colspan="1"
					:attr('colspan', colspans[i] ~= 1 and colspans[i] or nil)
					:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
					-- no border on the right of the rightmost column
					:css('border-right', i ~= lastCombatant and infoboxStyle.internal_border or nil)
					-- no padding on the left of the leftmost column
					:css('padding-left', i ~= 1 and '0.25em' or nil)
					-- don't show the border if we're directly under a header
					:css('border-top', not headerText and infoboxStyle.internal_border or nil)
					:newline()
					:wikitext(self.args[mw.message.newRawMessage(cfg.arg.arg_order, prefix, i, suffix)])
			end
		end
	end

	if jointText then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('text-align', 'center')
				-- don't show the border if we're directly under a header
				:css('border-top', (not headerText or colspans[1]) and infoboxStyle.internal_border or nil)
				:newline()
				:wikitext(jointText)
	end
end

function IMC:renderHeaderTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)
		:css('display', 'inline-table')

	if self.args[cfg.arg['date']] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(cfg.i18n['date'])
			:done()
			:tag('td')
				:wikitext(self.args[cfg.arg['date']])
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext(cfg.i18n.location)
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				-- hack so that people who don't know Lua know that this parameter is required
				:wikitext(self.args[cfg.arg.place] or cfg.i18n.place)
			:done()
	if self.args[cfg.arg.coordinates] then
		builder:wikitext(self.args[cfg.arg.coordinates])
	end
	builder = builder:done():done()

	-- only for "Putsch"
    if self.args[cfg.arg.action] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args[cfg.arg.action] and cfg.i18n.action)
			:done()
			:tag('td')
				:wikitext(self.args[cfg.arg.action])
	end

	if self.args[cfg.arg['status']] or self.args[cfg.arg.result] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args[cfg.arg['status']] and cfg.i18n['status'] or cfg.i18n.result)
			:done()
			:tag('td')
				:addClass('status')
				:newline()
				:wikitext(self.args[cfg.arg['status']] or self.args[cfg.arg.result])
	end

	if self.args[cfg.arg.territory] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(cfg.i18n.territory)
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args[cfg.arg.territory])
	end
end

function IMC:render()
	local builder = mw.html.create()
	if self.args[cfg.arg.campaignbox] then
		-- this should be the same as using {{stack|clear=right|...}}
		builder:wikitext(self.frame:expandTemplate{ title = 'stack begin', args = {
				[cfg.dep.stack_begin_clear] = cfg.dep_key.stack_begin_true
		}})
	end
	builder = builder:tag('table')
		:addClass('infobox vevent')
		:cssText(infoboxStyle.main_box_raw)
		:css('width', self.args[cfg.arg.width] or nil)

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants)
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args[cfg.arg.conflict] or mw.title.getCurrentTitle().text)
	if self.args[cfg.arg.partof] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext(string.format(cfg.i18n.part_of, self.args[cfg.arg.partof]))
	end
	if self.args[cfg.arg.image] then
		local caption = self.args[cfg.arg.caption]
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(
					require('Module:InfoboxImage').InfoboxImage{ args = {
						[cfg.dep.iimage_image] = self.args[cfg.arg.image],
						[cfg.dep.iimage_size] = self.args[cfg.arg.image_size],
						[cfg.dep.iimage_sizedefault] = cfg.dep_key.iimage_sizedefault_frameless,
						[cfg.dep.iimage_upright] = cfg.dep_key.iimage_upright_1,
						[cfg.dep.iimage_alt] = self.args[cfg.arg.alt]
					}} .. (caption and '<br />' .. caption or '') 
				)
	end
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.combatants_header] or cfg.i18n.belligerents,
		cfg.arg.combatant
	)
	-- can be un-hardcoded once gerrit:165108 is merged
	for _, v in ipairs(cfg.arg.letters) do
		self:renderPerCombatant(builder, nil, cfg.arg.combatant, v)
	end
	
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.commanders_header] or cfg.i18n.commanders,
		cfg.arg.commander
	)
	for _, v in ipairs(cfg.arg.letters) do
		self:renderPerCombatant(builder, nil, cfg.arg.commander, v)
	end
	
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.units_header] or cfg.i18n.units,
		cfg.arg.units
	)
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.strengths_header] or cfg.i18n.strength,
		cfg.arg.strength
	)
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.polstrengths_header] or cfg.i18n.political_support,
		cfg.arg.polstrength
	)
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.milstrengths_header] or cfg.i18n.military_support,
		cfg.arg.milstrength
	)
	self:renderPerCombatant(
		builder,
		self.args[cfg.arg.casualties_header] or cfg.i18n.casualties,
		cfg.arg.casualties
	)

	if self.args[cfg.arg.notes] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.section_border)
				:newline()
				:wikitext(self.args[cfg.arg.notes])
	end
	
	if self.args[cfg.arg.map_type] then
		local location_map_mod = require('Module:Location map')
		local location_map = {
			self.args[cfg.arg.map_type],
			relief = self.args[cfg.arg.map_relief],
			coordinates = self.args[cfg.arg.coordinates],
			width = self.args[cfg.arg.map_size] or 220,
			float = cfg.dep_key.map_float_center,
			border = cfg.dep_key.map_border_none,
			mark = self.args[cfg.arg.map_mark],
			marksize = self.args[cfg.arg.map_marksize] or 8,
			label = self.args[cfg.arg.map_label],
			alt = self.args[cfg.arg.map_alt],
			caption = self.args[cfg.arg.map_caption] or string.format(
				cfg.i18n.location_within,
				location_map_mod.data(self.frame, {self.args[cfg.arg.map_type], 'name'})
			)
		}
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.internal_border)
				:node(location_map_mod.main(self.frame, location_map))
	end
	builder = builder:done()
	if self.args[cfg.arg.campaignbox] then
		builder = builder:done()
		builder:wikitext(self.args[cfg.arg.campaignbox] .. self.frame:expandTemplate{ title = 'stack end'})
	end
	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Infobox military conflict'})
	end
	local obj = {
		frame = frame,
		args = args
	}

	-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
	-- also, don't try to make this more efficient, or references could be in the wrong order
	obj.combatants = 2
	for _,v in ipairs(cfg.arg.letters_and_empty) do
		for i = 1,5 do
			if args[mw.message.newRawMessage(cfg.arg.arg_order, cfg.arg.combatant, i, v)] then
				obj.combatants = math.max(obj.combatants, i)
			end
		end
	end

	return setmetatable(obj, IMC)
end

local p = {}

function p.main(frame)
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(IMC.new(frame):render())
end

return p