--BamMod V2.0
-- New Features:
-- * Ace2 Rebuild
-- * Code Optimisation
-- * SCT Support
-- * Killing Blow
-- * Killing Blow Sound
-- * BamSupress for other BamMod users
-- under development
-- * RaidFeatures
-- * Raid Syncro
-- Updated for 3.0 by Syrielle|Castor|Áine|Andastra @ Teldrassil-EU

--Ace initialization

BamMod = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "AceHook-2.1");
local L = AceLibrary("AceLocale-2.2"):new("BamMod");
local cbh = LibStub("CallbackHandler-1.0");
local GUIDLib = LibStub("LibGUIDRegistry-0.1");
local BamModMutilateCount = 1;
local BamModMutilateDMG = 0;
local comboPoints = 0;
local last_cp = 0;
local max_sound_played = false;
-- local ORIG_ChatFrame_OnEvent = ChatFrame_OnEvent;
--Defaults
local defaultcf = {
	["version"]			= "2.2.6",
	["active"]			=	true,
	["crit_table"]		=	{},
	["crit_table_pet"]	=	{},
	["wTable"]			=		{
								DMG		=	0,
								MOB		=	"",
								LVL		=	0
							},
	["wTablePet"]			=		{
								DMG		=	0,
								MOB		=	"",
								LVL		=	0
							},
	["ever"]			=	true,
	["white"]			=	false,
	["pet"]				=	false,
	["pet_w"]			=	false,
	["pet_n"]			=	false,
	["max"]				=	false,
	["cp_max_only"]		=	false,
	["channel"]			=	0,
	["sound"]			= 	true,
	["chat_type"]		=   "system",
	["heal"]			=	true,
	["no_text"]			= 	false,
	["killing_blow"]	= 	false,
	["killing_sound"]	= 	false,
	["killing_pvp"]		= 	false,
	["killing_group"]	= 	false,
	["sup_spam"]		= 	false,
	["mutilate"]		=	false,
	["spell_alert"]		=	true,
};

function BamMod:OnInitialize()
	self:RegisterDB("BamModDB");
	self:RegisterDefaults('profile', defaultcf);
	--Option Menu
	local options = {
	type = 'group',
	args = {
		sa = {
			name = "Spell Alert Toggle",
			type = 'toggle',
	    	desc = "Toggles SpellAlert",
	    	get = function() return BamMod.db.profile["spell_alert"]; end,
    		set = function(v) BamMod.db.profile["spell_alert"] = v; end,
	    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
		--Switches Mutilate Display on / off
		mut = {
			name = "Mutilate Toggle",
			type = 'toggle',
	    	desc = L["toggle_mutilate"],
	    	get = function() return BamMod.db.profile["mutilate"]; end,
    		set = function(v) BamMod.db.profile["mutilate"] = v; end,
	    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
		--Switches BamMod on / off
		a = {
			name = "ActiveToggle",
			type = 'toggle',
	    	desc = L["toggle_on_off"],
	    	get = function() return BamMod.db.profile["active"]; end,
    		set = function(v) BamMod.db.profile["active"] = v; end,
	    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
		--Switches BamSuppress on / off
		sup = {
			name = "BamSuppress",
			type = 'toggle',
	    	desc = L["toggle_suppress"],
	    	get = function() return BamMod.db.profile["sup_spam"]; end,
    		set = function(v) BamMod.db.profile["sup_spam"] = v; end,
	    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
    	--Toggle Killing Blow Display in SCT
		kb = {
			type = 'group',
			name = "KillingBlow Group",
			desc = L["toggle_killing_blow"],
			args = {
				   	active = {
								type = 'toggle',
								name = "KillingBlow Toggle",
						    	desc = L["toggle_killing_blow"],
						    	get = function() return BamMod.db.profile["killing_blow"]; end,
					    		set = function(v) BamMod.db.profile["killing_blow"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						     },
					sound  = {
								type = 'toggle',
								name = "KillingBlow Sound Toggle",
						    	desc = L["toggle_killing_sound"],
						    	get = function() return BamMod.db.profile["killing_sound"]; end,
					    		set = function(v) BamMod.db.profile["killing_sound"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
					pvp  = {
								type = 'toggle',
								name = "KillingBlow only for PvP",
						    	desc = L["toggle_killing_pvp"],
						    	get = function() return BamMod.db.profile["killing_pvp"]; end,
					    		set = function(v) BamMod.db.profile["killing_pvp"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
					grouponly  = {
								type = 'toggle',
								name = "KillingBlow only in groups",
						    	desc = L["toggle_killing_group"],
						    	get = function() return BamMod.db.profile["killing_group"]; end,
					    		set = function(v) BamMod.db.profile["killing_group"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
				   },
    	},
    	--Toggle pet display
		pet = {
			type = 'group',
			name = "Pet's crits",
			desc = L["toggle_killing_blow"],
			args = {
				   	active = {
								type = 'toggle',
								name = "Toggle pet's crits",
						    	desc = L["toggle_pet"],
						    	get = function() return BamMod.db.profile["pet"]; end,
					    		set = function(v) BamMod.db.profile["pet"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						     },
					white  = {
								type = 'toggle',
								name = "Pet's white crits",
						    	desc = L["toggle_pet_w"],
						    	get = function() return BamMod.db.profile["pet_w"]; end,
					    		set = function(v) BamMod.db.profile["pet_w"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
					new  = {
								type = 'toggle',
								name = "Only the new crits of your pet",
						    	desc = L["toggle_pet_n"],
						    	get = function() return BamMod.db.profile["pet_n"]; end,
					    		set = function(v) BamMod.db.profile["pet_n"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
					sound  = {
								type = 'toggle',
								name = "Sounds for your pet",
						    	desc = L["toggle_pet_s"],
						    	get = function() return BamMod.db.profile["pet_s"]; end,
					    		set = function(v) BamMod.db.profile["pet_s"] = v; end,
						    	map = { [false] = L["msg_off"], [true] = L["msg_on"] },
						    },
				   },
    	},
		--Sound in BamMod on / off
		m = {
			name = "SoundToggle",
			type = 'toggle',
    		desc = L["toggle_mute"],
    		get = function() return BamMod.db.profile["sound"]; end,
    		set = function(v) BamMod.db.profile["sound"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
		--Crit only on new High Crit on / off
		e = {
			type = 'toggle',
			name = "Critsound every crit",
    		desc = L["toggle_ever"],
    		get = function() return BamMod.db.profile["ever"]; end,
    		set = function(v) BamMod.db.profile["ever"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
    	--White DMG Crit on / off
		s = {
			type = 'toggle',
			name = "Display white Crits",
    		desc = L["toggle_special"],
    		get = function() return BamMod.db.profile["white"]; end,
    		set = function(v) BamMod.db.profile["white"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
    	--CP Dependancy on / off
		cp = {
			type = 'toggle',
    		desc = L["toggle_CP"],
    		name = "ComboPointToggle",
    		get = function() return BamMod.db.profile["max"]; end,
    		set = function(v) BamMod.db.profile["max"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
    	--CP Dependancy on / off
		cpmax = {
			type = 'toggle',
    		desc = L["toggle_CPMax"],
    		name = "ComboPointMaxOnly",
    		get = function() return BamMod.db.profile["cp_max_only"]; end,
    		set = function(v) BamMod.db.profile["cp_max_only"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
    	--Toggles HealBam on / off
		h = {
			type = 'toggle',
    		desc = L["toggle_heal"],
    		name = L["toggle_heal"],
    		get = function() return BamMod.db.profile["heal"]; end,
    		set = function(v) BamMod.db.profile["heal"] = v; end,
    		map = { [false] = L["msg_off"], [true] = L["msg_on"] },
    	},
		--Switches output Channel using /chan Keyword
        chan = {
            type = 'text',
            desc = L["msg_channel"],
            name = "ChannelSelection",
            usage= L["msg_channel"],
            set  = function(v) BamMod:BamModSetChannel(v); end,
            get  = function() return BamMod:BamModGetChannelMenu(); end,
        },
        --Display BamMod Status
        status = {
        	type = 'execute',
        	name = "Status",
        	desc = L["msg_status"],
        	func = function() BamMod:BamModShowStatus(); end,
        },
        --Reset everything to default
        reset = {
        	type = 'execute',
        	name = "Reset",
        	desc = L["msg_reset"],
        	func = function() BamMod:ResetDB("profile"); end,
        },
        --reset Crittables
        cr = {
        	type = 'execute',
        	name = "CritReset",
        	desc = L["msg_crit_reset"],
        	func = function()
        			BamMod.db.profile["w_Table"] = {
        									       		DMG		=	0,
														MOB		=	"",
														LVL		=	0
											   	   };
        			BamMod.db.profile["crit_table"] = {};
    		end,
  			},
		},
	};
	--End OptionTable!
	self:RegisterChatCommand({"/bam", "/bm"}, options);
	--set to profile by char by default
	if not self.db.char.notFirstTime then
		self.db.char.notFirstTime = true;
		self:SetProfile("char");
	end
end

-- Register for the Combat Log events
local BamModFrame = CreateFrame("Frame", "BamModFrame")
BamModFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
BamModFrame:RegisterEvent("CHAT_MSG_CHANNEL")
BamModFrame:SetScript("OnEvent", function() BamMod:BamMod_OnEvent(event) end)

function BamMod:BamMod_OnEvent(event)
	if (event == "COMBAT_LOG_EVENT_UNFILTERED") then
    	BamMod:CombatLogEvent(event)
	end
	if (event == "CHAT_MSG_CHANNEL") then
		BamMod:ChatEvent(event)
	end
end

--Event-Handler Functions

function BamMod:CombatLogEvent(event)
	if not (BamMod.db.profile["active"]) then return end;
	local eventType = arg2;
	local sourceName = arg4;
	local sourceFlags = arg5;
	local destGUID = arg6;
	local destName = arg7;
	if (UnitPowerType("player")==3 and BamMod.db.profile["cp_max_only"]) then
        if (GetComboPoints("player") == 5) and not max_sound_played then
            BamMod:Sound(6);
            max_sound_played = true;
        end
        if (GetComboPoints("player") ~= 5) and max_sound_played then
        	max_sound_played = false;
        end
	end
	if (BamMod.db.profile["max"]) and not (BamMod.db.profile["cp_max_only"]) then
		if (UnitPowerType("player")==3) then
			if (GetComboPoints("player") > 0) and (GetComboPoints("player") > last_cp) then
	            BamMod:Sound(GetComboPoints("player"));
	        end
	        last_cp = GetComboPoints("player");
		else
			if sourceName == UnitName("player") then
            	BamMod:Sound(math.random(5));
            end
		end
	end
	if (BamMod.db.profile["spell_alert"]) then
		if (eventType == "SPELL_PERIODIC_CAST_START") or (eventType == "SPELL_CAST_START") then
			local spellName = arg10;
			BamMod:SpellAlert(sourceName, spellName);
		end
		if (eventType == "SPELL_PERIODIC_INTERRUPT") or (eventType == "SPELL_INTERRUPT") then
			local spellName = arg10;
			local extraSpellName = arg13;
			BamMod:InterruptAlert(sourceName, destName, spellName, extraSpellName);
		end
		if (eventType == "SPELL_AURA_APPLIED") then
			local spellName = arg10;
			local type = arg12;
			BamMod:BuffAlert(destName, type, spellName);
		end
	end
	if sourceName == UnitName("player") then
		if (eventType == "SWING_DAMAGE") then
			local amount = arg9;
			local critical = arg15;
			if BamMod.db.profile["white"] then
				BamMod:CritCheck("normal", amount, destGUID, critical, "player");
			end
		end
		if (eventType == "RANGE_DAMAGE") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg18;
			if BamMod.db.profile["white"] == true then
				BamMod:CritCheck(spellName, amount, destGUID, critical, "player");
			end
		end
		if (eventType == "SPELL_DAMAGE" or eventType == "SPELL_PERIODIC_DAMAGE") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg18;
			BamMod:CritCheck(spellName, amount, destGUID, critical, "player");
		end
		if (eventType == "SPELL_HEAL" or eventType == "SPELL_PERIODIC_HEAL") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg15;
			if BamMod.db.profile["heal"] then
				BamMod:CritCheck(spellName, amount, destGUID, critical, "player");
			end
		end
		--if (eventType == "UNIT_DIED") or (eventType == "UNIT_DESTROYED") then
		--	BamMod:BamModKillingBlow(destName);
		--end
	elseif (sourceFlags == 0x00001111) or (sourceFlags == 0x00002111) and BamMod.db.profile["pet"] then
		if (eventType == "SWING_DAMAGE") then
			local amount = arg9;
			local critical = arg15;
			if BamMod.db.profile["pet_w"] then
				BamMod:CritCheck("normal", amount, destGUID, critical, "pet");
			end
		end
		if (eventType == "RANGE_DAMAGE") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg18;
			if BamMod.db.profile["white"] == true then
				BamMod:CritCheck(spellName, amount, destGUID, critical, "pet");
			end
		end
		if (eventType == "SPELL_DAMAGE" or eventType == "SPELL_PERIODIC_DAMAGE") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg18;
			BamMod:CritCheck(spellName, amount, destGUID, critical, "pet");
		end
		if (eventType == "SPELL_HEAL") then
			local spellName = arg10;
			local amount = arg12;
			local critical = arg15;
			if BamMod.db.profile["heal"] then
				BamMod:CritCheck(spellName, amount, destGUID, critical, "pet");
			end
		end
	end
end

function BamMod:InterruptAlert(source, dest, spell, extraSpell)
	if ((not BamMod.db.profile["spell_alert"])or(not SCT)) then return end;

	local rgbcolor = {r=0, g=1, b=0};
	SCT:DisplayMessage("*** "..dest.."'s "..extraSpell.." was interrupted by "..source.."'s "..spell.."! ***", rgbcolor);
end

function BamMod:BuffAlert(dest, bufftype, spell)
	if ((not BamMod.db.profile["spell_alert"])or(not SCT)) then return end;

	local rgbcolor = {r=0, g=0, b=1};
	if (bufftype == "BUFF") then
		SCT:DisplayMessage("*** "..dest.." gains "..spell.." ***", rgbcolor);
	elseif (bufftype == "DEBUFF") then
		SCT:DisplayMessage("*** "..dest.." suffers from "..spell.." ***", rgbcolor);
	end
end

function BamMod:SpellAlert(source, spell)
	if ((not BamMod.db.profile["spell_alert"])or(not SCT)) then return end;

	if (not (source == UnitName("player"))) and (not (source == UnitName("pet"))) then
		local rgbcolor = {r=1, g=0, b=0};
		SCT:DisplayMessage("*** "..source.." begins to cast "..spell.." ***", rgbcolor);
	end
end

--Core Functions

function BamMod:Sound(i)
	local cp = GetComboPoints("player");
	if (cp==0) then
		cp = 1;
	end
	if (UnitPowerType("player")~=3) then
		cp = math.random(5);
	end

	if (i ~= nil) then
		PlaySoundFile(BamModC.SOUND[i]);
		return
    else
    	PlaySoundFile(BamModC.SOUND[1]);
    	return
    end
end

function BamMod:Alert(weap,dmg, who)
	local chatid = BamMod:BamModGetChannel();
		
	if (BamMod.db.profile["sound"]) then
		BamMod:Sound();
	end
	if (BamMod.db.profile["no_text"]) then return end
	if (who == "player") then
		if (string.lower(BamMod.db.profile["chat_type"])=="system") then
			BamMod:ShowMessage("B\195\164M!! [ "..weap.." - "..dmg.." ]");
			return;
		elseif (string.lower(BamMod.db.profile["chat_type"])=="sct") then
		 	BamMod:ShowInSCT("B\195\164M!! [ "..weap.." - "..dmg.." ]");
		 	return;
		elseif (chatid>0) then
			SendChatMessage("B\195\164M!! [ "..weap.." - "..dmg.." ]","CHANNEL",nil, chatid);
			return;
		elseif (BamMod:BamModIsSpecialChan()) then
			SendChatMessage("B\195\164M!! [ "..weap.." - "..dmg.." ]", BamMod.db.profile["chat_type"]);
			return;
		end
	elseif (who == "pet") then
		if (chat_type=="system") then
			BamMod:ShowMessage("<PET> B\195\164M!! [ "..weap.." - "..dmg.." ]");
			return;
		elseif (chat_type=="sct") then
		 	BamMod:ShowInSCT("<PET> B\195\164M!! [ "..weap.." - "..dmg.." ]");
		 	return;
		elseif (chatid>0) then
			SendChatMessage("B\195\164M!! [ "..weap.." - "..dmg.." ]","CHANNEL",nil, channel);
			return;
		elseif (BamMod:BamModIsSpecialChan()) then
			SendChatMessage("B\195\164M!! [ "..weap.." - "..dmg.." ]", BamMod.db.profile["chat_type"]);
			return;
		end
	end
end

function BamMod:ShowInSCT(msg)
	if (not SCT) then
		BamMod:ShowMessage(msg);
		return
	end;
	local rgbcolor = {r=0, g=0, b=1};
	SCT:DisplayMessage(msg, rgbcolor);
end

function BamMod:ShowMessage(msg)
	if( DEFAULT_CHAT_FRAME ) then
		DEFAULT_CHAT_FRAME:AddMessage(BamModC.VERSION..msg);
	end
end

function BamMod:BamModShowStatus()
	BamMod:ShowMessage(L["msg_show_data"]..UnitName("player"));
	local wDMGDisplay = L["msg_attack"]..
				 		BamModC.RED..BamMod.db.profile["wTable"].DMG.."|r"..
				 		BamModC.YELLOW.." ("..self.db.profile["wTable"].MOB..
				 		" ("..self.db.profile["wTable"].LVL..") ) |r";
	BamMod:ShowMessage(wDMGDisplay);
	BamMod:ShowMessage(L["msg_spells"]);
	local color = BamModC.YELLOW;
	for spell, spell_info in pairs(BamMod.db.profile["crit_table"]) do
		local mob_lvl = "--";
		if (spell_info.LVL ~= nil) then
			mob_lvl = spell_info.LVL;
		end
		local spell_dmg = spell_info.DMG.." @ "..color.."[ "..spell_info.MOB.." ("..mob_lvl..")]|r";
		BamMod:ShowMessage(BamModC.RED..spell.."|r - "..spell_dmg);
	end
	if not (BamMod.db.profile["pet"]) then return end;
	BamMod:ShowMessage(L["msg_show_data"]..UnitName("Pet"));
	local wDMGDisplay = L["msg_attack_pet"]..
				 		BamModC.RED..BamMod.db.profile["wTablePet"].DMG.."|r"..
				 		BamModC.YELLOW.." ("..self.db.profile["wTablePet"].MOB..
				 		" ("..self.db.profile["wTablePet"].LVL..") ) |r";
	BamMod:ShowMessage(wDMGDisplay);
	BamMod:ShowMessage(L["msg_spells_pet"]);
	local color = BamModC.YELLOW;
	for spell, spell_info in pairs(BamMod.db.profile["crit_table_pet"]) do
		local mob_lvl = "--";
		if (spell_info.LVL ~= nil) then
			mob_lvl = spell_info.LVL;
		end
		local spell_dmg = spell_info.DMG.." @ "..color.."[ "..spell_info.MOB.." ("..mob_lvl..")]|r";
		BamMod:ShowMessage(BamModC.RED..spell.."|r - "..spell_dmg);
	end
end

function BamMod:BamModGetChannelMenu()
	if (BamMod.db.profile["no_text"]) then
		return L["msg_channel_off"];
	elseif (BamMod.db.profile["channel"]>0) then
		return L["msg_channel"]..BamMod.db.profile["channel"];
	else
		return L["msg_channel"]..BamMod.db.profile["chat_type"];
	end
end

function BamMod:BamModGetChannel()
	local channel = BamMod.db.profile["channel"];
	local chat_type = BamMod.db.profile["chat_type"];
	local chatid = 0;
	if (channel>0) then
		chatid = GetChannelName(channel);
	elseif ((chat_type ~= "system") and (chat_type ~= "none")) then
		chatid = GetChannelName(chat_type);
	end;
	return chatid;
end

function BamMod:BamModIsSpecialChan()
	local cn = string.lower(BamMod.db.profile["chat_type"]);
	if ((cn == "say") or (cn == "emote") or (cn == "yell") or (cn == "party") or (cn == "guild") or (cn == "raid") or (cn == "raid_warning") or (cn == "battleground") or (cn == "whisper")) then
    	return true;
	else
		return false;
	end
end

function BamMod:BamModSetChannel(c)
	c = string.lower(c);
	if (c=="off") then
		BamMod.db.profile["no_text"] = true;
		return
	else
		BamMod.db.profile["no_text"] = false;
	end
	if (tonumber(c)==nil) then
		BamMod.db.profile["chat_type"] = c;
		BamMod.db.profile["channel"] = 0;
	else
		BamMod.db.profile["channel"] = tonumber(c);
		BamMod.db.profile["chat_type"] = "none";
	end
end

function BamMod:MutilateSum(spell, dmg, guid)
	if (spell~=L["spell_mutilate"]) then return end
	nDMG = dmg;
	if (BamModMutilateCount==2) then
		BamModMutilateCount=1;
		BamMod:AddToCritTable(spell.."Sum", BamModMutilateDMG+nDMG, guid);
	else
		BamModMutilateCount=2;
		BamModMutilateDMG = nDMG;
	end
end

function BamMod:AddToCritTable(spell, dmg, guid, who)
	local lvl =  GUIDLib:GetLevel(guid);
	local ndmg = dmg;
	if (who == "player") then
		if(spell == "normal") then
			local crit = BamMod.db.profile["wTable"].DMG;
			if (crit == nil)or(dmg > crit) then
			   BamMod.db.profile["wTable"].DMG = ndmg;
			   BamMod.db.profile["wTable"].MOB = GUIDLib:GetName(guid);
			   BamMod.db.profile["wTable"].LVL = lvl;
			   BamMod:Alert("Normal",dmg,who);
			   return;
			elseif (BamMod.db.profile["ever"]) then
				BamMod:Alert("Normal",dmg, who);
				return;
			end
		else
			if (BamMod.db.profile["crit_table"][spell] == nil) then
				BamMod.db.profile["crit_table"][spell] = {
												  			DMG = ndmg,
												  			MOB = GUIDLib:GetName(guid),
												  			LVL = lvl
												 	     };
				BamMod:Alert(spell, dmg, who);
				return;
			else
				local crit = BamMod.db.profile["crit_table"][spell].DMG;
				if (ndmg>crit) then
					BamMod.db.profile["crit_table"][spell].DMG = ndmg;
					BamMod.db.profile["crit_table"][spell].MOB = GUIDLib:GetName(guid);
					BamMod.db.profile["crit_table"][spell].LVL = lvl;
					BamMod:Alert(spell, dmg, who);
					return;
				end
			end
		end
		if (spell==L["spell_mutilate"].."Sum") then
			BamMod:ShowInSCT("B\195\164M!! [ "..spell.." - "..dmg.." ]");
			return
		end
		if (BamMod.db.profile["ever"]) then
			BamMod:Alert(spell, dmg, who);
			return
		end
	elseif (who == "pet") then
		if(spell == "normal") then
			local crit = BamMod.db.profile["wTablePet"].DMG;
			if (crit == nil)or(dmg > crit) then
			   BamMod.db.profile["wTablePet"].DMG = ndmg;
			   BamMod.db.profile["wTablePet"].MOB = GUIDLib:GetName(guid);
			   BamMod.db.profile["wTablePet"].LVL = lvl;
			   BamMod:Alert("Normal",dmg, who);
			   return;
			elseif not (BamMod.db.profile["pet_n"]) then
				BamMod:Alert("Normal",dmg,who);
				return;
			end
		else
			if (BamMod.db.profile["crit_table_pet"][spell] == nil) then
				BamMod.db.profile["crit_table_pet"][spell] = {
												  			DMG = ndmg,
												  			MOB = GUIDLib:GetName(guid),
												  			LVL = lvl
												 	     };
				BamMod:Alert(spell, dmg, who);
				return;
			else
				local crit = BamMod.db.profile["crit_table_pet"][spell].DMG;
				if (ndmg>crit) then
					BamMod.db.profile["crit_table_pet"][spell].DMG = ndmg;
					BamMod.db.profile["crit_table_pet"][spell].MOB = GUIDLib:GetName(guid);
					BamMod.db.profile["crit_table_pet"][spell].LVL = lvl;
					BamMod:Alert(spell, dmg, who);
					return;
				end
			end
		end
		if not (BamMod.db.profile["pet_n"]) then
			BamMod:Alert(spell, dmg, who);
			return
		end
	end
end

function BamMod:CritCheck(spell, dmg, guid, crit, who)
	if (BamMod.db.profile["mutilate"]) and (who == "player") then
		BamMod:MutilateSum(spell, dmg, guid);
	end
	if crit == 1 then
		BamMod:AddToCritTable(spell, dmg, guid, who);
	end
end

function BamMod:GetConfig(var)
	return BamMod.db.profile[var];
end

function BamMod:SetConfig(var,value)
	BamMod.db.profile[var]=value;
end

function BamMod:ChatEvent(event)

end

-- The ignore function


local function chatFilter(ChatFrameSelf, event, ...)
	local msg = arg1
	if BamMod.db.profile["sup_spam"] then
        if (strfind(msg, "<PET> B\195\164M!!") or strfind(msg, "<PET> BäM!!") or strfind(msg, "B\195\164M!!") or strfind(msg, "BäM!!")) and (arg2 ~= UnitName("player")) then
            return true
        end
    end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", chatFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", chatFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", chatFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", chatFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_GUILD", chatFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_OFFICER", chatFilter)

--[[ Hook Functions

function ChatFrame_OnEvent(event)
  if (BamMod:GetConfig("sup_spam")) and
  	 (BamModC.EVENTS[event]) and
  	 (strfind(arg1,"B\195\164M!!")) and
  	 (arg2~=UnitName("player")) then
  else
    ORIG_ChatFrame_OnEvent(event);
  end
end ]]--


--[[function BamModKillingBlow(name)
	if (not SCT) then return; end;
	local active = (BamMod.db.profile["killing_blow"]);
	local active_2 = true;
	if (BamMod.db.profile["killing_group"]) and (GetNumPartyMembers() > 0) and (GetNumRaidMembers > 0) then
		active_2 = true;
	else
		active_2 = false;
	end
	if (BamMod.db.profile["killing_pvp"]) and (UnitIsPlayer(name)) then
		active_2 = active_2 and true;
	else
		active_2 = false;
	end
	if (not active) and (not active_2) then return end;
	local rgbcolor = {r=0, g=0, b=1};
	SCT:DisplayMessage(string.format(L["killing_blow"], name), rgbcolor);
	if (BamMod.db.profile["killing_sound"]) then
		BamMod:Sound(6);
	end
end ]]--