-- ==================================================================================================================== --
--															--
--	File			CC.lua											--
--															--
--															--
--	Author			Zarnivoop					| Version	   :	 10.0.2 |	--
--										| Curse-Project-ID :	 694380 |	--
--										| License	   :	  MIT/X |	--
--															--
--											| Basis, thx	|		--
--															--
--															--
--				Baltha						| Version	   :	 11.2.0 |	--
--										| Curse-Project-ID :	1188318 |	--
--										| License	   :	  MIT/X |	--
--															--
--											| Revision	|		--
--															--
--															--
--	Path ( Revision )	"..\World of Warcraft\_retail_\Interface\AddOns\Skada\Misc\Changelog\ChangeLog.txt"	--
--															--
-- ==================================================================================================================== --


local _, Skada	 = ...


local emptyLines = string.rep("\n", 2)


-- =========================
-- | outer function: start |
-- =========================
Skada:AddLoadableModule("CC", nil, function(Skada, L)


	if Skada.db.profile.modulesBlocked.CC then return end


	local mod  = Skada:NewModule(L["CC breakers"])


	local CCId = {

			-- ================================================================================
			-- | hint			: CC (Crowd Contol) effects may ends from damage. |
			-- ================================================================================


			-- banish			: target is made immune to all effects but is unable to perform any.
			-- charm			: target is under control of the caster.
			-- disorient			: target wanders around slowly, unable to perform actions.
			-- fear				: target runs randomly around.
			-- horror			: similar to Fear effect, but duration tends to be short.
			-- incapacitate			: a stun which breaks on damage to the target.
			-- polymorph			: target is transformed into a critter, unable to perform actions. most polymorph effects also include disorient.
			-- sleep			: target is put to sleep, unable to move or perform any actions.
			-- stun (controlled and random)	: target is unable to move or perform any actions.
			-- "turn" effect		: the paladins's [Turn Evil] functions very similarly to fear effects, but is considered a "turn" effect for the purpose of immunities.


			-- ===================================
			-- | classes and CC abilities in TWW |
			-- ===================================

			-- | Deamon Hunter
			[179057] = true,	-- | Chaos Nova			max  2 sec	stun
			[211881] = true,	-- | Fel Eruption		max  4 sec	stun (havoc)
			[217832] = true,	-- | Imprison			max 60 sec	demons, beasts, humanoids
			[191427] = true,	-- ! Metamorphosis		max  3 sec	stun (havoc)
			[207684] = true,	-- | Sigil of Misery		max 15 sec	disorient

			-- | Death Knight
			[221562] = true,	-- | Asphyxiate			max  5 sec	stun
			[111673] = true,	-- | Control Undead		max  5 min	charm, only usable against undeads
			[295953] = true,	-- | Gnaw (ghoul ability)	max  1 sec	stun

			-- | Druid
			[33786]  = true,	-- | Cyclone			max  5 sec	disorient, mostly PVP
			[2637]   = true,	-- | Hibernate			max 40 sec	sleep
			[22570]	 = true,	-- | Maim			max  _ sec	stun (cat form), duration increased per combo point
			[5211]	 = true,	-- | Mighty Bash		max  4 sec	stun

			-- | Evoker
			[433874] = true,	-- | Deep Breath		max  3 sec	stun
			[360806] = true,	-- | Sleep Walk			max 20 sec	disorient
			[371032] = true,	-- | Terror of the Skies	max  3 sec	stun

			-- | Hunter
			[109248] = true,	-- | Binding Shot		max  5 sec	stun, activated if enemies moves 5 yards from the arrow [, pvp max  3 sec]
			[187650] = true,	-- | Freezing Trap		max 60 sec	incapacitate
			[19577]	 = true,	-- | Intimidation		max  5 sec	stun
			[1513]	 = true,	-- | Scare Beast		max 20 sec	fear, only usable againsts beasts

			-- | Mage
			[31661]	 = true,	-- | Dragon's Breath		max  4 sec	disorient
			[61305]  = true,	-- | Polymorph Black Cat	max 60 sec	polymorph
			[28272]  = true,	-- | Polymorph Pig
			[61721]  = true,	-- | Polymorph Rabbit
			[118]    = true,	-- | Polymorph Sheep
			[61780]  = true,	-- | Polymorph Turkey
			[28271]  = true,	-- | Polymorph Turtle

			-- | Monk
			[119381] = true,	-- | Leg Sweep			max  3 sec	stun, knock down all enemies within 5 yards
			[115078] = true,	-- | Paralyse			max 60 sec	incapacitate

			-- | Paladin
			[115750] = true,	-- | Blinding Light		max  6 sec	disorient
			[853]	 = true,	-- | Hammer of Justice		max  6 sec	stun
			[20066]	 = true,	-- | Repentance			max 60 sec	incapacitate, usable against humanoids, demons, undead, dragonkin, and giants
			[10326]	 = true,	-- | Turn Evil			max 40 sec	"turn" effect, usable against undead, aberration or demon

			-- | Priest
			[205364] = true,	-- | Dominate Mind		max 30 sec	charm, not demons, undead, mechanicals or players
			[605]	 = true,	-- | Mind Control		max 30 sec	charm, not demons, undead or mechanicals
			[64044]	 = true,	-- | Psychic Horror		max  4 sec	stun, shadow only
			[8122]	 = true,	-- | Psychic Scream		max  8 sec	fear
			[9484]	 = true,	-- | Shackle Undead		max 50 sec	charm

			-- | Rogue
			[2094]	 = true,	-- | Blind			max 60 sec	disorient
			[1833]   = true,	-- | Cheap Shot			max  6 sec	stun
			[1776]	 = true,	-- | Gouge			max  4 sec	incapacitate
			[408]	 = true,	-- | Kidney Shot		max  8 sec	stun length depends on combo points
			[6770]   = true,	-- | Sap			max 60 sec	sleep

			-- | Shaman
			[192058] = true,	-- | Capacitor Totem		max  3 sec	AoE stun (8 yards)
			[51514]  = true,	-- | Hex			max 60 sec	polymorph

			-- | Warlock
			[119914] = true,	-- | Axe Toss			max  4 sec	stun
			[710]	 = true,	-- | Banisch			max 30 sec	banish
			[5782]	 = true,	-- | Fear			max 20 sec	fear
			[5484]	 = true,	-- | Howl of Terror		max 20 sec	fear
			[30283]	 = true,	-- | Shadowfury			max  3 sec	AoE stun (8 yards)
			[1098]	 = true,	-- | Subjugate Demon		max 10 min	charm, only demon

			-- | Warrior
			[5246]	 = true,	-- | Intimidating Shout		max  8 sec	(AoE) fear (8 yards, 1 til max 5 enemies)
			[275338] = true,	-- | Menace			max 15 sec	fear, knock back all nearby enemies except your primary target
			[385952] = true,	-- | Shield Charge		max  4 sec	stun (10 yards, only protection)
			[46968]	 = true,	-- | Shockwave 			max  2 sec	AoE stun (frontal cone, 10 yards)
			[107570] = true,	-- | Storm Bolt			max  4 sec	stun


			-- =================================
			-- | races and CC abilities in TWW |
			-- =================================

			-- | Tauren
			[20549] = true,		-- | War Stomp			max  2 sec	(AoE) stun (8 yards, 1 til max 5 enemies)

			-- | Kul Tiran
			[287712] = true,	-- | Haymaker			max  3 sec	stun

	             }




	local function log_ccbreak(set, srcGUID, srcName)

		local player = Skada:get_player(set, srcGUID, srcName)


		if player then
	
			player.ccbreaks = player.ccbreaks + 1
			set.ccbreaks    = set.ccbreaks    + 1
		end
	end




	local function SpellAuraBroken(timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, ...)

		local spellId, spellName, extraSpellId, extraSpellName


		if eventtype == "SPELL_AURA_BROKEN" then

			spellId, spellName = ...
		else

			spellId, spellName, _, extraSpellId, extraSpellName = ...
		end


		if CCId[spellId] then

			local petid      = srcGUID
			local petname    = srcName


			srcGUID, srcName = Skada:FixMyPets(srcGUID, srcName)


			log_ccbreak(Skada.current, srcGUID, srcName)
			log_ccbreak(Skada.total, srcGUID, srcName)


			local inInstance, instanceType = IsInInstance()


			if Skada.db.profile.modules.ccannounce and IsInRaid() and UnitInRaid(srcName) and not (instanceType == "pvp") then

				if Skada.db.profile.modules.ccignoremaintanks then

					for i = 1, 40 do


						local name, _, _, _, _, class, _, _, _, role, _ = GetRaidRosterInfo(i)


						if name == srcName and role == "maintank" then return end
					end
				end


				if petid ~= srcGUID then srcName = petname .. " (" .. srcName .. ")" end


				if extraSpellName then

					-- ======================================================================
					-- | API   : C_Spell.GetSpellLink					|
					-- | valid : + 11.0.0 / 4.4.1 / 1.15.4					|
					-- |									|
					-- | Returns nil if spell is not found, therefore ...			|
					-- ======================================================================
					local spellLink = C_Spell.GetSpellLink(extraSpellId) or ""


					SendChatMessage(string.format(L["%s on %s removed by %s's %s"], spellName, dstName, srcName, spellLink), "RAID")
				else

					SendChatMessage(string.format(L["%s on %s removed by %s"], spellName, dstName, srcName),"RAID")
				end
			end
		end
	end




	function mod:OnEnable()

		mod.metadata = {showspots = true, icon = "Interface\\Icons\\Spell_magic_polymorphrabbit"}

		Skada:RegisterForCL(SpellAuraBroken, 'SPELL_AURA_BROKEN'      , {src_is_interesting = true})
		Skada:RegisterForCL(SpellAuraBroken, 'SPELL_AURA_BROKEN_SPELL', {src_is_interesting = true})

		Skada:AddMode(self)
	end




	function mod:OnDisable()

		Skada:RemoveMode(self)
	end




	function mod:AddToTooltip(set, tooltip)

		GameTooltip:AddDoubleLine(L["CC breaks"], set.ccbreaks, 1, 1, 1)
	end




	function mod:GetSetSummary(set)

		return set.ccbreaks
	end




	function mod:AddPlayerAttributes(player)

		if not player.ccbreaks then player.ccbreaks = 0	end
	end




	function mod:AddSetAttributes(set)

		if not set.ccbreaks then set.ccbreaks = 0 end
	end




	function mod:Update(win, set)

		local max = 0
		local nr  = 1


		for i, player in ipairs(set.players) do

			if player.ccbreaks > 0 then

				local d         = win.dataset[nr] or {}


				win.dataset[nr] = d


				d.class		= player.class
				d.id		= player.id
				d.label		= player.name

				d.role		= player.role
				d.value		= player.ccbreaks
				d.valuetext	= tostring(player.ccbreaks)


				if player.ccbreaks > max then max = player.ccbreaks end


				nr = nr + 1
			end
		end

		win.metadata.maxvalue = max
	end




	     -- ==============================
	     -- | options: CC.lua	     |
	     -- ==============================
	     local opts =

			{


		      ccoptions =

				{
	
				   type = "group",
				   name = L["CC"],
				  order	= function()

						  if GetLocale() == "deDE" then return 4 end
						  if GetLocale() == "enUS" then return 2 end
					   end,


				   args =

					{

					     ccsettings =

							{

							   type = "description",
							   name = "\124cnNORMAL_FONT_COLOR:" .. L["Options: cc (crowd control)"] .. emptyLines,
							  order = 0,

							},

					       announce =

							{

							   type = "toggle",
							   name = L["Announce CC breaking to party"],
							    get = function()

									  return Skada.db.profile.modules.ccannounce
								  end,
							    set = function()

									  Skada.db.profile.modules.ccannounce = not Skada.db.profile.modules.ccannounce
								  end,
							  order = 1,

							},

					ignoremaintanks =

							{

							   type = "toggle",
							   name = L["Ignore Main Tanks"],
							    get = function()

									  return Skada.db.profile.modules.ccignoremaintanks
								  end,
							    set = function()

									  Skada.db.profile.modules.ccignoremaintanks = not Skada.db.profile.modules.ccignoremaintanks
								  end,
							  order = 2,

							},
					},

				}

			}




	function mod:OnInitialize()

		table.insert(Skada.options.plugins, opts)
	end


end)
-- =======================
-- | outer function: end |
-- =======================