local TraceFrame = DEFAULT_CHAT_FRAME;

--CraftFrameTitleText:SetText(GetCraftName());

--numberOfCrafts = GetNumCrafts();

--craftName, craftSubSpellName, craftType, numAvailable, isExpanded, trainingPointCost, requiredLevel = GetCraftInfo(index)
--GetCraftItemLink(index)   - Returns an itemLink for the specified craftable item

--numRequiredReagents = GetCraftNumReagents(index)

--name, texturePath, numRequired, numHave = GetCraftReagentInfo(index, n);
--reagentLink = GetCraftReagentItemLink(index, n)

--craftDescription = GetCraftDescription(index)
--name, rank, maxRank = GetCraftDisplaySkillLine()

--local tradeskillName, currentLevel, maxLevel GetTradeSkillLine(); 
--skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(skillIndex)
--link = GetTradeSkillItemLink(skillId)
--minMade, maxMade = GetTradeSkillNumMade(skillId)
--numReagents = GetTradeSkillNumReagents(tradeSkillRecipeId);
--reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(tradeSkillRecipeId, reagentId);
--link = GetTradeSkillReagentItemLink(skillId, reagentId)
--GetTradeSkillTools(skillIndex);

DBCS_GetTradeSkillInfo_id = 0;
DBCS_GetTradeSkillInfo_time = 0;

--local Saved_DBCS_GetTradeSkillInfo = GetTradeSkillInfo;
function DBCS_GetTradeSkillInfo(id)

local tradeskillName;
local currentLevel;
local maxLevel;
local skillName;
local skillType;
local numAvailable;
local isExpanded;
local minMade;
local maxMade;
local numReagents;
local i;
local reagentName;
local reagentTexture;
local reagentCount;
local playerReagentCount;
local sdate;
local slead;
local stime;
local ibon;
local link;
local c;

	skillName, skillType, numAvailable, isExpanded = Saved_DBCS_GetTradeSkillInfo(id);
	if ( (GetTime() - DBCS_GetTradeSkillInfo_time) > 1 ) then
		tradeskillName, currentLevel, maxLevel = GetTradeSkillLine(); 
		if ( skillType == "header" ) then
		else
			numReagents = GetTradeSkillNumReagents(id);
			minMade, maxMade = GetTradeSkillNumMade(id);
		end
--if ( DBCS_debug > 1 ) then
	--TraceFrame:AddMessage("-----------------------------------------");
	--TraceFrame:AddMessage("craft = "..tradeskillName);
	--TraceFrame:AddMessage("id = "..id);
	--TraceFrame:AddMessage("skillName = "..skillName);
	--TraceFrame:AddMessage("skillType = "..skillType);
	--TraceFrame:AddMessage("numAvailable = "..numAvailable);
	--if ( skillType == "header" ) then
	--else
	--	TraceFrame:AddMessage("GetTradeSkillItemLink = "..GetTradeSkillItemLink(id));
	--	TraceFrame:AddMessage("minMade = "..minMade);
	--	TraceFrame:AddMessage("maxMade = "..maxMade);
	--	TraceFrame:AddMessage("numReagents = "..numReagents);
	--	for i = 1 , numReagents do
	--		reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(id, i);
	--		if ( reagentName ) then
	--			TraceFrame:AddMessage("reagentName = "..reagentName);
	--			TraceFrame:AddMessage("reagentTexture = "..reagentTexture);
	--			TraceFrame:AddMessage("reagentCount = "..reagentCount);
	--			TraceFrame:AddMessage("playerReagentCount = "..playerReagentCount);
	--			TraceFrame:AddMessage("GetTradeSkillReagentItemLink = "..GetTradeSkillReagentItemLink(id,i));
	--		end
	--	end
	--end
	--TraceFrame:AddMessage("-----------------------------------------");
--end
----------------------------------------------------------------
		if ( skillType == "header" or skillName == nil ) then
		else
			sdate = DBCS_date();
--/script DEFAULT_CHAT_FRAME:AddMessage(tonumber(DBCS_date()));
--/script DEFAULT_CHAT_FRAME:AddMessage(tonumber(nil));
--/script DEFAULT_CHAT_FRAME:AddMessage(tonumber(DBmobs_datemob("blabla","0")));
--/script DEFAULT_CHAT_FRAME:AddMessage(tonumber(DBmobs_datemob("blabla","0"))+7);
			if ( tonumber(sdate) < (tonumber(DBmobs_datemob(skillName,"1"))+7) ) then
			else
				DBCS_GetTradeSkillInfo_id = id;
				slead = DBCS_joueur;
				stime = DBCS_time();
				DBCS_mob = {};
				DBCS_mob.time = GetTime()*1000;
				DBCS_mob.name = skillName;
				DBCS_mob.level = 0;
				DBCS_mob.classe = "artisanat";
				if ( DBMS and DBMS[skillName] and DBMS[skillName]["1"] ) then
					DBCS_mob.famille = DBMS[skillName]["1"][4];
				else
					DBCS_mob.famille = "";
				end
				DBCS_mob.type = tradeskillName;
				DBCS_mob.plusmob = nil;
				DBCS_mob.nbloot = numReagents + 1;
				DBCS_mob.loot = {};
				DBCS_mob.numzone = "1";
				DBCS_mob.x = 0;
				DBCS_mob.y = 0;
				ibon = 1;
				for i = 1, DBCS_mob.nbloot - 1 do
					DBCS_mob.loot[i] = {};
					reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(id, i);
					if ( reagentName ) then
						link = GetTradeSkillReagentItemLink(id,i);
						if ( link ) then
							for _,c,_,_,_,_,_,_,_,_,_,_ in string.gmatch(link,DBCS_message_item) do
								DBCS_mob.loot[i].numobjet = tonumber(c);
								DBCS_mob.loot[i].numcourt2 = "0:0:0:0:0:0:0:0";
								DBCS_mob.loot[i].nb = reagentCount;
							end
							if ( DBCS_mob.loot[i].numobjet ) then
							else
								ibon = 0;
							end
						else
							ibon = 0;
						end
					else
						ibon = 0;
					end
				end
				i = DBCS_mob.nbloot;
				DBCS_mob.loot[i] = {};
				link = GetTradeSkillItemLink(id);
				if ( link ) then
					for _,c,_,_,_,_,_,_,_,_,_,_ in string.gmatch(link,DBCS_message_item) do
						DBCS_mob.loot[i].numobjet = tonumber(c);
						DBCS_mob.loot[i].numcourt2 = "0:0:0:0:0:0:0:0";
						DBCS_mob.loot[i].nb = minMade;
					end
					if ( DBCS_mob.loot[i].numobjet ) then
					else
						ibon = 0;
					end
				else
					ibon = 0;
				end
				if ( ibon == 1 ) then
					DBCS_GetTradeSkillInfo_id = 0;
if ( DBCS_debug > 1 ) then
TraceFrame:AddMessage(DBCS_mob.name.." est bien lu.");
end
					if ( DBCS_CMS_numcanalCMS > 0 ) then
						DBmobs_envoyermob(DBCS_getlocale,sdate,slead,stime,DBCS_mob.name,DBCS_mob.numzone,DBCS_mob.x,DBCS_mob.y,DBCS_mob.level,DBCS_mob.classe,DBCS_mob.famille,DBCS_mob.type,DBCS_mob.plusmob,1,0,DBCS_mob.nbloot);
					end
					DBcorps_ajoutmob(DBCS_getlocale,sdate,slead,stime,DBCS_mob.name,DBCS_mob.numzone,DBCS_mob.x,DBCS_mob.y,DBCS_mob.level,DBCS_mob.classe,DBCS_mob.famille,DBCS_mob.type,DBCS_mob.plusmob,1,0,DBCS_mob.nbloot);
					for i = 1, DBCS_mob.nbloot do
						if ( DBCS_CMS_numcanalCMS > 0 ) then
							DBmobs_envoyerloot(DBCS_getlocale,sdate,slead,stime,i,DBCS_mob.loot[i].numobjet,DBCS_mob.loot[i].numcourt2,DBCS_mob.loot[i].nb);
						end
						DBcorps_ajoutloot(sdate,slead,stime,i,DBCS_mob.loot[i].numobjet,DBCS_mob.loot[i].numcourt2,DBCS_mob.loot[i].nb);
					end
					DBCS_GetTradeSkillInfo_time = GetTime();
				else
if ( DBCS_debug > 1 ) then
TraceFrame:AddMessage(DBCS_mob.name.." est mal lu.<---------------------------------");
end
				end
			end
		end
----------------------------------------------------------------
	end
	return skillName, skillType, numAvailable, isExpanded;

end
--GetTradeSkillInfo = DBCS_GetTradeSkillInfo;

DBCS_GetCraftInfo_id = 0;
DBCS_GetCraftInfo_time = 0;

--craftName, craftSubSpellName, craftType, numAvailable, isExpanded, trainingPointCost, requiredLevel = GetCraftInfo(id);
--local Saved_DBCS_GetCraftInfo = GetCraftInfo;
function DBCS_GetCraftInfo(id)

local craftName;
local craftSubSpellName;
local craftType;
local numAvailable;
local isExpanded;
local trainingPointCost;
local requiredLevel;
local numRequiredReagents;
local i;
local name;
local texturePath;
local numRequired;
local numHave;
local reagentLink;
local link;
local sdate;
local slead;
local stime;
local ibon;
local c;

	craftName, craftSubSpellName, craftType, numAvailable, isExpanded, trainingPointCost, requiredLevel = Saved_DBCS_GetCraftInfo(id);
	-- dressage des bêtes , type = none
	-- Enchantement, type = optimal etc
	if ( (GetTime() - DBCS_GetCraftInfo_time) > 1 ) then
		if ( craftName == nil or craftType == "none" or craftType == "header" or craftType == "used" ) then
		else
			sdate = DBCS_date();
			if ( tonumber(sdate) < (tonumber(DBmobs_datemob(craftName,"1"))+7) ) then
			else
--if ( DBCS_debug > 1 ) then
--TraceFrame:AddMessage("craftType = "..craftType);
--end
				DBCS_GetCraftInfo_id = id;
				numRequiredReagents = GetCraftNumReagents(id);
				slead = DBCS_joueur;
				stime = DBCS_time();
				DBCS_mob = {};
				DBCS_mob.time = GetTime()*1000;
				DBCS_mob.name = craftName;
				DBCS_mob.level = 0;
				DBCS_mob.classe = "artisanat";
				if ( DBMS and DBMS[craftName] and DBMS[craftName]["1"] ) then
					DBCS_mob.famille = DBMS[craftName]["1"][4];
				else
					DBCS_mob.famille = "";
				end
				DBCS_mob.type = GetCraftName();
				DBCS_mob.plusmob = nil;
				DBCS_mob.nbloot = numRequiredReagents;
				DBCS_mob.loot = {};
				DBCS_mob.numzone = "1";
				DBCS_mob.x = 0;
				DBCS_mob.y = 0;
				ibon = 1;
				for i = 1, DBCS_mob.nbloot do
					DBCS_mob.loot[i] = {};
					name, texturePath, numRequired, numHave = GetCraftReagentInfo(id, i);
					if ( name ) then
						link = GetCraftReagentItemLink(id, i);
						if ( link ) then
							for _,c,_,_,_,_,_,_,_,_,_,_ in string.gmatch(link,DBCS_message_item) do
								DBCS_mob.loot[i].numobjet = tonumber(c);
								DBCS_mob.loot[i].numcourt2 = "0:0:0:0:0:0:0:0";
								DBCS_mob.loot[i].nb = numRequired;
							end
							if ( DBCS_mob.loot[i].numobjet ) then
							else
								ibon = 0;
							end
						else
							ibon = 0;
						end
					else
						ibon = 0;
					end
				end
--			link = GetCraftItemLink(id);
--			if ( link ) then
--if ( DBCS_debug > 1 ) then
--TraceFrame:AddMessage("link enchant = "..link);
--end
--			else
--			end
				if ( ibon == 1 ) then
					DBCS_GetCraftInfo_id = 0;
if ( DBCS_debug > 1 ) then
TraceFrame:AddMessage(DBCS_mob.name.." est bien lu.");
end
					if ( DBCS_CMS_numcanalCMS > 0 ) then
						DBmobs_envoyermob(DBCS_getlocale,sdate,slead,stime,DBCS_mob.name,DBCS_mob.numzone,DBCS_mob.x,DBCS_mob.y,DBCS_mob.level,DBCS_mob.classe,DBCS_mob.famille,DBCS_mob.type,DBCS_mob.plusmob,1,0,DBCS_mob.nbloot);
					end
					DBcorps_ajoutmob(DBCS_getlocale,sdate,slead,stime,DBCS_mob.name,DBCS_mob.numzone,DBCS_mob.x,DBCS_mob.y,DBCS_mob.level,DBCS_mob.classe,DBCS_mob.famille,DBCS_mob.type,DBCS_mob.plusmob,1,0,DBCS_mob.nbloot);
					for i = 1, DBCS_mob.nbloot do
						if ( DBCS_CMS_numcanalCMS > 0 ) then
							DBmobs_envoyerloot(DBCS_getlocale,sdate,slead,stime,i,DBCS_mob.loot[i].numobjet,DBCS_mob.loot[i].numcourt2,DBCS_mob.loot[i].nb);
						end
						DBcorps_ajoutloot(sdate,slead,stime,i,DBCS_mob.loot[i].numobjet,DBCS_mob.loot[i].numcourt2,DBCS_mob.loot[i].nb);
					end
					DBCS_GetCraftInfo_time = GetTime();
				else
if ( DBCS_debug > 1 ) then
TraceFrame:AddMessage(DBCS_mob.name.." est mal lu.<---------------------------------");
end
				end
			end
		end
--if ( DBCS_debug > 1 ) then
--	TraceFrame:AddMessage("-----------------------------------------");
--	TraceFrame:AddMessage("craft = "..GetCraftName());
--	TraceFrame:AddMessage("id = "..id);
--	TraceFrame:AddMessage("craftName = "..craftName);
--	TraceFrame:AddMessage("craftSubSpellName = "..craftSubSpellName);
--	TraceFrame:AddMessage("craftType = "..craftType);
--	TraceFrame:AddMessage("numAvailable = "..numAvailable);
--	--TraceFrame:AddMessage("isExpanded = "..isExpanded);
--	TraceFrame:AddMessage("trainingPointCost = "..trainingPointCost);
--	TraceFrame:AddMessage("requiredLevel = "..requiredLevel);
----GetCraftItemLink(index)   - Returns an itemLink for the specified craftable item
--	link = GetCraftItemLink(id);
--	if ( link ) then
--		TraceFrame:AddMessage("GetCraftItemLink = "..link);
----numRequiredReagents = GetCraftNumReagents(index)
----numRequiredReagents = GetCraftNumReagents(index)
--		TraceFrame:AddMessage("GetCraftNumReagents = "..numRequiredReagents);
--		for i = 1 , numRequiredReagents do
----name, texturePath, numRequired, numHave = GetCraftReagentInfo(index, n);
--			name, texturePath, numRequired, numHave = GetCraftReagentInfo(id, i);
--			TraceFrame:AddMessage("i = "..i);
--			TraceFrame:AddMessage("name = "..name);
--			TraceFrame:AddMessage("texturePath = "..texturePath);
--			TraceFrame:AddMessage("numRequired = "..numRequired);
--			TraceFrame:AddMessage("numHave = "..numHave);
----reagentLink = GetCraftReagentItemLink(index, n)
--			reagentLink = GetCraftReagentItemLink(id, i);
--			TraceFrame:AddMessage("reagentLink = "..reagentLink);
--		end
--	end
--	TraceFrame:AddMessage("-----------------------------------------");
--end
	end
	return craftName, craftSubSpellName, craftType, numAvailable, isExpanded, trainingPointCost, requiredLevel;

end
--GetCraftInfo = DBCS_GetCraftInfo;

--TradeSkill
--local Saved_DBCS_TradeSkillFrame_SetSelection = TradeSkillFrame_SetSelection;
--function DBCS_TradeSkillFrame_SetSelection(id)

--local skillName;
--local skillType;
--local numAvailable;
--local isExpanded;

--	Saved_TradeSkillFrame_SetSelection(id);
--if ( DBCS_debug > 1 ) then
--	TraceFrame:AddMessage("-----------------------------------------");
--	TraceFrame:AddMessage("GetTradeSkillSelectionIndex() = "..GetTradeSkillSelectionIndex());
--	TraceFrame:AddMessage("id = "..id);
--	skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(id);
--	TraceFrame:AddMessage("skillName = "..skillName);
--	TraceFrame:AddMessage("skillType = "..skillType);
--	TraceFrame:AddMessage("numAvailable = "..numAvailable);
--	TraceFrame:AddMessage("isExpanded = "..isExpanded);
--end

--end
--TradeSkillFrame_SetSelection = DBCS_TradeSkillFrame_SetSelection;
--TradeSkillFrame_SetSelection
--hooksecurefunc("TradeSkillFrame_SetSelection", DBCS_TradeSkillFrame_SetSelection);

--CraftButtonTemplate

--CraftFrame_SetSelection
--local Saved_DBCS_CraftFrame_SetSelection = CraftFrame_SetSelection;
--function DBCS_CraftFrame_SetSelection(arg1)
--
--local craftName;
--local craftSubSpellName;
--local craftType;
--local numAvailable;
--local isExpanded;
--local trainingPointCost;
--local requiredLevel;
--local id;
--
--	if ( arg1 == "LeftButton" ) then
--		id = this:GetID();
--	else
--		return nil;
--	end
--	--Saved_CraftFrame_SetSelection(id);
--if ( DBCS_debug > 1 ) then
--	TraceFrame:AddMessage("-----------------------------------------");
--	TraceFrame:AddMessage("craft = "..GetCraftName());
--	TraceFrame:AddMessage("id = "..id);
--	craftName, craftSubSpellName, craftType, numAvailable, isExpanded, trainingPointCost, requiredLevel = GetCraftInfo(id);
--	TraceFrame:AddMessage("craftName = "..craftName);
--	TraceFrame:AddMessage("craftType = "..craftType);
--	TraceFrame:AddMessage("numAvailable = "..numAvailable);
--end
--
--end
--CraftFrame_SetSelection = DBCS_CraftFrame_SetSelection;

--CraftButtonTemplate:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft1:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft2:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft3:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft4:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft5:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft6:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft7:SetScript("OnClick",DBCS_CraftFrame_SetSelection);
--Craft8:SetScript("OnClick",DBCS_CraftFrame_SetSelection);

--hooksecurefunc("CraftFrame_SetSelection", DBCS_CraftFrame_SetSelection);

--local Saved_DBCS_CraftButton_OnClick = CraftButton_OnClick;
--function CraftButton_OnClick(button)
--	Saved_CraftButton_OnClick(button);
--if ( DBCS_debug > 1 ) then
--	TraceFrame:AddMessage("-----------------------------------------");
--	TraceFrame:AddMessage("button = "..button);
--end
--end
--CraftButton_OnClick = DBCS_CraftButton_OnClick;

--	Requiert : Couture (300)


function DBcorps_artisanat_OnLoad(self)

	--this:RegisterEvent("CRAFT_SHOW");
	--this:RegisterEvent("CRAFT_UPDATE");
	--this:RegisterEvent("TRADE_SKILL_SHOW");
	--this:RegisterEvent("TRADE_SKILL_UPDATE");
	
end

function DBcorps_artisanat_OnEvent(self,event,...)

--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
--end
	if ( event == "" ) then
	elseif ( event == "CRAFT_SHOW" ) then
--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
--end
	elseif ( event == "CRAFT_UPDATE" ) then
		if ( DBCS_GetCraftInfo_id > 0 ) then
			DBCS_GetCraftInfo_time = 0;
			GetCraftInfo(DBCS_GetCraftInfo_id);
		end
--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
--end
	elseif ( event == "TRADE_SKILL_SHOW" ) then
--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
--end
	elseif ( event == "TRADE_SKILL_UPDATE" ) then
		if ( DBCS_GetTradeSkillInfo_id > 0 ) then
			DBCS_GetTradeSkillInfo_time = 0;
			GetTradeSkillInfo(DBCS_GetTradeSkillInfo_id);
		end
--if ( DBCS_debug > 1 ) then
--DBCS_afficherevent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
--end
	end

end
