function loadSpeech(fileName)
	local speechProp=loadPropertiesFile(fileName)
	local speechTable={}
	for key,value in pairs(speechProp) do
		speechTable[key]={}
		local indice=1
		local finished=false
		local probaMin=1
		--fdebug("processing "..key.."-"..value.." pair :")
		--will break the string into groups of speech and proba
		while not finished do
			local groupEnd=string.find(value, "]", 1, true)
			local oneGroup=string.sub(value, 1, groupEnd)
			--fdebug("found group: "..oneGroup)
			local sz=stringSize(value)
			if stringSize(oneGroup)<5 then
				--fdebug("stringSize(oneGroup)="..stringSize(oneGroup))
				break
			end
			
			--breaking oneGroup into speech and proba
			local pipeIndice=string.find(oneGroup, "|", 1, true)
			local speechStr=string.sub(oneGroup, 2, pipeIndice-1)
			local proba=tonumber(string.sub(oneGroup, pipeIndice+1, -2))
			--fdebug("found speech="..speech.." and proba="..proba)
			
			
			--save that in a table
			speechTable[key][indice]={}
			speechTable[key][indice]["speech"]=speechStr
			speechTable[key][indice]["min"]=probaMin-1
			speechTable[key][indice]["max"]=probaMin+proba-1
			--fdebug("speechTable["..key.."]["..indice.."][\"speech\"]="..speechTable[key][indice]["speech"])
			---fdebug("speechTable["..key.."]["..indice.."][\"min\"]="..speechTable[key][indice]["min"])
			--fdebug("speechTable["..key.."]["..indice.."][\"max\"]="..speechTable[key][indice]["max"])
			probaMin=probaMin+proba --for next iteration
			indice=indice+1
			
			value=string.sub(value, groupEnd+1, sz)
		end
	end
	return speechTable
end

SPEECHTABLE=loadSpeech("configuration/speech.properties")


function getRandomSpeech(key) --random based on the configuration file
	--fdebug("getRandomSpeech called with "..key)
	local subTable=SPEECHTABLE[key]
	if subTable==nil then
		--fdebug("getRandomSpeech returns nil because subTable is nil")
		return nil
	end
	local nbSpeeches=tableLength(subTable)
	local r=betterRandom(0,99)
	local i=0
	for i=1, nbSpeeches do
		if r>=subTable[i]["min"] and r<subTable[i]["max"] then
			--fdebug("getRandomSpeech returns "..subTable[i]["speech"])
			return subTable[i]["speech"]
		end
	end
	--fdebug("getRandomSpeech returns nil because it decided to")
	return nil
end

lastrandomforspeech=0
--will ensure the same random number is not selected consecutively
--forces a difference of 5
function betterRandom(min, max)
	local good=false
	local r=0
	if max-min<15 then
		return random(min, max)
	end
	while not good do
		r=random(min, max)
		if r<=lastrandomforspeech-5 or r>=lastrandomforspeech+5 then
			lastrandomforspeech=r
			return r
		end
	end
	return min --default
end

function getSpeechAboutCards(tableOfCards)
	if tableOfCards==nil then
		return nil	
	end
	local sz=tableLength(tableOfCards)
	local nbTwos=countCardsByPower(tableOfCards, TWO)
	local pow=cardsPower(tableOfCards)
	--look for a super combo
	if ((sz>=5 and nbTwos>=2) or (nbTwos>=3)) and sz~=nbTwos then
		--fdebug("getSpeechAboutCards found supercombo")
		return getRandomSpeech("superCombo")
	--plays a two
	elseif nbTwos==1 and sz==1 then
		--fdebug("getSpeechAboutCards found bigGroup")
		return getRandomSpeech("playA2")
	--look for a powerful group
	elseif 		countCardsByPower(tableOfCards, JACK)>=5
			or	countCardsByPower(tableOfCards, KNIGHT)>=5
			or 	countCardsByPower(tableOfCards, QUEEN)>=5
			or	countCardsByPower(tableOfCards, KING)>=4
			or  countCardsByPower(tableOfCards, ACE)>=3
			or  nbTwos>=2 then
		--fdebug("getSpeechAboutCards found powerfulGroup")
		return getRandomSpeech("powerfulGroup")
	--look for a simple combo
	elseif sz~=nbTwos and nbTwos>=1 then
		--fdebug("getSpeechAboutCards found combine")
		return getRandomSpeech("combine")
	end
	--look for a big group
	if sz>=6 then
		--fdebug("getSpeechAboutCards found bigGroup")
		return getRandomSpeech("bigGroup")
	--look if cards are weak
	elseif (sz==1 and pow<=QUEEN) then
		 return getRandomSpeech("badCardsSingle")
	elseif pow <10 then
		return getRandomSpeech("badCardsGroup")
	else --normal cards
		return getRandomSpeech("normalCards")
	end
	return nil
end

function countCardsByPower(tableOfCards, power)
	local i=0
	local ret=0
	local sz=tableLength(tableOfCards)
	--fdebug("countCardsByPower called with "..sz.." cards and power = "..power)
	for i=1, sz do
		if tableOfCards[i].power==power then
			ret=ret+1
		end
	end
	return ret
end

function cardsPower(tableOfCards)
	return tableOfCards[1].power
end













