-------------------------------------------------------------------------
local RegisteredRings = {};
RingSavedValues = {};

function StatRings_RegisterRing(frame, name)
	sr_pr("Registering " .. name);
	if RegisteredRings[frame] == nil then
		RegisteredRings[frame] = {};
	end
	RegisteredRings[frame].friendlyName = name;
end

function StatRings_SaveValue(key, value)
	if RingSavedValues[this:GetName()] == nil then
		RingSavedValues[this:GetName()] = {};
	end
	RingSavedValues[this:GetName()][key] = value;
end

function StatRings_LoadValue(key)
	if RingSavedValues[this:GetName()] == nil then
		return nil;
	end
	return RingSavedValues[this:GetName()][key];
end

function StatRings_OnLoad()
    SlashCmdList["STATRINGS"] = StatRings_SlashHandler;
    SLASH_STATRINGS1 = "/ring";
end

function StatRings_SlashHandler(msg)
	local words = {};
	for word in string.gfind(msg, "%w+") do
		table.insert(words, word);
	end
	local l = table.getn(words);

	if l == 0 then
		sr_pr("Registered rings:");
		sr_pr("-----------------");
		for k, v in RegisteredRings do
			sr_pr(v.friendlyName);
		end
		sr_pr("-----------------");
	elseif l >= 1 and words[1] == "lock" then
		
		for ring in RegisteredRings do
			local doLock = getglobal(ring:GetName() .. "MoveAnchor"):IsVisible();
			if doLock then
				getglobal(ring:GetName() .. "MoveAnchor"):Hide();
				getglobal(ring:GetName() .. "SizeSlider"):Hide();
			else
				getglobal(ring:GetName() .. "SizeSlider"):Show();
				getglobal(ring:GetName() .. "MoveAnchor"):Show();
				getglobal(ring:GetName() .. "MoveAnchor"):SetAlpha(1);
				getglobal(ring:GetName() .. "MoveAnchor"):GetParent():SetAlpha(0.8);
			end
		end
	elseif l >= 1 and words[1] == "reset" then
		for ring in RegisteredRings do
			getglobal(ring:GetName()):ClearAllPoints();
			getglobal(ring:GetName()):SetPoint("CENTER", "StatRingsAnchor", "CENTER", 0, 0);
		end
	elseif l >= 1 and words[1] == "hide" then
		for ring in RegisteredRings do
			ring["frame"]:Hide();
		end
	elseif l >= 1 and words[1] == "show" then
		for ring in RegisteredRings do
			ring["frame"]:Show();
		end
	end
end

function sr_pr(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg);
end