
--BadKitty was conceived and written from scratch by Anatinus of Gul'dan.
--It is still a work in progress, and I intend to get more things working soon.
--I do not recommend using this as a platform for other projects, or as a 
--learning tool, because even though I'm proud of what the result of my work 
--is, I consider the code to be pretty messy.  You've been warned :P

--LEGAL: Blah blah blah use at your own risk blah blah blah.

--SHIT TO DO:
---options
---SavedVars could be useful once there's actually some options


function BadKitty_Output(text)
	DEFAULT_CHAT_FRAME:AddMessage(text);
end

function BadKitty_OnLoad()
	this:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
	BadKitty_Output("BadKitty loaded successfully!")
	BadKittyFrameCPBar:SetValue(0)
	SlashCmdList["BADKITTY"] = BadKitty_Slashes
	SLASH_BADKITTY1 = "/badkitty"
	SLASH_BADKITTY2 = "/bk"
	BKtoggle = 1
end

BadKittySpells = {}
BadKittySpells[1] = "Mangle - Cat"
BadKittySpells[2] = "Savage Roar"
BadKittySpells[3] = "voidzillaland"--"Infected Wounds"
BadKittySpells[4] = "Rake"
BadKittySpells[5] = "Rip"
BadKittySpells[6] = "Faerie Fire (Feral)"
--stuff that isn't yours but you should care about--
BadKittySpells[7] = "Trauma"
BadKittySpells[8] = "Mangle - Bear"
BadKittySpells[9] = "Faerie Fire"

BadKittyUp = {}
BadKittyUp[0] = {}
BadKittyUp[1] = {}
BadKittyUp[2] = {}
BadKittyUp[3] = {}
BadKittyUp[4] = {}
BadKittyUp[5] = {}
BadKittyUp[6] = {}
BadKittyUp[7] = {}
BadKittyUp[8] = {}
BadKittyUp[9] = {}
BadKittyUp[0][1] = "cap"
BadKittyUp[1][1] = 0
BadKittyUp[2][1] = 0
BadKittyUp[3][1] = 0
BadKittyUp[4][1] = 0
BadKittyUp[5][1] = 0
BadKittyUp[6][1] = 0
BadKittyUp[7][1] = 0
BadKittyUp[8][1] = 0
BadKittyUp[9][1] = 0

BadKittyUp[0][-1] = ""	--these may or may not be necessary
BadKittyUp[1][-1] = 0
BadKittyUp[2][-1] = 0
BadKittyUp[3][-1] = 0
BadKittyUp[4][-1] = 0
BadKittyUp[5][-1] = 0
BadKittyUp[6][-1] = 0
BadKittyUp[7][-1] = 0
BadKittyUp[8][-1] = 0
BadKittyUp[9][-1] = 0


BadKittyDurations = {}
BadKittyDurations[1] = 18
BadKittyDurations[3] = 12
BadKittyDurations[4] = 9
BadKittyDurations[5] = 12
BadKittyDurations[6] = 40
BadKittyDurations[7] = 15
BadKittyDurations[8] = 12
BadKittyDurations[9] = 40



function BadKitty_OnEvent(self, event, ...)
	unittarget = UnitGUID("target")
	local spelltype = select(2,...);
	local user = select(4,...);
	local tarid = select(6,...);
	local spellname = select(10,...);
	local iter = 1;
	repeat
		if spellname==BadKittySpells[iter] then
			if spelltype ~= "SPELL_AURA_MISSED" then
				if  spelltype=="SPELL_AURA_APPLIED" or spelltype=="SPELL_AURA_REFRESH" or spelltype=="SPELL_AURA_APPLIED_DOSE" then
					if iter == 4 and UnitName("player") == user then--Only update MY Rake and MY Rip
						targ = BadKitty_SetTarget(tarid)
						BadKittyUp[iter][targ] = GetTime()
					elseif iter == 5 and UnitName("player") == user then
						local name, rank, icon, count, debuffType, duration, expirationTime, isMine, isStealable  =  UnitDebuff("target", "Rip");
						BadKittyUp[5][BadKitty_FindTarget(tarid)] = expirationTime
						BadKittyDurations[5] = -1
					else
						if type(tarid) == "table" then		--Bear Mangle gives a table of values when using Zerk
							for dex,val in pairs(tarid) do
								targ = BadKitty_SetTarget(val)
								BadKittyUp[iter][targ] = GetTime()
							end
						else
							targ = BadKitty_SetTarget(tarid)
							BadKittyUp[iter][targ] = GetTime()
						end
					end 
					if iter==2 and UnitName("player") == user then	--SR gets it's duration checked and ignores target
						targ = BadKitty_SetTarget(tarid)
						BadKittyUp[iter][targ] = GetTime()
						BadKittyUp[iter][1] = BadKittyUp[iter][targ]
						local name, rank, texture, count, debuffType, duration, timeLeft, untilCanceled  =  UnitAura(UnitName("player"), "Savage Roar");
						BadKittyDurations[2] = duration
					end
				end
			end
		end
		if spelltype=="SPELL_AURA_REMOVED" and spellname == BadKittySpells[iter] then
			targ = BadKitty_FindTarget(tarid)
			BadKittyUp[iter][targ] = 0;
		end
		iter = iter+1;
	until BadKittySpells[iter]==nil
end


 function BadKitty_UpdateMe()		--This fires once, and updates all bars except CP.
	index = BadKitty_FindTarget(UnitGUID("target"))
	--BadKitty_Output(index)
	local timenow = GetTime()
	fail = BadKitty_UpdateSR(timenow)
	local pointer = 1
	repeat
		local timeleft = 0	-- more magic
		if pointer == 1 then
			local name, rank, icon, count, debuffType, duration, expirationTime, isMine, isStealable  =  UnitDebuff("target", "Mangle - Cat");
			if name == nil then
				local name, rank, icon, count, debuffType, duration, expirationTime, isMine, isStealable  =  UnitDebuff("target", "Mangle - Bear");
				BadKittyUp[8][index] = expirationTime
			else
				BadKittyUp[1][index] = expirationTime
			end
			if BadKittyUp[7][index] >0 then					
				BadKittyUp[1][index] = BadKittyUp[7][index]
			end
			if BadKittyUp[8][index] ~= nil and BadKittyUp[8][index] >0 then
				BadKittyUp[1][index] = BadKittyUp[8][index]
			end
			BadKittyDurations[1] = -1
			if BadKittyUp[pointer][index] == nil then
				BadKittyUp[pointer][index] = 0
			end
		end
		if pointer==6 then
			if BadKittyUp[9][index] >0 then
				BadKittyUp[6][index] = BadKittyUp[9][index]
			end
			if BadKittyUp[pointer][index] == nil then
				BadKittyUp[pointer][index] = 0
			end 
		end
		--
		if BadKittyUp[pointer][index] >0 then
			if pointer == 4 then BadKitty_Output(BadKittyUp[pointer][index]) end
			if BadKittyDurations[pointer] >0 then
				timeleft = BadKittyDurations[pointer]-(timenow-BadKittyUp[pointer][index])
			else
				timeleft = BadKittyUp[pointer][index] - timenow
				--BadKitty_Output(pointer)
			end
			if timeleft < 0 then
				timeleft = 0
			end
		end
		if		pointer == 1 then BadKittyFrameMangleBar:SetValue(timeleft)
		elseif	pointer == 4 then BadKittyFrameRakeBar:SetValue(timeleft)
		elseif	pointer == 5 then BadKittyFrameRipBar:SetValue(timeleft)
		elseif	pointer == 6 then BadKittyFrameFFFBar:SetValue(timeleft)
		end
		pointer = pointer +1
	until pointer >9
end 

function BadKitty_FindTarget(target)
	zero = 0
	if target == nil then
		return -1
	end
	i = 1
	repeat
		if BadKittyUp[0][i] == target then
			return i
		end
		if BadKittyUp[0][i] == 0 then
			zero = i
		end
		i = i+1
	until BadKittyUp[0][i] == "cap" or BadKittyUp[0][i-1] == "cap"
	if zero ==0 then
		for q=1,9 do
			BadKittyUp[q][i]=0
		end
		BadKittyUp[0][i] = 0
		BadKittyUp[0][i+1] = "cap"			
		return i
	else
		return zero
	end
end

function BadKitty_SetTarget(target)
	index = BadKitty_FindTarget(target)
	BadKittyUp[0][index] = target
	return index
end

function BadKitty_UpdateSR(timenow)	--SR doesn't deal with targets. (bad programming)
	if BadKittyUp[2][1] > 0 then
		local timeleft = BadKittyDurations[2]-(timenow-BadKittyUp[2][1]);
		if timeleft < 0 then
			timeleft = 0
		end
		BadKittyFrameSRBar:SetValue(timeleft)
	else
		BadKittyFrameSRBar:SetValue(0)
	end
end

function BadKitty_PurgeTargets()	--erases target data if nothing's on that target.
	local i = 1						--coupled with FindSetTarget, it allows that index/slot
	local bool = 0					--to be reused instead of having to expand the array
	repeat							-- (no giant memory hogs after a raid)
		for q=1,9 do
			if BadKittyUp[q][i] ~= 0 then
				bool = 1
				break
			end
		end
		if bool == 0 then
			BadKittyUp[0][i] = 0
		else
			bool = 0
		end
		i = i+1
	until BadKittyUp[0][i] == "cap"
end
		
function BadKitty_UpdateCPs()
	cps = GetComboPoints(UnitName("player"))
	if cps ==1 then
		BadKittyFrameCPBar:SetStatusBarColor(0,1,0);
	end
	if cps ==2 then
		BadKittyFrameCPBar:SetStatusBarColor(0.25,0.75,0);
	end
	if cps ==3 then
		BadKittyFrameCPBar:SetStatusBarColor(0.5,0.5,0);
	end
	if cps ==4 then
		BadKittyFrameCPBar:SetStatusBarColor(0.75,0.25,0);
	end
	if cps ==5 then
		BadKittyFrameCPBar:SetStatusBarColor(1,0,0);
	end
	BadKittyFrameCPBar:SetValue(cps)
end 

function BadKitty_Slashes()
	if BKtoggle == 1 then
		BadKittyFrame:SetAlpha(0)
		BadKittyFrame:SetMovable(0)
		BadKitty_Output("BadKitty is now hidden.")
		BKtoggle = 0
	else
		BadKittyFrame:SetAlpha(1)
		BadKittyFrame:SetMovable(1)
		BadKitty_Output("BadKitty is now visible.")
		BKtoggle = 1
	end
end 