-- Natur EnemyCastBar PvP Module LUA

function NECB_PvP_Control(mob, spell, special, bcasted, event, rank)

	if (CEnemyCastBar.bPvP) then

		--BC casttime and spell adjustment; won't show multiple bars if mobs with same name cast!
		if ((special == "casts" and (UnitName("target") == mob or (UnitName("focus") == mob and CEnemyCastBar.bFocusPvP)) and event ~= "UNIT_SPELLCAST_START" and event ~= "UNIT_SPELLCAST_CHANNEL_START") --  'and PvECheckPassed' removed; currently see no need+bug; (before:)don't use ctype check here, otherwise PvE cooldowns might be blocked by PvP spells!
			or (special == "instcast" and (not CEnemyCastBar_Spells[spell] or not CEnemyCastBar_Spells[spell].icasted))
			) then
			return; -- because SPELLCAST_START will catch it!
				-- and instcast barrier
		end

		-- initialize locals, since this a separated mod now
		-- Network BC TempBCaster
		local clientlang = GetLocale();	
		-- initialize vars
		local ctype, castime;


		-- check if spell stacks (careful: casts like fearie fire (terror bear) must be rebuilt)
		-- now uses strings, too, but won't be cleared on fades!
		local fbracket,_,spellstacks = string.find(spell, "(%(.+%))" );
		if (fbracket) then
			spell = string.sub(spell, 1, fbracket - 2);
		end

		local spelllength = string.len (spell);
		-- crop the german aposthrophes from some spells (')
		local spell_de = string.sub(spell, 2, spelllength - 1);
		if (CEnemyCastBar_Spells[spell_de]) then spell = spell_de;
		end

		-- CD Buffer Routine
		if (CEnemyCastBar.bCDown
			and CEnemyCastBar_Spells[spell]
			and CEnemyCastBar_Spells[spell].d
			and not (UnitName("target") == mob and not UnitIsPlayer("target"))
			and not (UnitName("focus") == mob and not UnitIsPlayer("focus"))
			and not (CEnemyCastBar_Spells[spell].g == 0 and special == "gains")
		) then

			if (special == "casts" or special == "gains" or special == "performs" or special == "instcast") then

				castime = CEnemyCastBar_Spells[spell].d;

				if (special == "casts" and CEnemyCastBar_Spells[spell].t and not CEnemyCastBar_Spells[spell].channeled) then
					castime = castime + CEnemyCastBar_Spells[spell].t;
				end

				local i = 1;
				local CDDBUpdated = false;
				-- add CD to database
				while i <= #NECB_CD_DB do
					if ( NECB_CD_DB[i][1] == mob and NECB_CD_DB[i][2] == spell ) then
						NECB_CD_DB[i][3] = GetTime();
						CDDBUpdated = true;
					elseif ( GetTime() > ( NECB_CD_DB[i][3] + NECB_CD_DB[i][4] ) ) then
						table.remove(NECB_CD_DB, i);
						i = i - 1;
					end
					i = i + 1;
				end

				if ( not CDDBUpdated and mob ~= UnitName("player") and not CEnemyCastBar_Spells[spell].disabled and #NECB_CD_DB < 50) then
					table.insert(NECB_CD_DB, { mob, spell, GetTime(), castime } );
					--DEFAULT_CHAT_FRAME:AddMessage("Mob: "..mob.." |Spell: "..spell);
				end
			end
		end
		-- CD Buffer Routine finished

		if ((UnitName("target") == mob or (UnitName("focus") == mob and CEnemyCastBar.bFocusPvP) or (CEnemyCastBar.bGlobalPvP and (not CEnemyCastBar.bHostileOnly or CEnemyCastBar_IsHostile(event))) ) and mob ~= UnitName("player")) then

			if (special == "instcast") then
				special = "gains"; --rename to make things easier ('summon water elemental'); to display cd and to color bar if 't'
			end

		    if (special == "casts" or special == "gains" or special == "performs") then

			if (CEnemyCastBar_Spells[spell]) then


				if (
						-- check if previously disabled with shift + leftclick
					CEnemyCastBar_Spells[spell].disabled
						-- check if only gains are allowed
					or (CEnemyCastBar.bGainsOnly and CEnemyCastBar.bGains and special ~= "gains")
					) then
					return;
				end

				local icontex = CEnemyCastBar_Spells[spell].icontex; -- > get icon texture

				-- new with NECB 6.9.x, to update focus bars
				local necb_unit = "target";
				if (UnitName("focus") == mob) then
					necb_unit = "focus";
				end

				if (special ~= "gains") then
					if (mob == UnitName(necb_unit)) then
						if (UnitIsEnemy("player", necb_unit) ) then
							ctype = "hostile";
						else
							ctype = "friendly";
						end
	
					else
						if (event == "CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE" or event == "CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE") then
							ctype = "hostile";
						else
							ctype = "friendly";
						end
	
					end
				end


				if (CEnemyCastBar_Spells[spell].i) then
				
					castime = CEnemyCastBar_Spells[spell].i;
					CEnemyCastBar_Show(mob, spell, castime, ctype, nil, icontex);					
				
				end
				
				if (CEnemyCastBar_Spells[spell].d and CEnemyCastBar.bCDown and UnitName(necb_unit) == mob and UnitIsPlayer(necb_unit) ) then

					if (special == "gains" and CEnemyCastBar.bGains and not (CEnemyCastBar_Spells[spell].g == 0)) then

						castime = CEnemyCastBar_Spells[spell].d;
						if (CEnemyCastBar_UniqueCheck(spell.." (CD)", castime, mob, "trueupdate", nil, nil,"cooldown") == 0) then
							CEnemyCastBar_Show(mob, spell.." (CD)", castime, "cooldown", nil, icontex);
						end
					end
				end


				if (event == "UNIT_SPELLCAST_START") then
					castime = bcasted;
					bcasted = false;
				elseif (event == "UNIT_SPELLCAST_CHANNEL_START") then
					castime = bcasted;
					bcasted = true; --tell 'show' that a channeling spell comes
				else
					castime = CEnemyCastBar_Spells[spell].t; -- but if only "d=" is applied then "t=nil", so check at end of this block
					bcasted = false;
				end
				
				-- Spell might have the same name but a different cast time on another mob, ie. Death Talon Hatchers/Players on Bellowing Roar
				if (CEnemyCastBar_Spells[spell].r) then
				
					if (mob == CEnemyCastBar_Spells[spell].r) then
					
						castime = CEnemyCastBar_Spells[spell].a;
					
					end
				
				end

				if (special == "gains") then

					if (CEnemyCastBar.bGains) then

						if (event and (string.match(event, "HOSTILE") or string.match(event, "CREATURE") ) and not (UnitName(necb_unit) == mob and not UnitIsEnemy("player", necb_unit)) ) then
							ctype = "gainsfoe";
						else
							ctype = "gainsfriend";
						end
						if (CEnemyCastBar_Spells[spell].g) then
						
							castime = CEnemyCastBar_Spells[spell].g;
						
						end
						if (CEnemyCastBar_Spells[spell].HoT) then
							alreadyshowing = CEnemyCastBar_UniqueCheck(spell,castime,mob,"trueupdate","HoT", nil, "allgains");
						else
							alreadyshowing = CEnemyCastBar_UniqueCheck(spell,castime,mob,"trueupdate",spellstacks, nil,"allgains");
						end
						
					else

						return;

					end

				end

				if (CEnemyCastBar_Spells[spell].c) then
				
					ctype = CEnemyCastBar_Spells[spell].c;
				
				end

				-- rebuild original spellname for casts with bracket (fearie fire in bearform etc.)
				if (special == "casts" and spellstacks) then
					spell = spell.." "..spellstacks;
					spellstacks = nil;
				end

				-- display spell rank for real PvP (hostile + friendly)
				if (rank) then
					if (spellstacks) then
						spellstacks = rank .. spellstacks;
					else
						spellstacks = rank;
					end
				end

				if (castime and castime ~= 0 and not (special == "gains" and alreadyshowing == 1) ) then
					CEnemyCastBar_Show(mob, spell, castime, ctype, nil, icontex, nil, spellstacks, nil, bcasted); -- bcasted = channeling or not

					--play sound warning
					if (CEnemyCastBar.bPvPSoundWarn and CEnemyCastBar_Spells[spell].swarn and mob == UnitName(necb_unit) and UnitIsPlayer(necb_unit) and UnitIsEnemy("player", necb_unit) ) then
						PlaySoundFile("Sound\\Spells\\Thorns.wav");
					end
				end

		 	end
		
		    end

		end
			
	end

end


NECB_PvP_Module_loaded = true;
--NECB_InfoFrame:AddMessage("|cffaaaaaaNECB:|r PvP Module loaded into memory!", 1, 0.5, 0, 1, UIERRORS_HOLD_TIME);