local master = dwarf_characters
local intramodcom = dwarf_characters.intramodcom
local S = minetest.get_translator("dwarf_characters")

--persistent storage
local known_player_appearances =
	intramodcom.register_for_storage("known_player_appearances",
	{})



--non-persistent storage
local player_textures = {}




--put this into chreator
function dwarf_characters.cycle_appearance(appearance, field, direction)
	if field == "skin_color" or
		field == "hair_brightness" or
		field == "hair_redness"
	then
		local val = ((appearance[field] * 10 - 1 + direction) % 10 + 1) / 10
		appearance[field] = val
	elseif field == "sex"
	then
		local current_val = appearance.sex
		local model_list = dwarf_characters.get_models()
		for i, v in ipairs(model_list)
		do
			if v.sex == current_val
			then
				local index = (i - 1 + direction) % #model_list + 1
				local model = model_list[index]
				appearance.model = model.name
				appearance.sex = model.sex
				appearance.beard = dwarf_characters.get_random_beard(appearance)
			end
		end
	else
		local list = dwarf_characters["get_" .. field .. "_list"](appearance)
		local current_val = appearance[field]
		for i, v in ipairs(list)
		do
			if current_val == v
			then
				local index = (i - 1 + direction) % #list + 1
				appearance[field] = list[index]
				break
			end
		end
	end
end



local function get_random_appearance()
	local appearance = {}
	appearance.skin_color = math.random(1, 10) / 10
	appearance.hair_brightness = math.random(1, 10) / 10
	appearance.hair_redness = math.random(1, 10) / 10

	local model_list = dwarf_characters.get_models()
	local model = model_list[math.random(#model_list)]
	appearance.model = model.name
	appearance.sex = model.sex


	appearance.beard = dwarf_characters.get_random_beard(appearance)


	appearance.hair = dwarf_characters.get_random_hair()


	appearance.armor = dwarf_characters.get_random_armor()


	appearance.helmet = dwarf_characters.get_random_helmet()
	return appearance
end

dwarf_characters.get_random_appearance = get_random_appearance

local function get_appearance(name)
	local appearance = known_player_appearances[name]
	if not appearance
	then
		appearance = get_random_appearance()
		known_player_appearances[name] = appearance
	end
	return appearance
end

--publish to api
dwarf_characters.get_appearance = get_appearance

function dwarf_characters.set_appearance(playername, appearance)
	known_player_appearances[playername] = appearance
end

local animations = intramodcom.animations


--TODO: put stuff for applying appearance to player into its own function

function dwarf_characters.apply_appearance(player)
	local name = player:get_player_name()
	local appearance = get_appearance(name)
	local props = player:get_properties()
	props.textures = dwarf_characters.get_textures(appearance)
	props.mesh = appearance.model
	player:set_properties(props)
end

minetest.register_chatcommand("newappearance",
{
	privs = {interact = true, server = true},
	func = function(name, param)
		local player = minetest.get_player_by_name(param == "" and name or param)
		if player
		then
			local appearance = get_random_appearance()
			known_player_appearances[name] = appearance
			dwarf_characters.apply_appearance(player)

			return true, S("Your appearance changed!")
		else
			return true, S("Can't change appearance of player " .. param)
		end
	end
})





-- Update appearance when the player joins
minetest.register_on_joinplayer(function(player)
	local name = player:get_player_name()


	do --object property stuff
		local properties = player:get_properties()

		local appearance = get_appearance(name)
		properties.mesh = appearance.model
		properties.textures = dwarf_characters.get_textures(appearance)

		properties.visual = "mesh"
		properties.eye_height = 1.3
		properties.visual_size = {x = 1, y = 1, z = 1}
		player:set_properties(properties)
	end
	player:set_local_animation(
		animations.stand(),
		animations.walk(),
		animations.mine(),
		animations.walk_mine(),
		45)
	player:set_animation(animations.stand(), 45, 0, true)
end)
minetest.register_on_dieplayer(function(player)
	player:set_animation(animations.die(), 45, 0, false)
end)
minetest.register_on_respawnplayer(function(player)

	player:set_animation(animations.stand(), 45, 0, true)
end)
