KillingBlowAnnouncer = LibStub("AceAddon-3.0"):NewAddon("KillingBlowAnnouncer", "AceConsole-3.0", "AceEvent-3.0")
LibStub("AceConfigRegistry-3.0"):NotifyChange("KillingBlowAnnouncer")

local Media = LibStub("LibSharedMedia-3.0")
------------------------------------------------------------------ADD SOUND FILES HERE--------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------
--------HOW TO ADD SOUND FILES----------------------------------------------------------------------------------
---1. Make sure that the sound file is either a mp3 file or ogg file. This is important because WoW doesn't support any other soundfiles.
---2. Add the file to the Sounds map. Found at Interface\AddOns\Killing Blow Announcer\Sounds
---3. Add the following line to this lua file with your own soundname and the path to the sound file: 
---   Media:Register("sound", "HOW YOU WANT THE SOUND FILE TO BE NAMED", [[Interface\AddOns\Killing Blow Announcer\Sounds\FILENAME.mp3]]);
---4. Save changes to the Killing Blow Announcer file and  Restart WoW in case it was already open.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
---Make sure to backup any changes to this file and of the files added to the sounds map when updating this addon. Any changes will be overwritten
---when updating so just a heads up.
----------------------------------------------------------------------------------------------------------------

--QUAKE FEMALE SOUNDS
Media:Register("sound", "Quake female: bottomfeeder", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\bottomfeeder.mp3]]);
Media:Register("sound", "Quake female: dominating", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\dominating.mp3]]);
Media:Register("sound", "Quake female: firstblood", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\firstblood.mp3]]);
Media:Register("sound", "Quake female: godlike", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\godlike.mp3]]);
Media:Register("sound", "Quake female: headshot", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\headshot.mp3]]);
Media:Register("sound", "Quake female: humiliation", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\humiliation.mp3]]);
Media:Register("sound", "Quake female: killingspree", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\killingspree.mp3]]);
Media:Register("sound", "Quake female: monsterkill", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\monsterkill.mp3]]);
Media:Register("sound", "Quake female: multikill", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\multikill.mp3]]);
Media:Register("sound", "Quake female: play", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\prepare.mp3]]);
Media:Register("sound", "Quake female: rampage", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\rampage.mp3]]);
Media:Register("sound", "Quake female: ultrakill", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\ultrakill.mp3]]);
Media:Register("sound", "Quake female: unstoppable", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\unstoppable.mp3]]);
Media:Register("sound", "Quake female: wickedsick", [[Interface\AddOns\Killing Blow Announcer\Sounds\female\wickedsick.mp3]]);

--QUAKE MALE SOUNDS
Media:Register("sound", "Quake male: wickedsick", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\wickedsick.mp3]]);
Media:Register("sound", "Quake male: unstoppable", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\unstoppable.mp3]])
Media:Register("sound", "Quake male: ultrakill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\ultrakill.mp3]]);
Media:Register("sound", "Quake male: triplekill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\triplekill.mp3]]);
Media:Register("sound", "Quake male: teamkiller", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\teamkiller.mp3]]);
Media:Register("sound", "Quake male: rampage", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\rampage.mp3]]);
Media:Register("sound", "Quake male: prepare", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\prepare.mp3]]);
Media:Register("sound", "Quake male: perfect", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\perfect.mp3]]);
Media:Register("sound", "Quake male: multikill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\multikill.mp3]]);
Media:Register("sound", "Quake male: monsterkill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\monsterkill.mp3]]);
Media:Register("sound", "Quake male: megakill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\megakill.mp3]]);
Media:Register("sound", "Quake male: ludicrouskill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\ludicrouskill.mp3]]);
Media:Register("sound", "Quake male: killingspree", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\killingspree.mp3]]);
Media:Register("sound", "Quake male: impressive", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\impressive.mp3]]);
Media:Register("sound", "Quake male: humiliation", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\humiliation.mp3]]);
Media:Register("sound", "Quake male: holyshit", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\holyshit.mp3]]);
Media:Register("sound", "Quake male: headshot", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\headshot.mp3]]);
Media:Register("sound", "Quake male: godlike", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\godlike.mp3]]);
Media:Register("sound", "Quake male: firstblood", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\firstblood.mp3]]);
Media:Register("sound", "Quake male: doublekill", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\doublekill.mp3]]);
Media:Register("sound", "Quake male: dominating", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\dominating.mp3]]);
Media:Register("sound", "Quake male: combowhore", [[Interface\AddOns\Killing Blow Announcer\Sounds\male\combowhore.mp3]]);



-----------------------------------------------------------------------END OF SOUND FILE LIST-------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------


--ingame options menu
local options = {
	name = "KillingBlowAnnouncer",
	handler = KillingBlowAnnouncer,
	type = 'group',
	childGroups = "tab",
	args = {
				enablePVP		=	{
									type		=	"toggle",
									name		=	"Enable pvp only",
									desc		=	"Enables the addon to only work for player kills. If left unchecked it will announce and track every kill regardless if it's a player or not.",	
									order 		= 	 1,
									width 		= 	"full",
									get			=	"EnablePVP",
									set			=	"ToggleEnablePVP",
									},
				Options= {
					name		=	"Chat options",
					desc 		= 	"Options related to the chat message",
					type		=	"group",					
					order		=	1,
					args		=	{		
								enableMSG		=	{
									type		=	"toggle",
									name		=	"Enable chat message",
									desc		=	"Enables the Killing blow chat message ",	
									order 		= 	 1,
									width 		= 	"full",
									get			=	"EnableMSG",
									set			=	"ToggleEnableMSG",
									},
								
							    msg = {
									type = "input",
									name = "Chat message",
									desc = "The chat message displayed when you get a KB",
									order = 2,
									width = "double",
									usage = "<Your message here>",
									get = "GetMessage",
									set = "SetMessage",
								},
								editChannel = {
									name 		= "Channel",
									desc 		= "Adjust to which channel the message gets printed.",
									order 		= 3, width = "double",
									type 		= "select",
									values 		= {["say"] = "say",["yell"] = "yell",["raid"] = "raid",["party"] = "party",["guild"] = "guild"},
									get		    = function(info) return KillingBlowAnnouncer:GetChatChannel(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetChatChannel(info,key) end,
									},
								},
						},
			Options2= {
					name		=	"Tracker options",
					desc 		= 	"Options related to the tracker frame",
					type		=	"group",	
					order		=	2,
					args		=	{		
								enableTracker		=	{
									type		=	"toggle",
									name		=	"Enable tracker",
									order		=  	1,
									desc		=	"Enables the Killing blow tracker frame.",						
									get			=	"EnableKBT",
									set			=	"ToggleEnableKBT",
									},
								enableLock		=	{
									type		=	"toggle",
									name		=	"Enable drag",
									order		=  	2,
									desc		=	"Enables the user to drag the frame around.",						
									get			=	"EnableKBTDrag",
									set			=	"ToggleEnableKBTDrag",
								},
								editBackground = {
									name 		= "Background file",
									desc 		= "Adjust background file of the tracker.",
									order 		= 6, width = "double",
									type 		= "select",
									values 		= Media:HashTable("background"),
									dialogControl = "LSM30_Background",
									get		    = function(info) return KillingBlowAnnouncer:GetTrackerBackground(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetTrackerBackground(info,key) end,
								},
								editBorder = {
									name 		= "Border file",
									desc 		= "Adjust border file of the tracker.",
									order 		= 6, width = "double",
									type 		= "select",
									values 		= Media:HashTable("border"),
									dialogControl = "LSM30_Border",
									get		    = function(info) return KillingBlowAnnouncer:GetTrackerBorder(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetTrackerBorder(info,key) end,
								},
								editFont = {
									name 		= "Font",
									desc 		= "Adjust the font settings.",
									order 		= 6, width = "double",
									type 		= "select",
									values 		= Media:HashTable("font"),
									dialogControl = "LSM30_Font",
									get		    = function(info) return KillingBlowAnnouncer:GetTrackerFont(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetTrackerFont(info,key) end,
								},
							},
						},
			Options3= {
					name		=	"Killingblow options",
					desc 		= 	"Options related to the killing blow message",
					type		=	"group",
					order		=	3,
					args		=	{		
								editMsg = {
									type 		= "input",
									name 		= "Killing blow message",
									order 		= 3,
									width		= "double",
									desc 		= "The message that gets displayed when you make a killing blow",
									usage 		= "<Your message here>",
									get 		= "GetKillingMessage",
									set		    = "SetKillingMessage",
											},										
								editFont = {
									name 		= "Font",
									desc 		= "Adjust the font settings.",
									order 		= 6, width = "double",
									type 		= "select",
									values 		= Media:HashTable("font"),
									dialogControl = "LSM30_Font",
									get		    = function(info) return KillingBlowAnnouncer:GetFont(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetFont(info,key) end,
											},
								editSound = {
									name 		= "Sound",
									desc 		= "Adjust the sound that gets played.",
									order 		= 6, width = "double",
									type 		= "select",
									values 		= Media:HashTable("sound"),
									dialogControl = "LSM30_Sound",
									get		    = function(info) return KillingBlowAnnouncer:GetSound(info) end,
									set 		= function(info,key) KillingBlowAnnouncer:SetSound(info,key) end,
											},
								editFontOutline = {
									name 		= "Font Outline",
									desc 		= "Adjust the font outline.",
									order 		= 5, width = "double",
									type 	    = "select",
									get         = function(info) return KillingBlowAnnouncer:GetOutline(info) end,
									set         = function(info,key) KillingBlowAnnouncer:SetOutline(info,key)end,
									values      = {
													NONE = "None",
													OUTLINE = "Thin",
													THICKOUTLINE = "Thick",
												},
											},
								editColor		=	{
									name		=	"Color",
									desc		=	"Adjust the color of the message.",	
									order	    = 4,
									width       = "double",
									type        = "color", hasAlpha = false,
									get         = function(info) return KillingBlowAnnouncer:GetRed(info),KillingBlowAnnouncer:GetGreen(info),KillingBlowAnnouncer:GetBlue(info) end,
									set         = function(info, r, g, b) KillingBlowAnnouncer:SetRed(info,r) KillingBlowAnnouncer:SetGreen(info,g) KillingBlowAnnouncer:SetBlue(info,b) setColors() end,
											},
	
								editFontSize		=	{
									type		=	"range",
									name		=	"FontSize" ,
									min 		= 	0,
									max    		=   30,
									width    	=   "double",
									desc		=	"Adjust the font size. Default is 30.",	
									order		=	7,		
									get			=	"GetFontSize",
									set			=	"SetFontSize",												
											
												},
								editVisibleTime		=	{
									type		=	"range",
									name		=	"VisibleTime" ,
									min 		= 	0,
									max    		=   60,
									width    	=   "double",
									desc		=	"Adjust the time that the message is visible in seconds. Default is 3.",	
									order		=	8,		
									get			=	"GetVisibleTime",
									set			=	"SetVisibleTime",												
											
												},
								editFadeTime		=	{
									type		=	"range",
									name		=	"FadeTime" ,
									min 		= 	0,
									max    		=   20,
									width    	=   "double",
									desc		=	"Adjust the time that it takes for the message to fade from start to end in seconds. Default is 1.",	
									order		=	9,		
									get			=	"GetFadeTime",
									set			=	"SetFadeTime",												
											
												},
										
								enablingEdit		=	{
									type		=	"toggle",
									name		=	"Snap message frame",
									desc		=	"Snaps the killing blow message to the window for editing purposes. Disable this option when done editing",						
									get			=	"EnableSnap",
									width		= 	"full",
									order		=	2,
									set			=	"ToggleEnableSnap",
								},
								enableKillingBlow		=	{
									type		=	"toggle",
									name		=	"Enable killing message",
									order		=  	1,
									desc		=	"Enables the Killing blow message frame.",	
									get			=	"EnableKB",
									set			=	"ToggleEnableKB",									
								},
								enableKillingBlowSound		=	{
									type		=	"toggle",
									name		=	"Enable sound file",
									order		=  	2,
									desc		=	"Enables playing of a sound file.",	
									get			=	"EnableKBSound",
									set			=	"ToggleEnableKBSound",									
								},
								
							},
						},
				
			},			
	
}


--create default db settings
--enableMSG = sets the printing of the chat message to true meaning when a player first logs in the option will be enabled
--enableKBT = sets "killing blow!" to true meaning when a player first logs in the option will be enabled
--message = sets the message that will be printed to chat using enableMSG to "default"
local defaults = {
    profile = {
		enablePVP= true,
		enableMSG= true,
		enableKBT= true,
		enableKB= true,
		enableKBSound= true,
		enableSnap = false,
		enableKBTDrag= false,
		prevSession = 0;
		chatChannel = "say",
		message = "default",
		sound = "DF UI_GuildLevelUp",
		killingMessage = "KILLING BLOW!",
		trackerBackground = "Blizzard Dialog Background",
		trackerBorder	= "Blizzard Tooltip",
		fontSize = 30,
		visibleTime = 3,
		fadeTime = 1,
		trackerFont = "Friz Quadrata TT",
		font = "Friz Quadrata TT",
		outline = "OUTLINE",
		red = 1,
		green = 1,
		blue = 0,
    },
};

--intitialize chatcommand(/kba) and dependencies
function KillingBlowAnnouncer:OnInitialize()
	KillingBlowAnnouncer:RegisterChatCommand("kba", "KillingBlowAnnouncerSlash")
	
	self.db = LibStub("AceDB-3.0"):New("kbaDB", defaults, "Default")	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("KillingBlowAnnouncer", options)		
	self.KillingBlowAnnouncerOptions = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("KillingBlowAnnouncer", "KillingBlowAnnouncer")
end;

--set defaults for addon.
KillingBlowAnnouncer.enablePVP = true;
KillingBlowAnnouncer.enableMSG = true;
KillingBlowAnnouncer.enableKBT = true;
KillingBlowAnnouncer.enableKB = true;
KillingBlowAnnouncer.enableKBSound = true;
KillingBlowAnnouncer.enableSnap = false;
KillingBlowAnnouncer.enableKBTDrag= false;
KillingBlowAnnouncer.chatChannel = "say";
KillingBlowAnnouncer.trackerBackground = "Blizzard Dialog Background";
KillingBlowAnnouncer.trackerBorder	= "Blizzard Tooltip";
KillingBlowAnnouncer.fontSize = 30;
KillingBlowAnnouncer.trackerFont = "Friz Quadrata TT";
KillingBlowAnnouncer.font = "Friz Quadrata TT";
KillingBlowAnnouncer.sound = "DF UI_GuildLevelUp";
KillingBlowAnnouncer.outline = "OUTLINE";
KillingBlowAnnouncer.visibleTime = 3;
KillingBlowAnnouncer.fadeTime = 1;
KillingBlowAnnouncer.red = 1;
KillingBlowAnnouncer.green = 1;
KillingBlowAnnouncer.blue = 0;
KillingBlowAnnouncer.prevSession = 0;

--getters and setters for database values
function KillingBlowAnnouncer:EnablePVP(info)
    return self.db.profile.enablePVP;
end;

function KillingBlowAnnouncer:ToggleEnablePVP(info, value)
    self.db.profile.enablePVP = value;	
end;

function KillingBlowAnnouncer:EnableMSG(info)
    return self.db.profile.enableMSG;
end;

function KillingBlowAnnouncer:ToggleEnableMSG(info, value)
    self.db.profile.enableMSG = value;	
end;

function KillingBlowAnnouncer:GetChatChannel(info)
    return self.db.profile.chatChannel;
end;

function KillingBlowAnnouncer:SetChatChannel(info, value)
    self.db.profile.chatChannel = value;	
end;

function KillingBlowAnnouncer:EnableKBSound(info)
    return self.db.profile.enableKBSound;
end;

function KillingBlowAnnouncer:ToggleEnableKBSound(info, value)
    self.db.profile.enableKBSound = value;	
end;

function KillingBlowAnnouncer:EnableKB(info)
    return self.db.profile.enableKB;
end;

function KillingBlowAnnouncer:ToggleEnableKB(info, value)
    self.db.profile.enableKB = value;	
end;

function KillingBlowAnnouncer:GetVisibleTime(info)
    return self.db.profile.visibleTime;
end;

function KillingBlowAnnouncer:SetVisibleTime(info, value)	
    self.db.profile.visibleTime = value;	
	setTime()	
end;

function KillingBlowAnnouncer:GetFadeTime(info)
    return self.db.profile.fadeTime;
end;

function KillingBlowAnnouncer:SetFadeTime(info, value)	
    self.db.profile.fadeTime = value;	
	setTime();	
end;

function KillingBlowAnnouncer:GetTrackerBackground(info)
    return self.db.profile.trackerBackground;
end;

function KillingBlowAnnouncer:SetTrackerBackground(info, value)	
    self.db.profile.trackerBackground = value;	
	setBackdrop();
end;

function KillingBlowAnnouncer:GetTrackerBorder(info)
    return self.db.profile.trackerBorder;
end;

function KillingBlowAnnouncer:SetTrackerBorder(info, value)	
    self.db.profile.trackerBorder = value;	
	setBackdrop();
end;

function KillingBlowAnnouncer:GetFontSize(info)
    return self.db.profile.fontSize;
end;

function KillingBlowAnnouncer:SetFontSize(info, value)	
    self.db.profile.fontSize = value;	
	setFont();	
end;

function KillingBlowAnnouncer:GetFont(info)
    return self.db.profile.font;
end;

function KillingBlowAnnouncer:SetFont(info, value)	
    self.db.profile.font = value;	
	setFont();	
end;

function KillingBlowAnnouncer:GetTrackerFont(info)
    return self.db.profile.trackerFont;
end;

function KillingBlowAnnouncer:SetTrackerFont(info, value)	
    self.db.profile.trackerFont = value;	
	setTrackerFont();	
end;

function KillingBlowAnnouncer:GetSound(info)
    return self.db.profile.sound;
end;

function KillingBlowAnnouncer:SetSound(info, value)	
    self.db.profile.sound = value;	
end;

function KillingBlowAnnouncer:GetOutline(info)
    return self.db.profile.outline;
end;

function KillingBlowAnnouncer:SetOutline(info, value)	
    self.db.profile.outline = value;	
	setFont();	
end;

function KillingBlowAnnouncer:EnableSnap(info)
    return self.db.profile.enableSnap;
end;

function KillingBlowAnnouncer:ToggleEnableSnap(info, value)
    self.db.profile.enableSnap = value;	
	snapMessage();
end;

function KillingBlowAnnouncer:EnableKBTDrag(info)
    return self.db.profile.EnableKBTDrag;
end;

function KillingBlowAnnouncer:ToggleEnableKBTDrag(info, value)
    self.db.profile.EnableKBTDrag = value;
	lockKbtracker();
end;

function KillingBlowAnnouncer:EnableKBT(info)
    return self.db.profile.EnableKBT;
end;

function KillingBlowAnnouncer:ToggleEnableKBT(info, value)
    self.db.profile.EnableKBT = value;
	hideKbtracker();
end;

function KillingBlowAnnouncer:GetKillingMessage(info)
    return self.db.profile.killingMessage;
end;

function KillingBlowAnnouncer:SetKillingMessage(info, newValue)
    self.db.profile.killingMessage = newValue;
	setKillingMsg();
end;

function KillingBlowAnnouncer:GetMessage(info)
    return self.db.profile.message;
end;

function KillingBlowAnnouncer:SetMessage(info, newValue)
    self.db.profile.message = newValue;
end;

function KillingBlowAnnouncer:GetRed(info)
    return self.db.profile.red;
end;

function KillingBlowAnnouncer:SetRed(info, newValue)
    self.db.profile.red = newValue;
end;

function KillingBlowAnnouncer:GetGreen(info)
    return self.db.profile.green;
end;

function KillingBlowAnnouncer:SetGreen(info, newValue)
    self.db.profile.green = newValue;
end;

function KillingBlowAnnouncer:GetBlue(info)
    return self.db.profile.blue;
end;

function KillingBlowAnnouncer:SetBlue(info, newValue)
    self.db.profile.blue = newValue;
end;

function KillingBlowAnnouncer:GetPrevSession(info)
    return self.db.profile.prevSession;
end;

function KillingBlowAnnouncer:SetPrevSession(info, newValue)
    self.db.profile.prevSession = newValue;
end;


-- open ingame options menu when /kba gets called
function KillingBlowAnnouncer:KillingBlowAnnouncerSlash(input)
	InterfaceOptionsFrame_OpenToCategory("KillingBlowAnnouncer");
	InterfaceOptionsFrame_OpenToCategory("KillingBlowAnnouncer");
end;

-- create startup event when player logs in
local startUp = CreateFrame("Frame")
startUp:RegisterEvent("PLAYER_LOGIN");

--set kbs_tracker variables to zero
local kbs_session = 0;
local kbs_cZone = 0;
local kbs_pZone = 0;


--function that gets called when an event within startUp triggers.
startUp:SetScript("OnEvent", function(self, event, ...)
    --print startup message for addon
	print("|cffffff00Killing Blow Announcer|r|cff00E5EE v1.06");
		
	--create killing blow message frame.
	message = CreateFrame("MessageFrame", "KBFrame", UIParent)

	--set place to display message in the center
	message:SetPoint("LEFT")
	message:SetPoint("RIGHT")
	message:SetPoint("CENTER")
	
	--set height of message
	message:SetHeight(30)
	
	--set where message gets inserted into the frame
	message:SetInsertMode("TOP")
	
	--set layer at high(ATTENTION CHILD FRAMES ALWAYS GET INSERTED ABOVE THEIR PARENT)
	message:SetFrameStrata("HIGH")
	
	--set amount of seconds before message fades(3 seconds)
	message:SetTimeVisible(KillingBlowAnnouncer:GetVisibleTime(info))
	
	--set amount of seconds it takes for message to fade(1 second)
	message:SetFadeDuration(KillingBlowAnnouncer:GetFadeTime(info))
	
	--set message font,font-size,and flags
	--max font size 30
	--flags: 
	--MONOCHROME - Font is rendered without antialiasing
	--OUTLINE - Font is displayed with a black outline
	--THICKOUTLINE - Font is displayed with a thick black outline
	message:SetFont(Media:Fetch("font", KillingBlowAnnouncer:GetFont(info)), KillingBlowAnnouncer:GetFontSize(info), KillingBlowAnnouncer:GetOutline(info))
	
	--snap edit killing blow message to screen if enabled
	snapMessage();
		
	--display or hide the kbtracker
	hideKbtracker();
	
    --enable or disable drag on startup
	lockKbtracker();
	
	--set tracker backdrop
	setBackdrop();
	
	
	if(KillingBlowAnnouncer:GetPrevSession(info) == 0) then
		new = kbs_session + 1;
		old = KillingBlowAnnouncer:GetPrevSession(info) + 1;
		p = format("%.2f",(new- old)/ old  * 100)
	else
		p = format("%.2f",(kbs_session- KillingBlowAnnouncer:GetPrevSession(info))/ KillingBlowAnnouncer:GetPrevSession(info)  * 100)
	end
	
	p = tonumber(p)
	
	if(p < 0) then 
		zone = "|cffE2252D" .. p;
	elseif(p > 0)then
		zone = "|cff2ed689" .. p;
	else
		zone = "|cFFFFFFFF" .. p;
	end
	
	kbtrackerText:SetText("|cFFFFFFFF          Killing blow tracker\n"
							 .. "|cffffcc00Current zone:\n"
						     .. "Previous zone:\n\n" 
							 .. "Current session:\n"
							 .. "Previous session:" )
	kbtrackerTextVars:SetText("|cFFFFFFFF"..kbs_cZone .."\n"
						     .. kbs_pZone .."\n\n" 
							 .. kbs_session.."\n"
							 .. KillingBlowAnnouncer:GetPrevSession(info))	  					 
	kbtrackerTextProc:SetText("|cFFFFFFFF0%\n\n\n"..zone.."%\n")

	
	setTrackerFont();
end);

local checkKill = CreateFrame("Frame")
checkKill:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");

-- function for checking if a player gets a killing blow 
checkKill:SetScript("OnEvent", function(self, event, timestamp, subevent, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, ...)
	local spellID, spellName, spellSchool, amount, overkill
	local playerName = UnitName("player");
	local petName = UnitName("pet");
	
            --IF event = a kill that happens within party(yours or anyone else in your raid/party)
			if (subevent == "PARTY_KILL") 
			--AND if the source of the event is the player(meaning you) or your pet 
			and (sourceName == playerName or sourceName == petName)  
			--AND if the killed enemy is a player instead of an npc/monster etc
			and (destFlags == 66888  or KillingBlowAnnouncer:EnablePVP(info) == false) 
			--THEN add the message KILLING BLOW! to the messageframe, and update the kbs_tracker
			then		
				--IF enableKB = true			
				if (KillingBlowAnnouncer:EnableKB(info) == true) 
				--THEN addMessage
				then
					message:AddMessage(KillingBlowAnnouncer:GetKillingMessage(info),KillingBlowAnnouncer:GetRed(info),KillingBlowAnnouncer:GetGreen(info),KillingBlowAnnouncer:GetBlue(info))	
				end
				
				--IF enableKBSound = true			
				if (KillingBlowAnnouncer:EnableKBSound(info) == true) 
				--THEN playsoundfile
				then
					PlaySoundFile(Media:Fetch("sound", KillingBlowAnnouncer:GetSound(info)), "Master");
				end
				
				--IF enableKBT = true
				if (KillingBlowAnnouncer:EnableKBT(info) == true) 
				--THEN update kbtracker
				then
					updateKbtracker();
				end
				
				--IF chat message is enabled(variable enableMSG needs to be true for this)
				if (KillingBlowAnnouncer:EnableMSG(info) == true) 
				--THEN get message from variable message and send it to the chat chat
				then
					SendChatMessage(KillingBlowAnnouncer:GetMessage(info),KillingBlowAnnouncer:GetChatChannel(info));
				end			
		end		
end);


-- Creating kbtracker frame	
kbtracker = CreateFrame("Frame", "DragFrame2", UIParent)

	  --set frame to move on left mouse button
	  kbtracker:SetMovable(true)
	  kbtracker:EnableMouse(true)
	  kbtracker:RegisterForDrag("LeftButton")

	  --set default place for frame
      kbtracker:SetPoint("CENTER")
	  --95
	  kbtracker:SetSize(200,90)	

	  --create textframe within kbtracker
	  kbtrackerText =  kbtracker:CreateFontString("frametext" ,"ARTWORK","GameFontNormal");
	  kbtrackerTextVars =  kbtracker:CreateFontString("frametext" ,"ARTWORK","GameFontNormal");
	  kbtrackerTextProc =  kbtracker:CreateFontString("frametext" ,"ARTWORK","GameFontNormal");
							
	  --set position of text in frame
      kbtrackerText:SetPoint("TopLeft", kbtracker, "TopLeft", 5, -8)
	  kbtrackerTextVars:SetPoint("TopLeft", kbtracker, "TopLeft", 130, -22)
	  kbtrackerTextProc:SetPoint("TopLeft", kbtracker, "TopLeft", 160, -16)
							 
	  
	  --align text 
	  kbtrackerText:SetJustifyH("left")
	  kbtrackerTextVars:SetJustifyH("right")
	  kbtrackerTextProc:SetJustifyH("right")
 
 --lock or unlock kbtracker based on options
function lockKbtracker() 
	if (KillingBlowAnnouncer:EnableKBTDrag(info) == true) then
	  kbtracker:SetScript("OnDragStart", kbtracker.StartMoving)
	  kbtracker:SetScript("OnDragStop", kbtracker.StopMovingOrSizing)
	else
	  kbtracker:SetScript("OnDragStart", nil)
	  kbtracker:SetScript("OnDragStop", nil)
	end
end
 
-- function for updating the frame
function updateKbtracker()
	--get zone information
	local pvpType, isFFA, faction = GetZonePVPInfo();
	
	--if pvptype is contested(everywhere in a pvp server)
	if pvpType == "contested" then
		--increase kbs_cZone
		kbs_cZone = kbs_cZone + 1;
	end
	
	--increase kbs_session
	kbs_session = kbs_session + 1;

	
	if(kbs_pZone == 0) then
		new = kbs_cZone + 1;
		old = kbs_pZone + 1;
		p = format("%.2f",(new- old)/ old  * 100)
	else
		p = format("%.2f",(kbs_cZone- kbs_pZone)/ kbs_pZone  * 100)
	end
	
	p = tonumber(p)
	
	if(p < 0) then 
		zone = "|cffE2252D" .. p;
	elseif(p > 0)then
		zone = "|cff2ed689" .. p;
	else
		zone = "|cFFFFFFFF" .. p;
	end

	
	if(KillingBlowAnnouncer:GetPrevSession(info) == 0) then
		new = kbs_session + 1;
		old = KillingBlowAnnouncer:GetPrevSession(info) + 1;
		s = format("%.2f",(new- old)/ old  * 100)
	else
		s = format("%.2f",(kbs_session- KillingBlowAnnouncer:GetPrevSession(info))/ KillingBlowAnnouncer:GetPrevSession(info)  * 100)
	end
	
	s = tonumber(s)
	
	if(s < 0) then 
		s = "|cffE2252D" .. s;
	elseif(s > 0)then
		s = "|cff2ed689" .. s;
	else
		s = "|cFFFFFFFF" .. s;
	end
	
	--update text on tracker
	  kbtrackerText:SetText("|cFFFFFFFF          Killing blow tracker\n"
							 .. "|cffffcc00Current zone:\n"
						     .. "Previous zone:\n\n" 
							 .. "Current session:\n"
							 .. "Previous session:" )
	  kbtrackerTextVars:SetText("|cFFFFFFFF"..kbs_cZone .."\n"
						     .. kbs_pZone .."\n\n" 
							 .. kbs_session.."\n"
							 .. KillingBlowAnnouncer:GetPrevSession(info))	  					 
	  kbtrackerTextProc:SetText(zone.."%\n\n\n"..s.."%\n")
	
end


--function for hiding kbtracker frame
function hideKbtracker()	
	--IF enableKBT = false then hide otherwise show kbtracker
	if (KillingBlowAnnouncer:EnableKBT(info) == false) then
			kbtracker:Hide();
	else 
			kbtracker:Show();
	end	
end;


--create a frame and register when player changes zones
--EXAMPLE from jade forest -> Kun lai = event occured
--EXAMPLE from out of bg -> in to bg = event occured
--EXAMPLE from in of bg -> out of bg = event occured
local resetTrackerVariable = CreateFrame("Frame") 
resetTrackerVariable:RegisterEvent("ZONE_CHANGED_NEW_AREA")


--on end of bg set kbs_bg variable to zero and update kbtracker text.
resetTrackerVariable:SetScript("OnEvent",function(self, event, addon)
	kbs_pZone = kbs_cZone;
	kbs_cZone = 0;
	 
	if(kbs_pZone == 0) then
		old = kbs_pZone + 1;
		new = kbs_cZone + 1;
		p = format("%.2f",(new- old)/ old  * 100)
	else
		p = format("%.2f",(kbs_cZone- kbs_pZone)/ kbs_pZone  * 100)
	end
	
	p = tonumber(p)
	
	if(p < 0) then 
		zone = "|cffE2252D" .. p;
	elseif(p > 0)then
		zone = "|cff2ed689" .. p;
	else
		zone = "|cFFFFFFFF" .. p;
	end
	 
	 if(KillingBlowAnnouncer:GetPrevSession(info) == 0) then
		new = kbs_session + 1;
		old = KillingBlowAnnouncer:GetPrevSession(info) + 1;
		s = format("%.2f",(new- old)/ old  * 100)
	else
		s = format("%.2f",(kbs_session- KillingBlowAnnouncer:GetPrevSession(info))/ KillingBlowAnnouncer:GetPrevSession(info)  * 100)
	end
	
	s = tonumber(s)
	
	if(s < 0) then 
		s = "|cffE2252D" .. s;
	elseif(s > 0)then
		s = "|cff2ed689" .. s;
	else
		s = "|cFFFFFFFF" .. s;
	end

	
	  --update text on tracker
	  kbtrackerText:SetText("|cFFFFFFFF          Killing blow tracker\n"
							 .. "|cffffcc00Current zone:\n"
						     .. "Previous zone:\n\n" 
							 .. "Current session:\n"
							 .. "Previous session:" )
	  kbtrackerTextVars:SetText("|cFFFFFFFF"..kbs_cZone .."\n"
						     .. kbs_pZone .."\n\n" 
							 .. kbs_session.."\n"
							 .. KillingBlowAnnouncer:GetPrevSession(info))	  					 
	  kbtrackerTextProc:SetText(zone.."%\n\n\n"..s.."%\n")
	  
end)

local playerLeave = CreateFrame("Frame") 
playerLeave:RegisterEvent("PLAYER_LOGOUT")

playerLeave:SetScript("OnEvent",function(self, event, addon)
	  KillingBlowAnnouncer:SetPrevSession(info, kbs_session)
end)


--Snap killing blow message frame to window when enabled and when disabled clear and set timevisible back to normal
function snapMessage()
	--EnableSnap is false then set back to database value else if edit mode is true then set time to 10000
	if (KillingBlowAnnouncer:EnableSnap(info) == false) then
			message:SetTimeVisible(KillingBlowAnnouncer:GetVisibleTime(info))
			message:Clear()				
	else 
			message:SetTimeVisible(10000)
			message:AddMessage(KillingBlowAnnouncer:GetKillingMessage(info),KillingBlowAnnouncer:GetRed(info),KillingBlowAnnouncer:GetGreen(info),KillingBlowAnnouncer:GetBlue(info))
	end	

end

--setters for changing the snap message to the changed values
--setFont when changed in options menu
function setFont()
	message:SetFont(Media:Fetch("font", KillingBlowAnnouncer:GetFont(info)), KillingBlowAnnouncer:GetFontSize(info), KillingBlowAnnouncer:GetOutline(info))
end

function setTrackerFont()
	kbtrackerText:SetFont(Media:Fetch("font", KillingBlowAnnouncer:GetTrackerFont(info)), 12, "OUTLINE")
	kbtrackerTextVars:SetFont(Media:Fetch("font", KillingBlowAnnouncer:GetTrackerFont(info)), 12, "OUTLINE")
	kbtrackerTextProc:SetFont(Media:Fetch("font", KillingBlowAnnouncer:GetTrackerFont(info)), 12, "OUTLINE")
	width = kbtrackerText:GetWidth() + kbtrackerTextVars:GetWidth() + kbtrackerTextProc:GetWidth()
	kbtracker:SetWidth(math.floor(width))
end

--setColors when changed in options menu
function setColors()
	if (KillingBlowAnnouncer:EnableSnap(info) == true) then
		message:Clear()		
		message:AddMessage(KillingBlowAnnouncer:GetKillingMessage(info),KillingBlowAnnouncer:GetRed(info),KillingBlowAnnouncer:GetGreen(info),KillingBlowAnnouncer:GetBlue(info))
	end
end

--setTime when changed in options menu
function setTime()
	if (KillingBlowAnnouncer:EnableSnap(info) == false) then
		message:SetTimeVisible(KillingBlowAnnouncer:GetVisibleTime(info))
		message:SetFadeDuration(KillingBlowAnnouncer:GetFadeTime(info))
	end
end

--setKillingMsg when changed in options menu
function setKillingMsg()
	if (KillingBlowAnnouncer:EnableSnap(info) == true) then
		message:Clear()		
		message:AddMessage(KillingBlowAnnouncer:GetKillingMessage(info),KillingBlowAnnouncer:GetRed(info),KillingBlowAnnouncer:GetGreen(info),KillingBlowAnnouncer:GetBlue(info))
	end
end

function setBackdrop()
	--setbackground and border for tracker
	backdrop = {
			bgFile = Media:Fetch("background", KillingBlowAnnouncer:GetTrackerBackground(info)), 
			edgeFile= Media:Fetch("border", KillingBlowAnnouncer:GetTrackerBorder(info)),
			tile = false,   
			tileSize = 32,  
			edgeSize = 10, 
			insets = {   
				left = 0,
				right = 0,
				top = 0,
				bottom = 0
			}
	}   
	kbtracker:SetBackdrop(backdrop);
end