local debug = false

defense_mob_api.get_debug = function()
	return debug
end

function defense_mob_api:toggle_debug(on)
	debug = on
	if debug then
		regeneration.rate = 100
		minetest.set_timeofday(0.3)
		return true, "Debug mode activated"
	else
		regeneration.rate = self.regeneration_rate
		return true, "Debug mode deactivated"
	end
end

minetest.register_chatcommand("debug", {
	description = "Toggle Defense mod debug mode",
	privs = {server=true},
	func = function(name)
		return defense:toggle_debug(not defense.debug)
	end,
})

--Optimization tools

local times = {}

function defense_mob_api:track_time(name, t)
	times[name] = (times[name] or {0, 0})
	times[name][1] = times[name][1] + t
	times[name][2] = times[name][2] + 1
end
minetest.register_chatcommand("get_mob_times",
{
	privs = {server = true},
	description = "Display times spent in functions of defense_mob_api",
	func = function()
		local ret = "Times spent on:\n"
		for k, t in pairs(times)
		do
			ret = ret .. k .. ": " .. t[1] .. ", called " .. t[2] ..
				" times, avg: " .. t[1] / t[2] .. "\n"
		end
		return true, ret
	end
})


-- Pathfinder debugger
local pf_player = nil
local pf_class_name = nil
local pf_update_interval = 1.0

minetest.register_chatcommand("debug_pf", {
	description = "Debug the pathfinder",
	params = "<class>",
	privs = {server=true},
	func = function(name, class)
		if class and class ~= "" then
			if defense.pathfinder.classes[class] then
				pf_class_name = class
				pf_player = minetest.get_player_by_name(name)
				return true, "Pathfinder debugger for " .. pf_class_name .. " activated"
			else
				return false, "No pathfinder class of that name"
			end
		else
			pf_class_name = nil
			pf_player = nil
			return true, "Pathfinder debugger deactivated"
		end
	end,
})

minetest.register_node("defense_mob_api:debug_pf", {
	drawtype = "allfaces",
	tiles = {"defense_mob_api_debug_path.png"},
	light_source = 14,
	groups = {dig_immediate = 3},
	drop = "",
	walkable = false,
})

minetest.register_abm({
	nodenames = {"defense_mob_api:debug_pf"},
	interval = 2.0,
	chance = 1,
	action = function(pos)
		minetest.remove_node(pos)
	end,
})

local function pf_update()
	if pf_class_name then
		local pathfinder = defense_mob_api.pathfinder
		local pos = pf_player:get_pos()
		local sector = pathfinder.find_containing_sector(pathfinder.classes[pf_class_name], math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5))
		if sector then
			local distance_str = sector.distance
			if sector.distance == nil then
				distance_str = "nil"
			end

			local bounds_str = "(" .. sector.min_x .. "," .. sector.min_y .. "," .. sector.min_z .. ";" .. sector.max_x .. "," .. sector.max_y .. "," .. sector.max_z .. ")"

			local links_str = ""
			for i,l in pairs(sector.links) do
				links_str = links_str .. " " .. i
			end
			links_str = "[" .. links_str .. " ]"

			defense_mob_api:log("You are in sector " .. sector.id .. " {d=" .. distance_str .. " b=" .. bounds_str .. " l=" .. links_str .. "}")

			for z = sector.min_z,sector.max_z do
				for y = sector.min_y,sector.max_y do
					for x = sector.min_x,sector.max_x do
						if (x == sector.min_x or x == sector.max_x)
						   and (y == sector.min_y or y == sector.max_y)
						   and (z == sector.min_z or z == sector.max_z) then
							local pos = {x=x,y=y,z=z}
							local node = minetest.get_node_or_nil(pos)
							if node and node.name == "air" then
								minetest.set_node(pos, {name="defense_mob_api:debug_pf"})
							end
						end
					end
				end
			end
		else
			defense:log("You are not in a sector")
		end
	end
end

local pf_last_update_time = 0
minetest.register_globalstep(function(dtime)
	local gt = minetest.get_gametime()
	if pf_last_update_time + pf_update_interval < gt then
 		pf_update()
		pf_last_update_time = gt
	end
end)
