-- This module implements
-- , 
-- , , , 
-- , , ,
-- , , ,
-- , , ,
-- , , 
--
-- , , 
-- , 
--
local p = {}

local data_module = 'Module:Sports color/basketball'

local function stripwhitespace(text)
	return text:match("^%s*(.-)%s*$")
end

local function bordercss
	local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 #' .. c 
		.. ', inset -' .. w .. 'px -' .. w .. 'px 0 #' .. c
	return 'box-shadow: ' .. s .. ';'
end

local function get_colors
	team = stripwhitespace(team or '')
	unknown = unknown or {"DCDCDC", "000000", "000000", "FFFFFF"}
	
	local use_default = {
		[""] = 1,
		["retired"] = 1,
		["free agent"] = 1,
	
	local colors = nil
	
	if ( team and use_default[team:lower] ) then
		colors = {"DCDCDC", "000000", "DCDCDC", "000000"}
	else
		local all_colors = mw.loadData(data_module)
		colors = all_colors[team]
		if ( colors and type(colors) == 'string' ) then
			colors = all_colors[colors]
		end
	end

	return colors or unknown
end

local function team_check
	local colors = get_colors
	if type(colors) == 'table' then
		return 'known'
	else
		return unknown
	end
end

local function team_color
	local colors = get_colors

	num = tonumber(num:match('[1-4]') or '0')
	if ( num ) then
		return colors[num]
	else
		return ''
	end
end

local function team_colorcell
	local colors = get_colors
	local border = ''
	borderwidth = borderwidth or ''
	
	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[bd]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			if simple then
				border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
			else
				border = bordercss
			end
		end
	end
	
	return 'background-color: #' .. stripwhitespace(colors[bg]) .. '; color: #' .. stripwhitespace(colors[fg]) .. '; ' .. border
end

local function team_titlestripe
	local colors = get_colors
	local border = ''
	borderwidth = borderwidth or ''

	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[topc]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			border = border .. 'border-top: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
		end
		bordercolor = stripwhitespace(colors[botc]) or bordercolor
		if bordercolor ~= 'FFFFFF' then
			border = border .. 'border-bottom: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
		end
	end

	return 'background-color: #FFFFFF; ' .. border
end

local function team_cellborder
	local colors = get_colors
	local border = ''
	borderwidth = borderwidth or ''

	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[pri] or colors[sec] or colors[ter]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			if simple then
				border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
			else
				border = bordercss
			end
		end
	end

	return 'background-color: #FFFFFF; ' .. border
end

function p.check(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'basketball')
	return team_check
end

function p.color(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'basketball')
	return team_color
end

function p.colorcell(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'basketball')
	return team_colorcell
end

function p.colorcell2(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'basketball')
	return team_colorcell
end

function p.titlestripe(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'ice hockey')
	return team_titlestripe
end

function p.cellborder(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'ice hockey')
	return team_cellborder
end

function p.cellborder2(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent.args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent.args['sport'] or 'ice hockey')
	return team_cellborder
end