local S = minetest.get_translator("dwarf_chreator")

local insert = table.insert
local concat = table.concat

local appearance_names =
{
	sex = S("Sex"),
	skin_color = S("Skin Color"),
	hair = S("Hair"),
	beard = S("Beard"),
	hair_brightness = S("Hair Brightness"),
	hair_redness = S("Hair Redness"),
	armor = S("Armor"),
	helmet = S("Helmet")
}

local appearance_fields =
{
	"sex",
	"skin_color",
	"hair",
	"beard",
	"hair_brightness",
	"hair_redness",
	"armor",
	"helmet"
}

local formspec_template =
{
	"size[12,8]" ..
	"real_coordinates[true]" ..
	"background[8,1;3,6;dwarf_chreator_background.png]",
	"button[3,0.3;2,0.7;randomize;" .. S("Randomize") .. "]"
}

do
	local offset = 0.3
	for i, v in ipairs(appearance_fields)
	do
		local name = appearance_names[v]
		offset = offset + 0.7
		
		local bg = "background[0.4," .. offset + 0.05 .. ";2.6,0.6;dwarf_chreator_text_background.png]"
		local label = "label[0.5," .. offset + 0.35 .. ";" .. name .."]"
		local button1 = "button[3," .. offset .. ";1,0.7;" .. v .. "<;<]"
		local button2 = "button[4," .. offset .. ";1,0.7;" .. v .. ">;>]"
		
		insert(formspec_template, bg)
		insert(formspec_template, label)
		insert(formspec_template, button1)
		insert(formspec_template, button2)
	end
end

--[[
	appearance:
	
	skin color
	hair brightness
	hair redness
	model
	sex
	beard
	hair
	armor
	helmet

]]


local image_spec_prefix = "image[8,1;3,6;"

local function get_skin_preview(appearance)
	return image_spec_prefix .. "dwarf_chreator_skin_preview.png" ..
		dwarf_characters.get_skin_color_mod(appearance.skin_color) .. "]"
end

local function get_simple_preview(appearance, field)
	local img = dwarf_characters["get_" .. field .. "_preview"](appearance[field])
	return img and image_spec_prefix .. img .. "]" or ""
end

local function get_haircolored_preview(appearance, field)
	local img = dwarf_characters["get_" .. field .. "_preview"](appearance[field])
	if not img
	then
		return ""
	end
	
	local tab =
	{
		image_spec_prefix,
		img,
		dwarf_characters.get_hair_color_mod(
			appearance.hair_brightness,
			appearance.hair_redness),
		"]"
	}
	return concat(tab)
end

local function assemble_formspec(appearance)
	local spec = {concat(formspec_template)}
	
	insert(spec, get_skin_preview(appearance))
	insert(spec, image_spec_prefix .. "dwarf_chreator_eyes_preview.png]")
	
	insert(spec, get_simple_preview(appearance, "armor"))
	
	insert(spec, get_haircolored_preview(appearance, "beard"))
	insert(spec, get_haircolored_preview(appearance, "hair"))
	
	insert(spec, get_simple_preview(appearance, "helmet"))
	
	insert(spec, image_spec_prefix ..
		"dwarf_chreator_" .. appearance.sex ..".png]")
	
	return concat(spec)
end

function dwarf_chreator.show_formspec(player)
	local name = player:get_player_name()
	minetest.show_formspec(name, "chreator", assemble_formspec(dwarf_characters.get_appearance(name)))
end

minetest.register_chatcommand("chreator",
{
	privs = {interact = true},
	func = function(name)
		local player = minetest.get_player_by_name(name)
		if player
		then
			dwarf_chreator.show_formspec(player)
			return true, S("Showing formspec")
		else
			return true, S("Can't show formspec: no player online with name @1", name)
		end
	end
})


minetest.register_on_player_receive_fields(
	function(player, formname, fields)
		if not minetest.check_player_privs(player, "interact") or
			formname ~= "chreator"
		then
			return
		end
		
		--check for < > type button presses
		local field
		local dir
		for i, v in ipairs(appearance_fields)
		do
			if fields[v .. ">"]
			then
				field = v
				dir = 1
			elseif fields[v .. "<"]
			then
				field = v
				dir = -1
			end
		end
		
		local playername = player:get_player_name()
		local appearance
		if not field --not a cycling button
		then
			if fields["randomize"] --handle randomize button
			then
				local playername = player:get_player_name()
				appearance = dwarf_characters.get_random_appearance()
				dwarf_characters.set_appearance(playername, appearance)

			else --no button: apply appearance to actual player model
				dwarf_characters.apply_appearance(player)
				return true
			end
		else
			--update appearance
			appearance = dwarf_characters.get_appearance(playername)
			--appearance[field] = --cycle through possibilities
			dwarf_characters.cycle_appearance(appearance, field, dir)
		end
		
		minetest.show_formspec(playername, "chreator",
			assemble_formspec(appearance))
		return true
	end)
