-- This file is provided under the Open Game License version 1.0a
-- For more information on OGL and related issues, see 
--   http://www.wizards.com/d20
--
-- For information on the Fantasy Grounds d20 Ruleset licensing and
-- the OGL license text, see the d20 ruleset license in the program
-- options.
--
-- All producers of work derived from this definition are adviced to
-- familiarize themselves with the above licenses, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- Copyright 2007 SmiteWorks Ltd.

 
-- ////////////////////////////////////////////////////////////////////////////////////////////////////
-- Version de base 	: 2.31 ?
--
-- Grimmbart 		: V1.0.1
-- Modif :
-- 	* 28/05/2008 Whisp entre 2 joueurs


function applyModifierStackToRoll(draginfo)
	if ModifierStack.isEmpty() then
		--[[ do nothing ]]
	elseif draginfo.getNumberData() == 0 and draginfo.getDescription() == "" then
		draginfo.setDescription(ModifierStack.getDescription());
		draginfo.setNumberData(ModifierStack.getSum());
	else
		local originalnumber = draginfo.getNumberData();
		local numstr = tostring(originalnumber);
		if originalnumber > 0 then
			numstr = "+" .. originalnumber;
		end

		local moddesc = ModifierStack.getDescription(true);
		local desc = draginfo.getDescription();
		
		if numstr ~= "0" then
			desc = desc .. " " .. numstr;
		end
		if moddesc ~= "" then
			desc = desc .. " (" .. moddesc .. ")";
		end
		
		draginfo.setDescription(desc);
		draginfo.setNumberData(draginfo.getNumberData() + ModifierStack.getSum());
	end
	
	ModifierStack.reset();
end




function onDiceLanded(draginfo)
	

	print("onDiceLanded 2");
	
	if ChatManager.getDieRevealFlag() then
		draginfo.revealDice(true);
	end

	applyModifierStackToRoll(draginfo);
	

	-- *****************************  TEST D100
	local entry = {};
	entry.dice = draginfo.getDieList();			
	entry.text = draginfo.getDescription();
	entry.font = "systemfont";
	entry.diemodifier = draginfo.getNumberData();		
				
	-- Pour 1 D100, il faut 2 dés !! (1d100 et 1d10)
	if(entry.dice[2] ~= nil)then		
		-- Si le 1er dé = D10 et le 2nd = d100   OU  1er dé = D100 et le 2nd = d10 
		if((entry.dice[1].type == "d10") and (entry.dice[2].type == "d100") or 
		   (entry.dice[1].type == "d100") and (entry.dice[2].type == "d10"))then			
			-- Calcul résultat du jet
			local d100Result = entry.dice[1].result + entry.dice[2].result;
			-- Réécriture dans le tableau
			entry.dice = {{type="d100", result=d100Result}};					
		end
	end	 	 
	
	-- Si Dé Custom
	if(entry.dice[1].type == "d80") then			
		-- Réécriture dans le tableau
		entry.dice = {{type="d100", result=d100Result}};	
		entry.dice = "";			
		entry.text = "Dé custom";
		entry.font = "systemfont";
		entry.diemodifier = 0;		
	
		if User.isHost() then
			if ChatManager.getDieRevealFlag() then
				entry.dicesecret = false;
			end
			entry.sender = GmIdentityManager.getCurrent();
		else
			entry.sender = User.getIdentityLabel();
		end
	
		-- On reposte le message
		deliverMessage(entry);
		
		-- Retour (pour terminer les affichages)
		return true;	
	end

	
	-- Pour éviter la triche avec les D10 et les D100
	local d10Found = false;
	local d100Found = false;
	local ListeDes = "";
	--
	if entry.dice then
		for k,die in pairs(entry.dice) do
			if die.type == "d100" then
				d100Found = true;  				
			end
			if die.type == "d10" then
				d10Found = true;				
			end										
			-- Composition de la liste des dés
			--print("dieList: " .. die.type);		
		end
		-- Composition de la liste des dés
		if d100Found and d10Found then		
			ListeDes = " [Tas de d10 et d100 !!]";					
		elseif d100Found then		
			ListeDes = " [" .. #entry.dice .. "d100]";					
		elseif d10Found  then			
			ListeDes = " [" .. #entry.dice .. "d10]";
		end				
	end
	
	
	-- On complete la structure 'Entry" avant le renvoie
	if User.isHost() then
		if ChatManager.getDieRevealFlag() then
			entry.dicesecret = false;
		end
		entry.sender = GmIdentityManager.getCurrent();
	else
		entry.sender = User.getIdentityLabel();
	end
		
	--  On calcul le résultat : Jet de dés + modificateur
	local Total = entry.diemodifier;
	for k,De in ipairs(entry.dice) do
		if De.result then
			Total = Total + De.result;
		end
	end
	--
	local Text = entry.text .. ListeDes;
	entry.text = Text .." [Total: ".. Total.."]";	
	
	-- On reposte le message
	deliverMessage(entry);
	
	-- Retour (pour terminer les affichages)
	return true;
end







function onDrop(x, y, draginfo)
	if draginfo.getType() == "number" then
		applyModifierStackToRoll(draginfo);
	end
end

function moduleActivationRequested(module)
	local msg = {};
	msg.text = "Players have requested permission to load '" .. module .. "'";
	msg.font = "systemfont";
	msg.icon = "indicator_moduleloaded";
	addMessage(msg);
end

function moduleUnloadedReference(module)
	local msg = {};
	msg.text = "Could not open sheet with data from unloaded module '" .. module .. "'";
	msg.font = "systemfont";
	addMessage(msg);
end

function onInit()
	ChatManager.registerControl(self);
	
	if User.isHost() then
		Module.onActivationRequested = moduleActivationRequested;
				
		-- Info Version Grimmbart
		ChatManager.GrimmVersion();
		-- Info sur Aide
		local msg = {};		
		msg.font = "systemfont";	
		msg.text = "";
		deliverMessage(msg);
		--		
		msg.text = "";
		deliverMessage(msg);
		msg.text = "Pour affficher l'aide, tapez la commande /";		
		deliverMessage(msg);
		msg.text = "";	
		deliverMessage(msg);
		
	else
		-- Info Version Grimmbart
		ChatManager.GrimmVersion();
		-- Info sur Aide
		local msg = {};		
		msg.font = "systemfont";	
		msg.text = "";
		deliverMessage(msg);
		--
		msg.text = "";
		deliverMessage(msg);
		msg.text = "Pour affficher l'aide, tapez la commande /";
		deliverMessage(msg);
		msg.text = "";
		deliverMessage(msg);
	end

	Module.onUnloadedReference = moduleUnloadedReference;
end

function onClose()
	ChatManager.registerControl(nil);
end


function onReceiveMessage(msg)
	if not User.isHost() then
		return false;
	elseif msg.sender ~= "thebox_box_roll:" then
		return false;
	else
		ChatManager.processDie(msg.text);
		return true;
	end
end

