require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local make_style_string = require('Module:Optional style').make_style_string
local CSS_unit = require('Module:CSS unit')._CSS_unit

function p.border_start(frame)
	local args = getArgs(frame)
	
	-- style args
	local maxwidth = args['max-width'] or args.maxwidth or args[2] or 'none'
	local display = args.display or 'block'
	local bstyle = args.bstyle or args[3] or 'solid'
	local bthickness = args.bthickness or args[4] or '1px'
	local color = args.color or args[5] or '#000000'
	local bgcolor = args.bgcolor or args[6] or '#FFFFFF'
	local align = args.align or args[7] or 'left'
	local position = args.position or args[8]
	local padding = args.padding or args[9] or '5px'
	local style = args.style or ''
	
	local positionStyle
	if position == 'left' then
		positionStyle = ''
	elseif position == 'right' then
		positionStyle = 'position:relative;margin-left:auto;'
	else
		positionStyle = 'margin-left:auto;margin-right:auto;'
	end
	
	local styleArgs = {
		['max-width'] = maxwidth,
		['display'] = display,
		['box-sizing'] = 'border-box',
		['border'] = bstyle .. " " .. bthickness .. " " .. color,
		['background-color'] = bgcolor,
		['text-align'] = align,
		['padding'] = padding,
		['style'] = positionStyle .. " " .. style
	}
	local styleParam = make_style_string(styleArgs)
	
	local divOpen = '<div class="wst-border" ' .. styleParam .. '>'
	
	local trackingCat
	if CSS_unit({maxwidth}) == 'px' then
		trackingCat = '[[Category:Pages using pixel widths (border)]]'
	else
		trackingCat = ''
	end
	
	return divOpen .. trackingCat
end

local function border_end()
	return '</div>'
end

function p.border(frame)
	return p.border_start(frame) .. getArgs(frame)[1] .. border_end()
end

return p