local TraceFrame = DEFAULT_CHAT_FRAME;

local DBCS_mobs_spell_wow_build;
local DBCS_mobs_spell_wow_build_live = 18414;	--14007;	-- patch 4.1.0

_, DBCS_mobs_spell_wow_build, _, _ = GetBuildInfo();
DBCS_mobs_spell_wow_build = tonumber(DBCS_mobs_spell_wow_build);

function DBCS_mobs_spell_OnLoad(self)

	self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
	self:RegisterEvent("VARIABLES_LOADED");

	--DBCS_mobs_spell = {};

end

function DBCS_mobs_spell_Liste_click()

local i;
local j;
local k;

	if ( DBMS and DBCORPS_MOB_CRITERES and DBCORPS_MOB_CRITERES.nommob and DBCORPS_MOB_CRITERES.numregion ) then
		if ( DBMS[DBCORPS_MOB_CRITERES.nommob] and DBMS[DBCORPS_MOB_CRITERES.nommob][DBCORPS_MOB_CRITERES.numregion] and DBMS[DBCORPS_MOB_CRITERES.nommob][DBCORPS_MOB_CRITERES.numregion][29] ) then
			i = DBMS[DBCORPS_MOB_CRITERES.nommob][DBCORPS_MOB_CRITERES.numregion][29];
			if ( DBCS_mobs_spell[i] ) then
				k = 0;
				for j, _ in pairs(DBCS_mobs_spell[i]) do
					k = k + 1;
					TraceFrame:AddMessage(DBCORPS_MOB_CRITERES.nommob.." connait comme sort num\195\169ro "..k.." : "..CMS_transformer_liens_en_liens_ds_langue(CMS_GetItemLink("@@blabla@-3@"..j.."@@"),"1"));
				end
			end
		end
	end
end

function DBCS_mobs_spell_OnEvent(self,event,...)

local ipos;
local sarg2_prefix;
local snom;
local sGUID_1_2;
local sGUID_3;
local sGUID_7_10;
local iGUID_7_10;

local arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13,arg14,arg15,arg16,arg17,arg18,arg19,arg20 = ...;

local val3,val4,val9;

	if ( event == "" ) then
	elseif ( event == "COMBAT_LOG_EVENT_UNFILTERED" ) then
--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13,arg14,arg15,arg16,arg17,arg18,arg19,arg20);
--end
		ipos = strfind(arg2,"_",1);
		if ( ipos ) then
			sarg2_prefix = strsub(arg2,1,ipos-1);
			if ( sarg2_prefix == "RANGE" or sarg2_prefix == "SPELL" ) then
				--if ( DBCS_mobs_spell_wow_build > DBCS_mobs_spell_wow_build_live ) then
					val3 = arg4;
					val4 = arg5;
					val9 = arg12;		-- 12 pour la 4.2
				--else
				--	val3 = arg4;
				--	val4 = arg5;
				--	val9 = arg10;		-- 12 pour la 4.2
				--end
				if ( val9 ) then
					snom = val4;
					if ( snom == nil ) then
						snom = val3;
					end
					if ( snom ) then
						if ( DBCS_mobs_spell_wow_build > DBCS_mobs_spell_wow_build_live ) then
							if ( val3 and strsub(val3,1,8) == "Creature" ) then
								if ( val9 and strsub(val9,1,6) == "Player" ) then
if ( DBCS_debug > 1 ) then
DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13,arg14,arg15,arg16,arg17,arg18,arg19,arg20);
end
								else
									_,_,_,_,_,sGUID_7_10,_ = strsplit("-",val3);
									iGUID_7_10 = tonumber(sGUID_7_10);
									idate = tonumber(DBCS_date());
									DBCS_mobs_spell_enreg(true,true,iGUID_7_10,val9,idate);
								end
							end
						else
							if ( val3 and strsub(val3,1,2) == "0x" ) then
								sGUID_1_2 = strsub(val3,3,4);
								if ( sGUID_1_2 == "00" or sGUID_1_2 == "01" ) then
								else
									sGUID_3 = strsub(val3,5,5);
									if ( sGUID_3 == "3" ) then
										sGUID_7_10 = strsub(val3,6,10);
										iGUID_7_10 = tonumber(sGUID_7_10,16);
--if ( DBCS_debug > 1 ) then
--TraceFrame:AddMessage(snom.." = "..sGUID_7_10.." = "..iGUID_7_10);
--end
										idate = tonumber(DBCS_date());
										DBCS_mobs_spell_enreg(true,true,iGUID_7_10,val9,idate);
									end
								end
							end
						end
--if ( DBCS_debug > 1 ) then
--TraceFrame:AddMessage(DBCS_lienjoueurbynom(snom).." connait "..CMS_transformer_liens_en_liens_ds_langue(CMS_GetItemLink("@@blabla@-3@"..val9.."@@"),"1"));
--end
					else
--if ( DBCS_debug > 1 ) then
--TraceFrame:AddMessage("inconnu avait "..CMS_transformer_liens_en_liens_ds_langue(CMS_GetItemLink("@@blabla@-3@"..val9.."@@"),"1"));
--end
					end
				end
			end
		end
	elseif ( event == "VARIABLES_LOADED" ) then
		if ( DBCS_data_Mobs_model_LOADED and DBCS_data_Mobs_model_LOADED == true ) then
		else
			DBCS_copie_mobs_spell_sauvegarde(false);
		end
	end

end

function DBCS_mobs_spell_enreg(bcms,bsave,iGUID,iSPELL,idate,bvisible)

local bmaj;
local smessage;
local smot_milieu;
local smot_tout;
local slink;

	if ( idate < 20101125 ) then
		return nil;
	end
	if ( bvisible == nil ) then
		bvisible = true;
	end
	--snumregion = DBCS_numregionbyname(GetRealZoneText());
	--fDBmobs_Local_ajouter(iGUID_7_10,sUnitName,snumregion,idate,true);
	bmaj = true;
	if ( DBCS_mobs_spell[iGUID] == nil ) then
		DBCS_mobs_spell[iGUID] = {};
	end
	if ( DBCS_mobs_spell[iGUID][iSPELL] == nil ) then
		DBCS_mobs_spell[iGUID][iSPELL] = idate;
if ( bvisible == true ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
		if ( DBCS_mobs_spell_wow_build > DBCS_mobs_spell_wow_build_live ) then
			if ( DBmobs_Local and DBmobs_Local[iGUID] and DBmobs_Local[iGUID][1] and DBmobs_Local[iGUID][1][1] ) then
				smot_tout = "unit:Creature-0-0-0-0-"..iGUID.."-0000000000:"..DBmobs_Local[iGUID][1][1];
				slink = "|cff4e96f7|H"..smot_tout.."|h["..DBmobs_Local[iGUID][1][1].."]|h|r";
			else
				smot_tout = "unit:Creature-0-0-0-0-"..iGUID.."-0000000000:Inconnu";
				slink = "|cff4e96f7|H"..smot_tout.."|h[Inconnu]|h|r";
			end
		else
			smot_milieu = format("%X",iGUID);
			if ( DBmobs_Local and DBmobs_Local[iGUID] and DBmobs_Local[iGUID][1] and DBmobs_Local[iGUID][1][1] ) then
				smot_tout = "unit:0xF53"..strrep("0",5-strlen(smot_milieu))..smot_milieu.."00000000:"..DBmobs_Local[iGUID][1][1];
				slink = "|cff4e96f7|H"..smot_tout.."|h["..DBmobs_Local[iGUID][1][1].."]|h|r";
			else
				smot_tout = "unit:0xF53"..strrep("0",5-strlen(smot_milieu))..smot_milieu.."00000000:Inconnu";
				slink = "|cff4e96f7|H"..smot_tout.."|h[Inconnu]|h|r";
			end
		end
TraceFrame:AddMessage("j'ajoutes le sort "..iSPELL.." : "..CMS_transformer_liens_en_liens_ds_langue(CMS_GetItemLink("@@blabla@-3@"..iSPELL.."@@"),"1").." , pour le mob "..iGUID.." : "..slink);
end
end
	else
		if ( idate > DBCS_mobs_spell[iGUID][iSPELL] ) then
			DBCS_mobs_spell[iGUID][iSPELL] = idate;
		else
			bmaj = false;
		end
	end
	if ( bmaj == true ) then
		if ( bsave == true ) then
			if ( DBCS_prog_serveur == 0 ) then
				if ( DBCS_mobs_spell_Client[iGUID] == nil ) then
					DBCS_mobs_spell_Client[iGUID] = {};
				end
				DBCS_mobs_spell_Client[iGUID][iSPELL] = idate;
			end
		end
		if ( bcms == true ) then
			if ( DBCS_CMS_numcanalCMS and DBCS_CMS_numcanalCMS > 0 ) then
				smessage = "";
				smessage = smessage.."@ADBCS@";
				smessage = smessage.."ZZZZ@";
				smessage = smessage.."MS01@";
				smessage = smessage..iGUID.."@";
				smessage = smessage..iSPELL.."@";
				smessage = smessage..idate.."@";
--TraceFrame:AddMessage("smessage = "..smessage);
				DBCS_SendChatMessage(smessage,"CHANNEL",DBCS_language,DBCS_CMS_numcanalCMS);
			end
		end
	end

end
