local linksrv = lgs.LinkSrv
local datasrv = lgs.DataSrv
local uisrv = lgs.DarkUISrv
local propsrv = lgs.PropertySrv

local defaultpath = "../books/"
local defaultpath2 = "../RES/books/"
local defaultpath3 = "../FMs/WoodenBox/books/"
local defaultpath4 = "../"
local defaultpath5 = "../scripts/"
local filename = ""
local currenttext = ' '

local function BookReader()
	local path1 = defaultpath .. filename
	
	local path2 = defaultpath2 .. filename
	
	local path3 = defaultpath3 .. filename
	
	local path4 = defaultpath4 .. filename
	
	local path5 = defaultpath5 .. filename
	
	if datasrv.GetString(path1,"page_0","strings") ~= nil then
		print(path1)
		currenttext = currenttext .. "\n" .. "\n" .. datasrv.GetString(path1,"page_0","strings")
	elseif datasrv.GetString(path2,"page_0","strings") ~= nil then
		print(path2)
		currenttext = currenttext .. "\n" .. "\n" .. datasrv.GetString(path2,"page_0","strings")
	elseif datasrv.GetString(path3,"page_0","strings") ~= nil then
		print(path3)
		currenttext = currenttext .. "\n" .. "\n" .. datasrv.GetString(path3,"page_0","strings")
	elseif datasrv.GetString(path4,"page_0","strings") ~= nil then
		print(path4)
		currenttext = currenttext .. "\n" .. "\n" .. datasrv.GetString(path4,"page_0","strings")
	else
		print(path5)
		currenttext = currenttext .. "\n" .. "\n" .. datasrv.GetString(path5,"page_0","strings")
	end	
end


function TurnOn(msg)
	print(">>>TURNON" .. tostring(msg.From))
	filename = propsrv.Get(msg.From,'Book')
	BookReader()
	if not linksrv.AnyExist('ScriptParams',script.objid,msg.From) then
		linksrv.Create('ScriptParams',script.objid,msg.From)
	end
	print("TURNON<<<")
	return true
end

function FrobWorldBegin(msg)
	if currenttext == ' ' then
		linksrv.BroadcastOnAllLinks(script.objid,'TurnOn','ScriptParams')
	end
	return true
end
function FrobWorldEnd(msg)
	print("Il était une fois",currenttext,"Fin")
	uisrv.TextMessage(currenttext,propsrv.Get(script.objid,'ScriptTiming'),tonumber((string.len(currenttext)*10)))
	return true
end
