local TraceFrame = DEFAULT_CHAT_FRAME;

-- version 01.00.06 du 20091210
	-- maj du .toc pour wow 3.3

-- version 01.00.05 du 20090831
	-- maj du .toc pour wow 3.2

-- version 01.00.04 du 20090501
	-- maj du .toc pour wow 3.1

-- version 01.00.03
	-- maj du .toc

-- version 1.00.02
	-- ajout de la fonction : SendChatMessageTimer_Vide pour demander si l'addon a des messages à envoyer, pour lui communiquer des données à envoyer pas urgentes

--/script SendChatMessageTimer_Refresh(36000)
local SendChatMessageTimer_time = 0;
--local SendChatMessageTimer_refresh = 0.25 -- = une fois tous les quarts de secondes
--local SendChatMessageTimer_refresh = 0.3 -- = une fois tous les 3 dixièmes de secondes
--local SendChatMessageTimer_refresh = 0.4 -- = une fois tous les 4 dixièmes de secondes
--local SendChatMessageTimer_refresh = 0.5 -- = une fois tous les 5 dixièmes de secondes
--local SendChatMessageTimer_refresh = 0.6 -- = une fois tous les 5 dixièmes de secondes
local SendChatMessageTimer_refresh = 1 -- = une fois tous les 5 dixièmes de secondes
--local SendChatMessageTimer_refresh = 36000 -- = une fois toutes les 10 heures
local SendChatMessageTimer_tablo = {};
local SendChatMessageTimer_indice = 0;

--SendChatMessage(smessage,"CHANNEL",this.language,DBCS_CMS_numcanalCMS);
--"SAY" 
--"EMOTE" 
--"YELL" 
--"PARTY" 
--"GUILD" 
--"OFFICER" 
--"RAID" 
--"RAID_WARNING" 
--"BATTLEGROUND" 
--"WHISPER" 
--"CHANNEL" 
--"AFK"
--"DND"
SendChatMessageTimer_Saved_SendChatMessage = SendChatMessage;
function SendChatMessage(smessage,smode,slangue,sdestinataire)

--TraceFrame:AddMessage("appel SendChatMessage");
	if ( smode == "CHANNEL" or smode == "GUILD" or smode == "OFFICER" ) then
		SendChatMessageTimer_indice = SendChatMessageTimer_indice + 1;
		SendChatMessageTimer_tablo[SendChatMessageTimer_indice] = {};
		SendChatMessageTimer_tablo[SendChatMessageTimer_indice][1] = smessage;
		SendChatMessageTimer_tablo[SendChatMessageTimer_indice][2] = smode;
		SendChatMessageTimer_tablo[SendChatMessageTimer_indice][3] = slangue;
		SendChatMessageTimer_tablo[SendChatMessageTimer_indice][4] = sdestinataire;
	else
		SendChatMessageTimer_Saved_SendChatMessage(smessage,smode,slangue,sdestinataire);
	end

end
--SendChatMessage = SendChatMessageTimer_SendChatMessage;

local SendChatMessageTimer_tablo_lent = {};
local SendChatMessageTimer_indice_lent = 0;

function SendChatMessageTimer_SendChatMessage_lent(val1,val2,val3,val4)

	SendChatMessageTimer_indice_lent = SendChatMessageTimer_indice_lent + 1;
	SendChatMessageTimer_tablo_lent[SendChatMessageTimer_indice_lent] = {};
	SendChatMessageTimer_tablo_lent[SendChatMessageTimer_indice_lent][1] = val1;
	SendChatMessageTimer_tablo_lent[SendChatMessageTimer_indice_lent][2] = val2;
	SendChatMessageTimer_tablo_lent[SendChatMessageTimer_indice_lent][3] = val3;
	SendChatMessageTimer_tablo_lent[SendChatMessageTimer_indice_lent][4] = val4;

end

function SendChatMessageTimer_OnLoad()

	SendChatMessageTimer = {};

end

local SendChatMessageTimer_saved_errorhandler = nil;

function SendChatMessageTimer_errorhandler()

local stexte;

	--SendChatMessageTimer_Saved_SendChatMessage(SendChatMessageTimer_tablo[1][1],SendChatMessageTimer_tablo[1][2],SendChatMessageTimer_tablo[1][3],SendChatMessageTimer_tablo[1][4]);
	seterrorhandler(SendChatMessageTimer_saved_errorhandler);
	stexte = SendChatMessageTimer_tablo[1][1];
	table.remove(SendChatMessageTimer_tablo,1);
	SendChatMessageTimer_indice = SendChatMessageTimer_indice - 1;
	SendChatMessageTimer_time = GetTime();
if ( DBCS_prog_serveur and DBCS_prog_serveur == 1 ) then
	message("ligne erreur SendChatMessageTimer_errorhandler : "..(stexte or ""));
	SendChatMessageTimer_ecrirecodedutexte(stexte);
end
			
end

local SendChatMessageTimer_saved_errorhandler_lent = nil;

function SendChatMessageTimer_errorhandler_lent()

local stexte;

	--SendChatMessageTimer_Saved_SendChatMessage(SendChatMessageTimer_tablo[1][1],SendChatMessageTimer_tablo[1][2],SendChatMessageTimer_tablo[1][3],SendChatMessageTimer_tablo[1][4]);
	seterrorhandler(SendChatMessageTimer_saved_errorhandler_lent);
	stexte = SendChatMessageTimer_tablo_lent[1][1];
	table.remove(SendChatMessageTimer_tablo_lent,1);
	SendChatMessageTimer_indice_lent = SendChatMessageTimer_indice_lent - 1;
	SendChatMessageTimer_time = GetTime();
if ( DBCS_prog_serveur and DBCS_prog_serveur == 1 ) then
	message("ligne erreur SendChatMessageTimer_errorhandler_lent : "..(stexte or ""));
	SendChatMessageTimer_ecrirecodedutexte(stexte);
end
			
end

function SendChatMessageTimer_Update()

	if ( SendChatMessageTimer_indice == 0 and SendChatMessageTimer_indice_lent == 0 ) then
		return;
	end

local itime;

	itime = GetTime();
	if ( itime - SendChatMessageTimer_time > SendChatMessageTimer_refresh ) then
		if ( SendChatMessageTimer_indice > 0 ) then
			--SendChatMessageTimer_ecrirecodedutexte(SendChatMessageTimer_tablo[1][1]);
			SendChatMessageTimer_saved_errorhandler = geterrorhandler();
			seterrorhandler(SendChatMessageTimer_errorhandler);
			SendChatMessageTimer_Saved_SendChatMessage(SendChatMessageTimer_tablo[1][1],SendChatMessageTimer_tablo[1][2],SendChatMessageTimer_tablo[1][3],SendChatMessageTimer_tablo[1][4]);
			seterrorhandler(SendChatMessageTimer_saved_errorhandler);
			table.remove(SendChatMessageTimer_tablo,1);
			SendChatMessageTimer_indice = SendChatMessageTimer_indice - 1;
		elseif ( SendChatMessageTimer_indice_lent > 0 ) then
			--SendChatMessageTimer_ecrirecodedutexte(SendChatMessageTimer_tablo_lent[1][1]);
			SendChatMessageTimer_saved_errorhandler_lent = geterrorhandler();
			seterrorhandler(SendChatMessageTimer_errorhandler_lent);
			SendChatMessageTimer_Saved_SendChatMessage(SendChatMessageTimer_tablo_lent[1][1],SendChatMessageTimer_tablo_lent[1][2],SendChatMessageTimer_tablo_lent[1][3],SendChatMessageTimer_tablo_lent[1][4]);
			seterrorhandler(SendChatMessageTimer_saved_errorhandler_lent);
			table.remove(SendChatMessageTimer_tablo_lent,1);
			SendChatMessageTimer_indice_lent = SendChatMessageTimer_indice_lent - 1;
		end
		--Saved_SendChatMessage("test : "..SendChatMessageTimer_time,"CHANNEL",this.language,6);
		SendChatMessageTimer_time = GetTime();
	end

end

function SendChatMessageTimer_Afficher_indice()

DEFAULT_CHAT_FRAME:AddMessage("SendChatMessageTimer_indice = "..SendChatMessageTimer_indice);

end

--/script SendChatMessageTimer_Refresh(0.1)
--/script SendChatMessageTimer_Refresh(0.2)
--/script SendChatMessageTimer_Refresh(0.21)
--/script SendChatMessageTimer_Refresh(0.22)
--/script SendChatMessageTimer_Refresh(0.23)
--/script SendChatMessageTimer_Refresh(0.24)
--/script SendChatMessageTimer_Refresh(0.25)
--/script SendChatMessageTimer_Refresh(0.26)
--/script SendChatMessageTimer_Refresh(0.27)
--/script SendChatMessageTimer_Refresh(0.28)
--/script SendChatMessageTimer_Refresh(0.29)
--/script SendChatMessageTimer_Refresh(0.3)
--/script SendChatMessageTimer_Refresh(0.4)
--/script SendChatMessageTimer_Refresh(0.5)
--/script SendChatMessageTimer_Refresh(0.6)
--/script SendChatMessageTimer_Refresh(0.7)
--/script SendChatMessageTimer_Refresh(0.8)
--/script SendChatMessageTimer_Refresh(0.9)
--/script SendChatMessageTimer_Refresh(1)
--/script SendChatMessageTimer_Refresh(36000)

-- change le délai entre chaque envoi de message, si blizzard change sa valeur ( en seconde )
function SendChatMessageTimer_Refresh(iseconde)

	SendChatMessageTimer_refresh = iseconde;

end

function SendChatMessageTimer_Vide()

	if ( SendChatMessageTimer_indice == 0 and SendChatMessageTimer_indice_lent == 0 ) then
		return true;
	else
		return false;
	end

end

function SendChatMessageTimer_ecrirecodedutexte(stexte,ideb,ifin)

local ilen;
local i;
local icarac;
local scarac;

DEFAULT_CHAT_FRAME:AddMessage("texte = "..stexte);
	ilen = strlen(stexte);
	if ( ideb ) then
	else
		ideb = 1;
	end
	if ( ifin ) then
	else
		ifin = ilen;
	end
DEFAULT_CHAT_FRAME:AddMessage("ilen = "..ilen);
	for i = 1 , ilen do
		if ( i >= ideb and i <= ifin ) then
			icarac = strbyte(stexte,i,i);
			if ( icarac ) then
				if ( icarac > 31 and icarac < 127 ) then
DEFAULT_CHAT_FRAME:AddMessage(i.." : "..icarac.." = -"..strsub(stexte,i,i).."-.");
				else
DEFAULT_CHAT_FRAME:AddMessage(i.." : "..icarac.." = -"..icarac.."-.");
				end
			else
DEFAULT_CHAT_FRAME:AddMessage(i.." = -erreur-.");
			end
		end
	end

end
