-- Style for olympic wrestling tables
local pp = {}

function pp.header(t,Args,p_sub,pos_label,group_col,VTE_text,full_table,results_header_txt)
	-- Load relevant modules
	local yesno = require('Module:Yesno')
	
	-- Create table header
	-- Pre stuff
	local team_width = Args['teamwidth'] or '250'
	local sort_text = yesno(Args['sortable_table'] or 'no') and ' sortable' or ''
	local show_played = not yesno(Args['hide_played'] or 'no')
	table.insert(t,'{| class="wikitable'..sort_text..'" style="text-align:center;"\n')            			-- Open table
	-- Table title
	if Args['title'] or Args['table_header'] then
		table.insert(t,'|+ ' .. (Args['title'] or Args['table_header']) .. '\n')
	end
	-- Custom header options
	local played_head_text = Args['pld_header'] or '<abbr title="Played">Pld</abbr>'
	local group_head_text = Args['group_header'] or '<abbr title="Group">Grp</abbr>'
	local round_head_text = Args['round_header'] or '<abbr title="Round">R</abbr>'
	local team_head_text = Args['team_header'] or 'Athlete'
	local show_win = yesno(Args['show_win'] or 'yes')
	local show_loss = yesno(Args['show_loss'] or 'yes')
	local show_tp = yesno(Args['show_tp'] or 'yes')
	local show_round = yesno(Args['show_round'] or 'no')
	
	-- Initialize
	local tt = {}
	tt.count = 0 		-- Up by one after every call
	tt.tab_text = t		-- Actual text
	-- Actual headers
	if pos_label ~= nil then
		tt = p_sub.colhead(tt,28,pos_label)									-- Position col
	end
	-- Add group header
	if full_table and group_col then
		tt = p_sub.colhead(tt,28,group_head_text)			-- Group col
	end
	tt = p_sub.colhead(tt,team_width,team_head_text..VTE_text)			-- Team col
	if show_played then
		tt = p_sub.colhead(tt,28,played_head_text)						-- Matches played col
	end
	if full_table then
		if show_win then
			tt = p_sub.colhead(tt,28,'<abbr title="Won">W</abbr>')		-- Win col
		end
		if show_loss then
			tt = p_sub.colhead(tt,28,'<abbr title="Lost">L</abbr>')		-- Loss col
		end
	end
	if show_round then
		tt = p_sub.colhead(tt,28,round_head_text)		-- Round col
	end
	tt = p_sub.colhead(tt,28,'<abbr title="Classification points">CP</abbr>')		-- Classification points col
	if show_tp then
		tt = p_sub.colhead(tt,28,'<abbr title="Technical points">TP</abbr>')			-- Technical points col
	end
	if full_table then
		tt.count = tt.count+1
		table.insert(tt.tab_text,results_header_txt)
	end

	return tt
end

-- Numeric alignment
local function num_align(num, digits)
	if tonumber(digits) and tonumber(num) then
		local n = tonumber(num)
		digits = tonumber(digits)
		if digits == 1 then
			if n >= 0 and n < 10 then
				return '<span style="visibility:hidden;color:transparent;">0</span>' .. num
			end
		elseif digits == 2 then
			if n >= 0 and n < 10 then
				return '<span style="visibility:hidden;color:transparent;">00</span>' .. num
			elseif n >= 10 and n < 100 then
				return '<span style="visibility:hidden;color:transparent;">0</span>' .. num
			end
		end
	end
	return num
end

function pp.row(frame,t,Args,p_sub,notes_exist,hth_id_list,full_table,rand_val,team_list,team_code_ii,ii_start,ii_end,ii_fw,bg_col,N_teams,ii,ii_show)
	-- Build the inner parts of individual rows
	
	-- Sub-module usage
	local mm = require('Module:Math')
	local yesno = require('Module:Yesno')

	-- Get custom/default options for in table
	local show_win = yesno(Args['show_win'] or 'yes')
	local show_loss = yesno(Args['show_loss'] or 'yes')
	local show_tp = yesno(Args['show_tp'] or 'yes')
	local show_round = yesno(Args['show_round'] or 'no')
	local digits = Args['digitpad']
	
	-- Get some input
	local wins = tonumber(Args['win_'..team_code_ii])			or 0
	local losses = tonumber(Args['loss_'..team_code_ii])		or 0
	local round = Args['r_'..team_code_ii] or 0
	local cp = tonumber(Args['cp_'..team_code_ii])			or 0
	local tp = tonumber(Args['tp_'..team_code_ii])		or 0
	local hth_local = yesno(Args['show_hth'] or 'yes') and Args['hth_'..team_code_ii] or nil
	local matches_text = wins + losses

	-- Some local vars	
	local hth_string
	local tt_return = p_sub.hth(frame,Args,full_table,hth_id_list,hth_local,notes_exist,team_list,team_code_ii,ii_start,ii_end,rand_val)
	hth_string = tt_return.str
	hth_id_list = tt_return.list
	notes_exist = tt_return.notes_exist
	
	-- What to rank the teams by
	local ranking_style = string.lower(Args['ranking_style'] or (show_round and 'r' or 'cp'))
	local win_fw, win_string, cp_fw, cp_string, round_fw, round_string = '', '', '', '', '', ''
	if (ranking_style == 'w' or ranking_style == 'win' or ranking_style == 'wins') then
		win_fw = 'font-weight:bold;'
		win_string = hth_string
	elseif (ranking_style == 'r' or ranking_style == 'rnd' or ranking_style == 'round') then
		round_fw = 'font-weight: bold;'
		round_string = hth_string
	elseif (ranking_style == 'cp') then
		cp_fw = 'font-weight: bold;'
		cp_string = hth_string
	else
		win_string = hth_string
	end
	
	local show_played = not yesno(Args['hide_played'] or 'no')
	if show_played then
		table.insert(t,'| style="'..ii_fw..bg_col..'" |'..matches_text..'\n') 		-- Played
	end
	if full_table then
		-- Use actual values if wins and losses are not numeric
		if (wins == 0 and losses == 0) then
			local function numorval(s)
				return s and ((s == '' and 0) or tonumber(s) or s) or 0
			end
			wins = numorval(Args['win_'..team_code_ii])
			losses = numorval(Args['loss_'..team_code_ii])
		end
		if show_win then
			table.insert(t,'| style="'..ii_fw..win_fw..bg_col..'" |'..num_align(wins,digits)..win_string..'\n') 	-- Won
		end
		if show_loss then
			table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(losses,digits)..'\n') -- Lost
		end
	end
	if show_round then
		table.insert(t,'| style="'..ii_fw..round_fw .. bg_col..'" |'..round..round_string..'\n') -- Lost
	end
	-- Add &minus; for negative point totals
	if cp<0 then
		table.insert(t,'| style="'..ii_fw..cp_fw..bg_col..'" | &minus;'..num_align(-cp,digits)..cp_string..'\n')
	else
		table.insert(t,'| style="'..ii_fw..cp_fw..bg_col..'" | '..num_align(cp,digits)..cp_string..'\n')
	end
	if show_tp then
		if tp<0 then
			table.insert(t,'| style="'..ii_fw..bg_col..'" | &minus;'..num_align(-tp,digits)..'\n')
		else
			table.insert(t,'| style="'..ii_fw..bg_col..'" | '..num_align(tp,digits)..'\n')
		end
	end
	return {t=t, notes_exist=notes_exist, hth_id_list=hth_id_list}
end

function pp.status(Args)
	-- Declare status options
	-- ------------------------------------------------------------
	-- NOTE: If you add to status_code, also add to status_called and status_letters!!
	-- Or functionality will be compromised
	-- ------------------------------------------------------------
	local status_code, status_called = {}, {}
	status_code = {	A='Advance to a further round', C='Champions', D='Disqualified', 
		E='Eliminated', G='Guests', H='Hosts', O='Play-off winners', P='Promoted', Q='Qualified for the phase indicated',
		R='Relegated', T='Qualified, but not yet for the particular phase indicated'}
	local status_letters = (Args['status_order'] or '') .. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	
	-- Status position (before or after read and default)
	local stat_pos_val = string.lower(Args['status_pos'] or '')
	local status_position = 'after' 	-- Default location
	if stat_pos_val=='before' then
		status_position = 'before'
	elseif stat_pos_val=='after' then
		status_position = 'after'
	end
	-- Read in custom status options
	for l in mw.text.gsplit(status_letters, '') do
		status_called[l] = false
		status_code[l] = status_code[l] or '?'
		status_letters = mw.ustring.gsub(status_letters, '(' .. l ..'.-)' .. l, '%1')

		if Args['status_text_' .. l] then
			status_code[l] = Args['status_text_' .. l]
		end
	 end
	
	return {code=status_code, called=status_called, letters=status_letters, position=status_position}
end

return pp