--
-- Error Redirect
--
-- by Bastian Pflieger <wb@illogical.de>
--
-- Credits: Idea by Bunny, compiling default filter lists
--          Thomas Watts, option to redirect to general or combat log
--
-- Last update: April 2, 2006
--
-- supports "myAddOns": http://www.curse-gaming.com/mod.php?addid=358
--

function ErrorRedirect_OnLoad()
  ErrorRedirect_IsEnabled = true;
  ErrorRedirect_Frame = "ChatFrame2";
  ErrorRedirect_HighlightMessages = true;
  ErrorRedirect_SuppressModErrors = false;

  ErrorRedirect_Org_AddMessage = UIErrorsFrame.AddMessage;
  UIErrorsFrame.AddMessage = ErrorRedirect_AddMessage;

  ErrorRedirect_Org_Message = ScriptErrors.Show;
  ScriptErrors.Show = ErrorRedirect_Message;

  SLASH_ERROR_REDIRECT1 = "/error_redirect";
  SLASH_ERROR_REDIRECT2 = "/config_redirect";
  SlashCmdList["ERROR_REDIRECT"] = function(msg)
    ShowUIPanel(ErrorRedirectOptionsFrame);
  end

  this:RegisterEvent("VARIABLES_LOADED");
end


function ErrorRedirect_Message(objData)
  if ErrorRedirect_SuppressModErrors and string.find(ScriptErrors_Message:GetText(), ":[0-9]+: ") then
    if ErrorRedirect_Frame ~= "" then
      getglobal(ErrorRedirect_Frame):AddMessage(ScriptErrors_Message:GetText(), 1.0, 0.0, 0.0);
    else
      ChatFrame2:AddMessage(ScriptErrors_Message:GetText(), 1.0, 0.0, 0.0);
    end
  else
    ErrorRedirect_Org_Message(objData);
  end
end


function ErrorRedirect_GetIndividualColors(msg, r, g, b)
  if ErrorRedirect_HighlightMessages and ErrorRedirect_Filter_FixedColorMessages[msg] then
    r = ErrorRedirect_Colors[ErrorRedirect_Filter_FixedColorMessages[msg]].r;
    g = ErrorRedirect_Colors[ErrorRedirect_Filter_FixedColorMessages[msg]].g;
    b = ErrorRedirect_Colors[ErrorRedirect_Filter_FixedColorMessages[msg]].b;
  end
  return r, g, b;
end

function ErrorRedirect_AddColoredMessage(msg, r, g, b, frame)
  r, g, b = ErrorRedirect_GetIndividualColors(msg, r, g, b);
  frame:AddMessage(msg, r, g, b, 1.0);
end

function ErrorRedirect_MatchesFixedString(msg)
  return ErrorRedirect_Filter_FixedErrorMessages[msg];
end

function ErrorRedirect_MatchesPartialString(msg)
  local bMatch = false;
  local filter = ErrorRedirect_Filter_PartialErrorMessages;
  local i, filterLength = 1, table.getn(filter)

  while not bMatch and i <= filterLength do
    bMatch = filter[i] ~= "" and string.find(msg, filter[i]);
    i = i + 1;
  end

  return bMatch;
end

function ErrorRedirect_AddMessage(objData, msg, r, g, b, a, holdTime)
  if ErrorRedirect_IsEnabled and ( ErrorRedirect_MatchesFixedString(msg) or ErrorRedirect_MatchesPartialString(msg) ) then
    if getglobal(ErrorRedirect_Frame) then
      ErrorRedirect_AddColoredMessage(msg, r, g, b, getglobal(ErrorRedirect_Frame));
    end
  else
    r, g, b = ErrorRedirect_GetIndividualColors(msg, r, g, b);
    ErrorRedirect_Org_AddMessage(objData, msg, r, g, b, a, holdTime);
  end
end

function ErrorRedirect_OnEvent(event)
  if event == "VARIABLES_LOADED" then
    ErrorRedirect_RegisterMyAddons();
  end
end

function ErrorRedirect_RegisterMyAddons()
  if myAddOnsFrame_Register then
    local ERdetails = {
      name = ERRORREDIRECT_NAME,
      description = ERRORREDIRECT_MYADDONS_DESCRIPTION,
      version = ERRORREDIRECT_VERSION,
      releaseDate = ERRORREDIRECT_MYADDONS_RELEASEDATE,
      author = "wbb",
      email = "<wb@illogical.de>",
      website = "http://www.illogical.de",
      category = MYADDONS_CATEGORY_OTHERS,
      frame = "ErrorRedirectFrame",
      optionsframe = "ErrorRedirectOptionsFrame"
    };
    myAddOnsFrame_Register( ERdetails, { ERRORREDIRECT_CHATHELP1 } );
  end
end

