Quantcast
function AG_OnLoad(self)
    self:RegisterEvent("CHAT_MSG_GUILD_ACHIEVEMENT")
    self:RegisterEvent("CHAT_MSG_ACHIEVEMENT")
    self:RegisterEvent("CHAT_MSG_PARTY")
    self:RegisterEvent("CHAT_MSG_SYSTEM")
	self:RegisterEvent("CHAT_MSG_SYSTEM")
	self:RegisterEvent("GUILD_ROSTER_UPDATE")
    --slash commands
	SlashCmdList["AG"] = AG_Command;
    SLASH_AG1 = "/ag";
    SLASH_AG2 = "/autogratzer";
    if(AG_GratsMessage == nil)then
		AG_GratsMessage="Gratzzz";
		AG_Print("Grats message set to: Gratzzz");
		AG_LastMessage = 1;
	end
	if(AG_GuildJoinMessageToggle == nil)then
		AG_GuildJoinMessageToggle = False;
	end
	if(AG_Guild == nil)then
		AG_Guild = true;
	end
	if(AG_Say == nil)then
		AG_Say = true;
	end
	if(AG_Party == nil)then
		AG_Party = false;
	end
	if(AG_Delay == nil)then
		AG_Delay=0;
	end
	if(AG_GroupingTime == nil)then
		AG_GroupingTime=6000;
	end
	if(AG_GuildDisabledOverride == nil)then
		AG_GuildDisabledOverride = false;
	end
	if(AG_GuildWelcomeMessage == nil)then
		AG_GuildWelcomeMessage = "Welcome :D";
	end
	AG_SetupOptionsUI();
	AG_Print("AG Enabled");
end

function AG_SetupOptionsUI()
	AutoGratzer = {};
	AutoGratzer.ui = {};
	AutoGratzer.ui.panel = CreateFrame( "Frame", "AutoGratzerPanel", InterfaceOptionsFramePanelContainer );
	AutoGratzer.ui.panel.name = "AutoGratzer";

	--Guild Check Button
	AutoGratzer.ui.guildCheckButton = CreateFrame("CheckButton","AG_GuildCheckButton",AutoGratzer.ui.panel,"UICheckButtonTemplate") --frameType, frameName, frameParent, frameTemplate
	AutoGratzer.ui.guildCheckButton:SetPoint("TOPLEFT",20,-20)
	AutoGratzer.ui.guildCheckButton.text:SetText("Guild Gratzing")
	AutoGratzer.ui.guildCheckButton:SetScript("OnShow", function(self,event,arg1)
		self:SetChecked(AG_Guild);
	end)
	AutoGratzer.ui.guildCheckButton:SetScript("OnClick", function(self,event,arg1)
		AG_ToggleGuild();
	end)

	--Party Check Button
	AutoGratzer.ui.partyCheckButton = CreateFrame("CheckButton","AG_PartyCheckButton",AutoGratzer.ui.panel,"UICheckButtonTemplate") --frameType, frameName, frameParent, frameTemplate
	AutoGratzer.ui.partyCheckButton:SetPoint("TOPLEFT",20,-40)
	AutoGratzer.ui.partyCheckButton.text:SetText("Party Gratzing")
	AutoGratzer.ui.partyCheckButton:SetScript("OnShow", function(self,event,arg1)
		self:SetChecked(AG_Party);
	end)
	AutoGratzer.ui.partyCheckButton:SetScript("OnClick", function(self,event,arg1)
		AG_ToggleParty();
	end)

	--Say Check Button
	AutoGratzer.ui.sayCheckButton = CreateFrame("CheckButton","AG_SayCheckButton",AutoGratzer.ui.panel,"UICheckButtonTemplate") --frameType, frameName, frameParent, frameTemplate
	AutoGratzer.ui.sayCheckButton:SetPoint("TOPLEFT",20,-60)
	AutoGratzer.ui.sayCheckButton.text:SetText("Say Gratzing")
	AutoGratzer.ui.sayCheckButton:SetScript("OnShow", function(self,event,arg1)
		self:SetChecked(AG_Say);
	end)
	AutoGratzer.ui.sayCheckButton:SetScript("OnClick", function(self,event,arg1)
		AG_ToggleSay();
	end)

	--Guild Welcome Check Button
	AutoGratzer.ui.guildWelcomeCheckButton = CreateFrame("CheckButton","AG_GuildWelcomeCheckButton",AutoGratzer.ui.panel,"UICheckButtonTemplate") --frameType, frameName, frameParent, frameTemplate
	AutoGratzer.ui.guildWelcomeCheckButton:SetPoint("TOPLEFT",20,-100)
	AutoGratzer.ui.guildWelcomeCheckButton.text:SetText("New Guild Member Welcoming")
	AutoGratzer.ui.guildWelcomeCheckButton:SetScript("OnShow", function(self,event,arg1)
		self:SetChecked(AG_GuildJoinMessageToggle);
	end)
	AutoGratzer.ui.guildWelcomeCheckButton:SetScript("OnClick", function(self,event,arg1)
		AG_ToggleGuildWelcome();
	end)

	--Delay Slider
	AutoGratzer.ui.delaySlider = CreateBasicSlider(AutoGratzer.ui.panel, "AG_DelaySlider", "Delay in milliseconds before sending the message", 0, 60000, 100);
	AutoGratzer.ui.delaySlider:HookScript("OnValueChanged", function(self,value)
		AG_Delay = floor(value)
	end)
	AutoGratzer.ui.delaySlider:HookScript("OnShow", function(self,value)
		self:SetValue(AG_Delay);
		self.editbox:SetNumber(AG_Delay);
	end)
	AutoGratzer.ui.delaySlider.editbox:SetScript("OnShow", function(self,event,arg1)
		self:SetNumber(AG_Delay);
	end)
	AutoGratzer.ui.delaySlider:SetPoint("TOPRIGHT",-120,-20)

	--Grouping Slider
	AutoGratzer.ui.groupingSlider = CreateBasicSlider(AutoGratzer.ui.panel, "AG_GroupingSlider", "Delay in milliseconds after a message is sent that it won't send another", 0, 60000, 100);
	AutoGratzer.ui.groupingSlider:HookScript("OnValueChanged", function(self,value)
		AG_GroupingTime = floor(value)
	end)
	AutoGratzer.ui.groupingSlider:HookScript("OnShow", function(self,value)
		self:SetValue(AG_GroupingTime);
		self.editbox:SetNumber(AG_GroupingTime);
	end)
	AutoGratzer.ui.groupingSlider.editbox:SetScript("OnShow", function(self,event,arg1)
		self:SetNumber(AG_GroupingTime);
	end)
	AutoGratzer.ui.groupingSlider:SetPoint("TOPRIGHT",-120,-100)
	--AutoGratzer.ui.delayLabel = AutoGratzer.ui.panel:CreateFontString(nil,"OVERLAY","GameFontNormal", 200,200)
	--AutoGratzer.ui.delayLabel:SetText("delay")
	--AutoGratzer.ui.delayEditBox = CreateFrame("EditBox","AG_DelayEditBox",AutoGratzer.ui.panel,"InputBoxTemplate") --frameType, frameName, frameParent, frameTemplate
	--AutoGratzer.ui.delayEditBox:SetSize(50,30)
 --   AutoGratzer.ui.delayEditBox:ClearAllPoints()
	--AutoGratzer.ui.delayEditBox:SetPoint("TOPLEFT",120,-120)
 --   AutoGratzer.ui.delayEditBox:SetText("test");
 --   AutoGratzer.ui.delayEditBox:SetAutoFocus(false)
	--AutoGratzer.ui.delayEditBox.text:SetText("Delay(ms)")
	--AutoGratzer.ui.delayEditBox:SetScript("OnShow", function(self,event,arg1)
		--self:SetChecked(AG_Say);
	--end)

	--Grats Message
	AutoGratzer.ui.gratsMessageEditBox = CreateFrame("EditBox", "AG_GratsMessage", AutoGratzer.ui.panel, "InputBoxTemplate")
	AutoGratzer.ui.gratsMessageEditBox:SetSize(500,30)
	AutoGratzer.ui.gratsMessageEditBox:SetMultiLine(false)
    AutoGratzer.ui.gratsMessageEditBox:ClearAllPoints()
	AutoGratzer.ui.gratsMessageEditBox:SetPoint("TOPLEFT",20,-180)
	AutoGratzer.ui.gratsMessageEditBox:SetCursorPosition(0);
	AutoGratzer.ui.gratsMessageEditBox:ClearFocus();
    AutoGratzer.ui.gratsMessageEditBox:SetAutoFocus(false)
	AutoGratzer.ui.gratsMessageEditBox:SetScript("OnShow", function(self,event,arg1)
		self:SetText(AG_GratsMessage)
		self:SetCursorPosition(0);
		self:ClearFocus();
	end)
	AutoGratzer.ui.gratsMessageEditBox:SetScript("OnTextChanged", function(self,value)
		AG_GratsMessage = self:GetText()
	end)
	AutoGratzer.ui.gratsMessageLabel = CreateBasicFontString(AutoGratzer.ui.gratsMessageEditBox,"AG_GratsMessageLabel","OVERLAY","GameFontNormal","Grats Message");
	AutoGratzer.ui.gratsMessageLabel:SetPoint("BOTTOMLEFT", AutoGratzer.ui.gratsMessageEditBox, "TOPLEFT", 0, 0)

	--Welcome to Guild Message
	AutoGratzer.ui.guildWelcomeMessageEditBox = CreateFrame("EditBox", "AG_GuildWelcomeMessage", AutoGratzer.ui.panel, "InputBoxTemplate")
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetSize(500,30)
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetMultiLine(false)
    AutoGratzer.ui.guildWelcomeMessageEditBox:ClearAllPoints()
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetPoint("TOPLEFT",20,-240)
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetCursorPosition(0);
	AutoGratzer.ui.guildWelcomeMessageEditBox:ClearFocus();
    AutoGratzer.ui.guildWelcomeMessageEditBox:SetAutoFocus(false)
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetScript("OnShow", function(self,event,arg1)
		self:SetText(AG_GuildWelcomeMessage)
		self:SetCursorPosition(0);
		self:ClearFocus();
	end)
	AutoGratzer.ui.guildWelcomeMessageEditBox:SetScript("OnTextChanged", function(self,value)
		AG_GuildWelcomeMessage = self:GetText()
	end)
	AutoGratzer.ui.guildWelcomeMessageLabel = CreateBasicFontString(AutoGratzer.ui.guildWelcomeMessageEditBox,"AG_GuildWelcomeMessageLabel","OVERLAY","GameFontNormal","Guild Welcome Message");
	AutoGratzer.ui.guildWelcomeMessageLabel:SetPoint("BOTTOMLEFT", AutoGratzer.ui.guildWelcomeMessageEditBox, "TOPLEFT", 0, 0)


	InterfaceOptions_AddCategory(AutoGratzer.ui.panel);

end

 function AG_GetCmd(msg)
 	if msg then
 		local a=(msg); --contiguous string of non-space characters
 		if a then
 			return msg
 		else
 			return "";
 		end
 	end
 end

function AG_ShowHelp()
	print("AutoGratzer usage:");
	print("'/ag' or '/ag options' to show options ui");
	print("'/ag {msg}' or '/autogratzer {msg}'");
	print("'/ag delay {delay}' or '/autogratzer {delay}', with delay in milliseconds to set delay");
	print("'/ag guild' or '/autogratzer guild' to enable/disable guild gratzing");
	print("'/ag say' or '/autogratzer say' to enable/disable say gratzing");
	print("'/ag party' or '/autogratzer party' to enable/disable say gratzing");
end

function AG_ToggleGuild()
	if(AG_Guild) then
		AG_Guild = false;
		AG_Print("Guild gratzing now off");
	else
		AG_Guild = true;
		AG_Print("Guild gratzing now on");
	end;
	AutoGratzer.ui.guildCheckButton:SetChecked(AG_Guild);
end

function AG_ToggleSay()
	if(AG_Say) then
		AG_Say = false;
		AG_Print("Say gratzing now off");
	else
		AG_Say = true;
		AG_Print("Say gratzing now on");
	end;
	AutoGratzer.ui.sayCheckButton:SetChecked(AG_Say);
end

function AG_ToggleParty()
	if(AG_Party) then
		AG_Party = false;
		AG_Print("Party gratzing now off");
	else
		AG_Party = true;
		AG_Print("Party gratzing now on");
	end;
	AutoGratzer.ui.partyCheckButton:SetChecked(AG_Party);
end

function AG_ToggleGuildWelcome()
	if(AG_GuildJoinMessageToggle) then
		AG_GuildJoinMessageToggle = false;
		AG_Print("New guild member welcoming now off");
	else
		AG_GuildJoinMessageToggle = true;
		AG_Print("New guild member welcoming now on");
	end;
	AutoGratzer.ui.guildWelcomeCheckButton:SetChecked(AG_GuildJoinMessageToggle);
end

function AG_SetDelay(delay)
	if(delay ~= nill)then
		AG_Delay = tonumber(delay);
		AG_Print("Grats message delay set to: " ..delay.."ms");
	else
		AG_Print("Provide a number in milliseconds, eg '/ag delay 5000' for 5 seconds");
	end
end

function AG_Command(msg)
    local Cmd, SubCmd = AG_GetCmd(msg);
    if (Cmd == "")then
        --If the interface options aren't already loaded this doesn't work fully(just opens to non addons tab)
		--but apparently if you call it twice it works fine!
		InterfaceOptionsFrame_OpenToCategory(AutoGratzer.ui.panel);
		InterfaceOptionsFrame_OpenToCategory(AutoGratzer.ui.panel);
    elseif (Cmd == "help")then
        AG_ShowHelp();
    elseif (Cmd == "options")then
        InterfaceOptionsFrame_OpenToCategory(AutoGratzer.ui.panel);
		InterfaceOptionsFrame_OpenToCategory(AutoGratzer.ui.panel);
    elseif (Cmd == "guild")then
        AG_ToggleGuild();
    elseif (Cmd == "say")then
        AG_ToggleSay();
    elseif (Cmd == "party")then
        AG_ToggleParty();
	elseif (Cmd == "guildwelcome")then
        AG_ToggleGuildWelcome();
	elseif (string.find(Cmd,"delay") == 1)then
        AG_SetDelay(string.match(Cmd,"%d+"));
    else
        AG_GratsMessage = Cmd;
		AG_Print("Grats message set to: " .. Cmd);
    end
end


function AG_OnEvent(self,event,arg1,arg2)
	if(event == "GUILD_ROSTER_UPDATE")then AG_CheckOverride(); return end
	if(AG_GratsMessage == nil)then
		AG_GratsMessage="Gratzzz";
    end
    if(not AG_IsMe(arg2))then
	    if(event == "CHAT_MSG_GUILD_ACHIEVEMENT" and not AG_GuildDisabledOverride)then AG_DoGrats("GUILD");
	    elseif(event == "CHAT_MSG_ACHIEVEMENT")then AG_DoGrats("SAY");
	    elseif(event == "CHAT_MSG_ACHIEVEMENT")then AG_DoGrats("PARTY");
	    elseif(event == "CHAT_MSG_SYSTEM" and not AG_GuildDisabledOverride) then
	    	if(arg1 ~= nil) then
				if(string.find(arg1,"has joined the guild.")) then AG_GuildWelcome();
				end
			end
	    end
	end
end

function AG_DoGrats(source)
	if((source == "SAY" and AG_Say == true) or (source == "GUILD" and AG_Guild == true) or (source == "PARTY" and AG_Party == true)) then
		CurTime=GetTime();
		if (AG_LastMessage == nil) then
			AG_LastMessage = 1;
		end
		if((CurTime - AG_LastMessage) > (AG_GroupingTime/1000))then
			AG_LastMessage = GetTime();
			if(AG_Delay > 0)then
				C_Timer.After((AG_Delay/1000), function() SendChatMessage(AG_GratsMessage, source); end)
			else
				SendChatMessage(AG_GratsMessage, source);
			end
		end
	end
end

function AG_GuildWelcome()
	--Testing, enable if you know what your doing...
	if(AG_GuildJoinMessageToggle and (GetTime() - AG_LastMessage > (AG_GroupingTime/1000)))then
		AG_LastMessage = GetTime();
		if(AG_Delay > 0)then
			C_Timer.After((AG_Delay/1000), function() SendChatMessage("Welcome :)", "GUILD"); end)
		else
			SendChatMessage(AG_GuildWelcomeMessage, "GUILD");
		end
    end
end

function AG_IsMe(nameString)
	local name,server = split(nameString,"-")
	local myName, myServer = UnitName("player")
	if(myServer == nil)then
		myServer = GetRealmName();
	end
	if(server == nil and name == myName)then
		return true;
	elseif(server ~= nil and name == myName and server == myServer)then
		return true;
	else
		return false;
	end
end

function AG_CheckOverride()
	local myName = UnitName("player")
	for index=1, GetNumGuildMembers() do

		 local name,_,_,_,_,_,note = GetGuildRosterInfo(index)
		 if AG_IsMe(name) then
			 note = note:lower()
			 if note:match("noag") then
				AG_GuildDisabledOverride = true;
				return true;
			 else
				AG_GuildDisabledOverride = false;
				return false;
			 end
			 break
		 end
	end
	return false;
end

function AG_Print(msg)
	print("\124cffffFF00[AG]\124r",msg);
end

function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
	 table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return unpack(t)
end



  function CreateBasicFontString(parent, name, layer, template, text)
    local fs = parent:CreateFontString(name,layer,template)
    fs:SetText(text)
    return fs
  end

  function CreateBasicSlider(parent, name, title, minVal, maxVal, valStep)
    local slider = CreateFrame("Slider", name, parent, "OptionsSliderTemplate")
    local editbox = CreateFrame("EditBox", "$parentEditBox", slider, "InputBoxTemplate")
    slider:SetMinMaxValues(minVal, maxVal)
    slider:SetValue(minVal)
    slider:SetValueStep(1)
    slider.text = _G[name.."Text"]
    slider.text:SetText(title)
    slider.textLow = _G[name.."Low"]
    slider.textHigh = _G[name.."High"]
    slider.textLow:SetText(floor(minVal))
    slider.textHigh:SetText(floor(maxVal))
    slider.textLow:SetTextColor(0.4,0.4,0.4)
    slider.textHigh:SetTextColor(0.4,0.4,0.4)
    editbox:SetSize(50,30)
	editbox:SetNumeric(true)
	editbox:SetMultiLine(false)
	editbox:SetMaxLetters(5)
    editbox:ClearAllPoints()
    editbox:SetPoint("TOP", slider, "BOTTOM", 0, -5)
    editbox:SetNumber(slider:GetValue())
	editbox:SetCursorPosition(0);
	editbox:ClearFocus();
    editbox:SetAutoFocus(false)
    slider:SetScript("OnValueChanged", function(self,value)
		self.editbox:SetNumber(floor(value))
		if(not self.editbox:HasFocus())then
			self.editbox:SetCursorPosition(0);
			self.editbox:ClearFocus();
		end
    end)
    editbox:SetScript("OnTextChanged", function(self)
      local value = self:GetText()
      if tonumber(value) then
		if(floor(value) > maxVal)then
			self:SetNumber(maxVal)
		end
        if floor(self:GetParent():GetValue()) ~= floor(value) then
          self:GetParent():SetValue(floor(value))
        end
      end
    end)
    editbox:SetScript("OnEnterPressed", function(self)
      local value = self:GetText()
      if tonumber(value) then
        self:GetParent():SetValue(floor(value))
        self:ClearFocus()
      end
    end)
    slider.editbox = editbox
    return slider
  end