------------------------------------------------------------------------------
-- DevTools.lua
--
-- Author: Daniel Stephens www.cheeseplant.org
---------------------------------------------------------------------------

local masterFrame = nil;

local function WriteMessage(msg)
   if( DEFAULT_CHAT_FRAME ) then
      DEFAULT_CHAT_FRAME:AddMessage(msg);
   end
end

function DevTools_OnLoad()

   SLASH_DEVTOOLSRELOAD1 = "/reload";
   SlashCmdList["DEVTOOLSRELOAD"] =
      function(msg)
	 ReloadUI();
      end
   
   SLASH_DEVTOOLSDUMP1 = "/dump";
   SlashCmdList["DEVTOOLSDUMP"] = 
      function(msg)
	 WriteMessage("DevTools: Dump " .. msg);
	 RunScript("DevTools_Dump({" .. msg .. "})");
      end

   if( DEFAULT_CHAT_FRAME ) then
      -- WriteMessage("DevTools Loaded");
   end

-- UIErrorsFrame:AddMessage("DevTools Loaded", 
--			    1.0, 1.0, 1.0, 1.0,
--			    UIERRORS_HOLD_TIME);
   masterFrame = this;
end


-- Return the specified value
local function DevTools_DumpValue(val,indent)
   local valType = type(val);
   local retVal;
   
   if (valType == "nil")  then
      retVal = "nil";
   elseif (valType == "number") then
      retVal = val;
   elseif (valType == "boolean") then
      retVal = val;
   elseif (valType == "string") then
      retVal = '"' .. val .. '"';
   elseif (valType == "table") then
      local k, v, first;
      retVal = "{";
      first = 1;
      for k, v in pairs(val) do
	 if (first == 1) then
	    first = 0;
	 else
	    retVal = retVal .. ",\n" .. indent;
	 end
	 retVal = retVal .. "[" .. DevTools_DumpValue(k) .. "]=" 
	    .. DevTools_DumpValue(v, indent .. "  ");
      end
      retVal = retVal .. "}";
   else
      retVal = "<" .. valType .. ">";
   end
   return retVal;
end

-- Dump the specified list of value
function DevTools_Dump(valList)
   WriteMessage("Values:");
   local k, v;
   for k, v in pairs(valList) do
      WriteMessage("[" .. k .. "]" .. DevTools_DumpValue(v, "  "));
   end
   WriteMessage("--------------------------------------------");
end
