local TraceFrame = DEFAULT_CHAT_FRAME;

DBCS_mobs_model_execute = true;

--/script DBCS_mobs_model_SetItemRef_nummob_i = -1;
--/script DBCS_mobs_model_SetItemRef_nummob_i = 90000;
local DBCS_mobs_model_SetItemRef_nummob_i = -1;
--DBCS_mobs_model_SetItemRef_nummob_i = 200000;
local DBCS_mobs_model_SetItemRef_nummob_imax = 70000;	--65000;	--60000;	--55000;	--50000;	--45000;

local DBCS_mobs_model_SetItemRef_nummob_phase = 1;
local DBCS_mobs_model_SetItemRef_nummob_phase_max = 2;

local DBCS_mobs_model_SetItemRef_nummob_time = 0;
local DBCS_mobs_model_SetItemRef_nummob_time_attente = 5;
--local DBCS_mobs_model_SetItemRef_nummob_time_attente = .5;
--local DBCS_mobs_model_SetItemRef_nummob_time_attente = .1;	-- fait crasher
local DBCS_mobs_model_SetItemRef_nummob_time_battente = false;
local DBCS_mobs_model_SetItemRef_nummob_time_model = "";
local DBCS_mobs_model_SetItemRef_nummob_time_nom = nil;
local DBCS_mobs_model_SetItemRef_nummob_time_b2test = false;
local DBCS_mobs_model_SetItemRef_nummob_time_link = "";

local DBCS_mobs_model_SetItemRef_nummob_x = 0;
local DBCS_mobs_model_SetItemRef_nummob_y = 0;

local DBCS_mobs_model_wow_build;
local DBCS_mobs_model_wow_build_live = 13623;
--
_, DBCS_mobs_model_wow_build, _, _ = GetBuildInfo();
DBCS_mobs_model_wow_build = tonumber(DBCS_mobs_model_wow_build);

function DBCS_mobs_model_OnLoad(self)

	self:RegisterEvent("VARIABLES_LOADED");

	--DBCS_mobs_model = {};
	DBCS_mobs_model_config = {};

end

function DBCS_mobs_model_OnEvent(self,event)

	if ( event == "" ) then
	elseif ( event == "VARIABLES_LOADED" ) then
		if ( DBCS_mobs_model_config["indice"] ) then
			DBCS_mobs_model_SetItemRef_nummob_i = DBCS_mobs_model_config["indice"] - 1;
		end
		if ( DBCS_mobs_model_config["phase"] ) then
			DBCS_mobs_model_SetItemRef_nummob_phase = DBCS_mobs_model_config["phase"];
		end
		if ( DBOT_linkable_execute == nil ) then
		else
			if ( DBCS_mobs_model_config["execute"] ) then
				DBCS_mobs_model_execute = DBCS_mobs_model_config["execute"];
			end
		end
		if ( DBCS_data_Mobs_model_LOADED and DBCS_data_Mobs_model_LOADED == true ) then
		else
			DBCS_copie_mobs_model_client(false);
		end
	end
	
end

function DBCS_mobs_model_OnUpdate()

	--if ( 1 == 1 ) then
	--	return nil;
	--end

	if ( DBCS_mobs_model_execute == true ) then
		DBCS_mobs_model_SetItemRef_nummob(false);
	end

end

--/script DBCS_mobs_model_SetItemRef_nummob();
function DBCS_mobs_model_SetItemRef_nummob(baffiche)

--local smot_debut = "unit:0x003";
--local smot_debut = "unit:0xF13";
local smot_debut = "";
--local smot_debut = "unit:0xF55";
local smot_fin;
local smot_milieu = nil;
local smot_tout = nil;
local ilen = nil;
local snom = nil;
--local slink = nil;
--local sregion = nil;
--local snumregion = nil;
--local i = nil;
--local j = nil;
--local b2test = false;
--local smodel = "";
local idate_jour = nil;
--local ttable = nil;
local smessage;
local bajout;
local x;
local y;

--if ( DBCS_mobs_model_wow_build > DBCS_mobs_model_wow_build_live ) then
	smot_fin = "00000000:";
--else
--	smot_fin = "000000:";
--end

	if ( baffiche ) then
		if ( baffiche == true ) then
		else
			baffiche = false;
		end
	else
		baffiche = false;
	end
	
	if ( DBCS_mobs_model_SetItemRef_nummob_time == 0 ) then
		DBCS_mobs_model_SetItemRef_nummob_time = GetTime();
	end

	idate_jour = tonumber(date("%Y%m%d"));

	if ( DBCS_mobs_model_SetItemRef_nummob_time_battente == false ) then

		if ( DBCS_mobs_model_SetItemRef_nummob_i > DBCS_mobs_model_SetItemRef_nummob_imax ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("je commences une nouvelle boucle de DBCS_mobs_model");
end
			DBCS_mobs_model_SetItemRef_nummob_i = -1;
			DBCS_mobs_model_config["indice"] = DBCS_mobs_model_SetItemRef_nummob_i;
			DBCS_mobs_model_SetItemRef_nummob_phase = DBCS_mobs_model_SetItemRef_nummob_phase + 1;
			if ( DBCS_mobs_model_SetItemRef_nummob_phase > DBCS_mobs_model_SetItemRef_nummob_phase_max ) then
				DBCS_mobs_model_SetItemRef_nummob_phase = 1;
			end
			DBCS_mobs_model_config["phase"] = DBCS_mobs_model_SetItemRef_nummob_phase;
			--return nil;
		end
		DBCS_mobs_model_SetItemRef_nummob_i = DBCS_mobs_model_SetItemRef_nummob_i + 1;
		DBCS_mobs_model_config["indice"] = DBCS_mobs_model_SetItemRef_nummob_i;

	end

	if ( DBCS_mobs_model_SetItemRef_nummob_phase == 1 ) then
		smot_debut = "unit:0xF13";
	elseif ( DBCS_mobs_model_SetItemRef_nummob_phase == 2 ) then
		smot_debut = "unit:0xF53";
	end
	smot_milieu = format("%X",DBCS_mobs_model_SetItemRef_nummob_i);
	ilen = strlen(smot_milieu);
	if ( DBmobs_Local and DBmobs_Local[DBCS_mobs_model_SetItemRef_nummob_i] ) then
		smot_tout = smot_debut..strrep("0",5-ilen)..smot_milieu..smot_fin..DBmobs_Local[DBCS_mobs_model_SetItemRef_nummob_i][1][1];
	else
		smot_tout = smot_debut..strrep("0",5-ilen)..smot_milieu..smot_fin.."bla";
	end
	DBCS_mobs_model_Frame_TooltipTextLeft1:SetText(nil);
	DBCS_mobs_model_Frame_Tooltip:SetOwner(WorldFrame,"ANCHOR_NONE");
	DBCS_mobs_model_Frame_Tooltip:SetHyperlink(smot_tout);
	snom = DBCS_mobs_model_Frame_TooltipTextLeft1:GetText();
	DBCS_mobs_model_SetItemRef_nummob_time_nom = snom;
	if ( snom ) then
		DBCS_mobs_model_SetItemRef_nummob_time_battente = true;
		smot_tout = smot_debut..strrep("0",5-ilen)..smot_milieu..smot_fin..snom;
		DBCS_mobs_model_SetItemRef_nummob_time_link = "|cff4e96f7|H"..smot_tout.."|h["..snom.."]|h|r";
	end
--if ( DBCS_debug and DBCS_debug > 1 ) then
--TraceFrame:AddMessage(DBCS_mobs_model_SetItemRef_nummob_i);
--end

--/script DBCS_mobs_model_PlayerFrameModel_cache:Show();
--/script DBCS_mobs_model_PlayerFrameModel_cache:Hide();
--/script DBCS_mobs_model_PlayerFrameModel_cache:SetWidth(100);
--/script DBCS_mobs_model_PlayerFrameModel_cache:SetHeight(100);
	if ( DBCS_mobs_model_SetItemRef_nummob_time_battente == true ) then

		if ( ( GetTime() - DBCS_mobs_model_SetItemRef_nummob_time ) > DBCS_mobs_model_SetItemRef_nummob_time_attente ) then
			x, y = GetPlayerMapPosition("player");
			if ( DBCS_mobs_model_SetItemRef_nummob_x == x and DBCS_mobs_model_SetItemRef_nummob_y == y ) then
--if ( DBCS_debug and DBCS_debug > 1 ) then
--TraceFrame:AddMessage("DBCS_mobs_model_SetItemRef_nummob : je ne bouges pas, je test.");
--end
				DBCS_mobs_model_SetItemRef_nummob_time_b2test = false;
				if ( DBCS_mobs_model_SetItemRef_nummob_time_b2test == false ) then
					DBCS_mobs_model_PlayerFrameModel_cache:SetCreature(DBCS_mobs_model_SetItemRef_nummob_i);
					DBCS_mobs_model_SetItemRef_nummob_time_model = DBCS_mobs_model_PlayerFrameModel_cache:GetModel();
					if ( type(DBCS_mobs_model_SetItemRef_nummob_time_model) == "string" ) then
						DBCS_mobs_model_SetItemRef_nummob_time_model, _ = string.gsub(DBCS_mobs_model_SetItemRef_nummob_time_model,"\\","/");
						DBCS_mobs_model_SetItemRef_nummob_time_b2test = true;
					end
				end
				if ( DBCS_mobs_model_SetItemRef_nummob_time_b2test == true and DBCS_mobs_model[DBCS_mobs_model_SetItemRef_nummob_i] and DBCS_mobs_model[DBCS_mobs_model_SetItemRef_nummob_i][1] and DBCS_mobs_model[DBCS_mobs_model_SetItemRef_nummob_i][1]["model"] == DBCS_mobs_model_SetItemRef_nummob_time_model ) then	--	and DBCS_mobs_model[DBCS_mobs_model_SetItemRef_nummob_i][1]["datemodel"] > idate_jour-7
					DBCS_mobs_model_SetItemRef_nummob_time_b2test = false;
				end
				if ( DBCS_mobs_model_SetItemRef_nummob_time_b2test == true ) then
					bajout = DBCS_mobs_model_ajout(DBCS_mobs_model_SetItemRef_nummob_i,DBCS_mobs_model_SetItemRef_nummob_time_model,idate_jour,DBCS_mobs_model_SetItemRef_nummob_time_link);
					if ( bajout == true ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage(DBCS_mobs_model_SetItemRef_nummob_i.." = "..DBCS_mobs_model_SetItemRef_nummob_time_model.." : "..DBCS_mobs_model_SetItemRef_nummob_time_link);
end
					end
					if ( DBCS_Mobs_Model_Client ) then
						if ( bajout == true ) then
							if ( DBCS_CMS_numcanalCMS and DBCS_CMS_numcanalCMS > 0 ) then
								if ( DBCS_mobs_model_SetItemRef_nummob_time_nom ) then
									fDBmobs_Local_ajouter(DBCS_mobs_model_SetItemRef_nummob_i,DBCS_mobs_model_SetItemRef_nummob_time_nom,DBCS_numregionbyname(GetRealZoneText()),idate_jour,true);
								end
								smessage = "";
								smessage = smessage.."@ADBCS@";
								smessage = smessage.."ZZzz@";
								smessage = smessage.."MM01@";
								smessage = smessage..DBCS_mobs_model_SetItemRef_nummob_i.."@";
								smessage = smessage..DBCS_mobs_model_SetItemRef_nummob_time_model.."@";
								smessage = smessage..idate_jour.."@";
								DBCS_SendChatMessage(smessage,"CHANNEL",DBCS_language,DBCS_CMS_numcanalCMS);
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("message cms depuis DBCS_mobs_model : "..smessage);
end
							end
						end
					end
				end
				DBCS_mobs_model_SetItemRef_nummob_time_battente = false;
			else
--if ( DBCS_debug and DBCS_debug > 1 ) then
--TraceFrame:AddMessage("DBCS_mobs_model_SetItemRef_nummob : je bouges, je ne test pas.");
--end
			end
			DBCS_mobs_model_SetItemRef_nummob_time = GetTime();
			DBCS_mobs_model_SetItemRef_nummob_x = x;
			DBCS_mobs_model_SetItemRef_nummob_y = y;
		end

	end

end

--/script DBCS_mobs_model_execute_ON()
function DBCS_mobs_model_execute_ON()

	DBCS_mobs_model_execute = true;
	DBCS_mobs_model_config["execute"] = DBCS_mobs_model_execute;
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("j'enclenches DBCS_mobs_model.");
end

end

--/script DBCS_mobs_model_execute_OFF()
function DBCS_mobs_model_execute_OFF()

	DBCS_mobs_model_execute = false;
	DBCS_mobs_model_config["execute"] = DBCS_mobs_model_execute;
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("je coupes DBCS_mobs_model.");
end

end

--/script DBCS_mobs_model_afficher_num()
function DBCS_mobs_model_afficher_num()

	TraceFrame:AddMessage("DBCS_mobs_model : DBCS_mobs_model_SetItemRef_nummob_i = "..DBCS_mobs_model_SetItemRef_nummob_i);

end

function DBCS_mobs_model_ajout(inummob,smodele,idate,slink)

local i;
local j;
local ttable;
local breturn = true;

	if ( DBCS_mobs_model[inummob] == nil ) then
		DBCS_mobs_model[inummob] = {};
	end
	i = 0;
	for j = 1 , table.getn(DBCS_mobs_model[inummob]) do
		if ( DBCS_mobs_model[inummob][j] == nil or ( not ( type(DBCS_mobs_model[inummob][j]) == "table" ) ) ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_ajout : "..inummob.." = "..smodele.." : "..slink.."sur elem "..j.." sur "..table.getn(DBCS_mobs_model[inummob]).." elems.");
end
		end
		if ( i == 0 and DBCS_mobs_model[inummob][j] and DBCS_mobs_model[inummob][j]["model"] and DBCS_mobs_model[inummob][j]["model"] == smodele ) then
			i = j;
		end
	end
	if ( i > 0 ) then
		if ( DBCS_mobs_model[inummob][i]["datemodel"] and DBCS_mobs_model[inummob][i]["datemodel"] > idate ) then
		else
			ttable = DBCS_mobs_model[inummob][i];
			tremove(DBCS_mobs_model[inummob],i);
			tinsert(DBCS_mobs_model[inummob],1,ttable);
			DBCS_mobs_model[inummob][1]["datemodel"] = idate;
		end
		breturn = false;
	else
		ttable = {};
		tinsert(DBCS_mobs_model[inummob],1,ttable);
		--DBCS_mobs_model[inummob][1] = {};
		DBCS_mobs_model[inummob][1]["model"] = smodele;
		DBCS_mobs_model[inummob][1]["datemodel"] = idate;
	end
	if ( DBCS_mobs_model[inummob][1] == nil ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_ajout : "..inummob.." = "..smodele.." : "..slink);
end
	end
	if ( breturn == true and DBCS_Mobs_Model_Client and DBCS_prog_serveur == 0 ) then
		if ( DBCS_Mobs_Model_Client[inummob] == nil ) then
			DBCS_Mobs_Model_Client[inummob] = {};
		end
		DBCS_Mobs_Model_Client[inummob][smodele] = idate;
	end

	return breturn;

end

function DBCS_mobs_model_blp_ajout(inummob,smodele,sblp,idate,slink)

local i;
local j;
local ttable;
local breturn = true;

	if ( DBCS_mobs_model[inummob] == nil ) then
		DBCS_mobs_model[inummob] = {};
	end
	i = 0;
	for j = 1 , table.getn(DBCS_mobs_model[inummob]) do
		if ( DBCS_mobs_model[inummob][j] == nil or ( not ( type(DBCS_mobs_model[inummob][j]) == "table" ) ) ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_blp_ajout : "..inummob.." = "..smodele.." : "..slink.."sur elem "..j.." sur "..table.getn(DBCS_mobs_model[inummob]).." elems.");
end
		end
		if ( i == 0 and DBCS_mobs_model[inummob][j] and DBCS_mobs_model[inummob][j]["model"] and DBCS_mobs_model[inummob][j]["model"] == smodele ) then
			i = j;
		end
	end
	if ( i > 0 ) then
		if ( DBCS_mobs_model[inummob][i]["datemodel"] and DBCS_mobs_model[inummob][i]["datemodel"] > idate ) then
		else
			ttable = DBCS_mobs_model[inummob][i];
			tremove(DBCS_mobs_model[inummob],i);
			tinsert(DBCS_mobs_model[inummob],1,ttable);
			DBCS_mobs_model[inummob][1]["datemodel"] = idate;
			DBCS_mobs_model[inummob][1]["blp"] = sblp;
		end
		breturn = false;
	else
		ttable = {};
		tinsert(DBCS_mobs_model[inummob],1,ttable);
		--DBCS_mobs_model[inummob][1] = {};
		DBCS_mobs_model[inummob][1]["model"] = smodele;
		DBCS_mobs_model[inummob][1]["blp"] = sblp;
		DBCS_mobs_model[inummob][1]["datemodel"] = idate;
	end
	if ( DBCS_mobs_model[inummob][1] == nil ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_blp_ajout : "..inummob.." = "..smodele.." : "..slink);
end
	end
	if ( breturn == true and DBCS_Mobs_Model_Client ) then
		if ( DBCS_Mobs_Model_Client[inummob] == nil ) then
			DBCS_Mobs_Model_Client[inummob] = {};
		end
		DBCS_Mobs_Model_Client[inummob][smodele] = idate;
	end

	return breturn;

end

function DBCS_mobs_model_display_ajout(inummob,smodele,sblp,idisplay,idate,slink)

local i;
local j;
local ttable;
local breturn = true;

	if ( DBCS_mobs_model[inummob] == nil ) then
		DBCS_mobs_model[inummob] = {};
	end
	i = 0;
	for j = 1 , table.getn(DBCS_mobs_model[inummob]) do
		if ( DBCS_mobs_model[inummob][j] == nil or ( not ( type(DBCS_mobs_model[inummob][j]) == "table" ) ) ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_blp_ajout : "..inummob.." = "..smodele.." : "..slink.."sur elem "..j.." sur "..table.getn(DBCS_mobs_model[inummob]).." elems.");
end
		end
		if ( i == 0 and DBCS_mobs_model[inummob][j] and DBCS_mobs_model[inummob][j]["model"] and DBCS_mobs_model[inummob][j]["model"] == smodele ) then
			i = j;
		end
	end
	if ( i > 0 ) then
		if ( DBCS_mobs_model[inummob][i]["datemodel"] and DBCS_mobs_model[inummob][i]["datemodel"] > idate ) then
		else
			ttable = DBCS_mobs_model[inummob][i];
			tremove(DBCS_mobs_model[inummob],i);
			tinsert(DBCS_mobs_model[inummob],1,ttable);
			DBCS_mobs_model[inummob][1]["datemodel"] = idate;
			DBCS_mobs_model[inummob][1]["blp"] = sblp;
			DBCS_mobs_model[inummob][1]["display"] = idisplay;
		end
		--breturn = false;
	else
		ttable = {};
		tinsert(DBCS_mobs_model[inummob],1,ttable);
		--DBCS_mobs_model[inummob][1] = {};
		DBCS_mobs_model[inummob][1]["model"] = smodele;
		DBCS_mobs_model[inummob][1]["blp"] = sblp;
		DBCS_mobs_model[inummob][1]["display"] = idisplay;
		DBCS_mobs_model[inummob][1]["datemodel"] = idate;
	end
	if ( DBCS_mobs_model[inummob][1] == nil ) then
if ( DBCS_debug and DBCS_debug > 1 ) then
TraceFrame:AddMessage("erreur DBCS_mobs_model_blp_ajout : "..inummob.." = "..smodele.." : "..slink);
end
	end
	if ( breturn == true and DBCS_Mobs_Model_Client ) then
		if ( DBCS_Mobs_Model_Client[inummob] == nil ) then
			DBCS_Mobs_Model_Client[inummob] = {};
		end
		DBCS_Mobs_Model_Client[inummob][smodele] = {};
		DBCS_Mobs_Model_Client[inummob][smodele]["blp"] = sblp;
		DBCS_Mobs_Model_Client[inummob][smodele]["display"] = idisplay;
		DBCS_Mobs_Model_Client[inummob][smodele]["date"] = idate;
	end

	return breturn;

end
