-- // debug2.lua I KNOW THIS WORKS FOR 0.674, should work for .698 also.
-- // replaces trigger words with whatever you specify

-- debug definitions
if not debugTable then
   debugTable = {}
end

-- debug chat commands
if not debugFunctions then
   debugFunctions = {}
end

-- pickle stuffs
debugFunctions.settingsFile = DC():GetAppPath() .. "/scripts/debug_table.txt"

function debugFunctions.tokenize( str )
   local ret = {}
   string.gsub( str, "(%S+)", function( s ) table.insert( ret, s ) end )
   return ret
end

if not debugFunctions.on then
   debugFunctions.on=false
end

function debugFunctions.addKey(key,value)
   debugFunctions.remKey(key)
   table.insert(debugTable,{k=key,v=value})
end

function debugFunctions.remKey(key)
   for i,map in ipairs(debugTable) do
      if map.k==key then
         table.remove(debugTable,i)
         return true
      end
   end
end

function debugFunctions.loadKeys()
   local o = io.open( debugFunctions.settingsFile,"r")
   if o then
      dofile( debugFunctions.settingsFile )
      o:close()
   end
end

function debugFunctions.saveKeys()
   pickle.store(debugFunctions.settingsFile, {debugTable = debugTable})
end

--////////////
dofile( DC():GetAppPath() .. "/scripts/libsimplepickle.lua" )
debugFunctions.loadKeys()
--////////////

function debugFunctions.listKeys(hub)
   if debugFunctions.on == true then
      hub:injectChat("*** DEBUG FUNCTIONS ARE ACTIVE")
   else
      hub:injectChat("*** DEBUG FUNCTIONS ARE INACTIVE")
   end
   for i,map in ipairs(debugTable) do
      hub:injectChat("*** Debug: "..map.k.." -> "..map.v)
   end
end

--
-- LISTENERS START HERE
--
dcpp:setListener("ownChatOut", "enteredDebug", function(hub,message,ret)
   if string.sub( message,1,1) ~= "/" then
      if debugFunctions.on then
         message = string.gsub(message,"([%a|;|%d]+)", function (word)
            for i,map in ipairs(debugTable) do
               if map.k == word then
                  return map.v
               end
            end
            return word
         end)
      hub:sendChat(message)
      return 1
      end
      return nil
   end
   local params = debugFunctions.tokenize( string.sub(message,2,-1) )
   if params[1] == "help" then
      hub:injectChat( "*** (debug.lua) /debug list, /debug add <key> <value>, /debug rem <key>, /debug [on|off] /debug save: key is replaced by value")
      return nil
   elseif params[1] == "debug" then
      if params[2] == "on" then
         debugFunctions.on=true
         hub:injectChat("*** Debug functions activated")
         return 1
      elseif params[2] == "list" then
         debugFunctions.listKeys(hub)
         return 1
      elseif params[2] =="off" then
         debugFunctions.on=false
         hub:injectChat("*** Debug functions deactivated")
         return 1
      elseif params[2] == "add" then
         local mapto = string.gsub(message,"^/debug add %S+%s+","")
         debugFunctions.addKey(params[3],mapto)
         hub:injectChat("*** Debug: "..params[3].." -> "..mapto)
         return 1
      elseif params[2] == "rem" then
         if debugFunctions.remKey(params[3]) then
            hub:injectChat("*** Debug: Removed "..params[3])
            return 1
         end
      elseif params[2] == "save" then
         debugFunctions.saveKeys()
         hub:injectChat("*** DebugTable saved")
         return 1
      end
   end
end)

DC():PrintDebug("  ** Loaded debug.lua **")
