

--à utliser en aliasname
--script du CLOSEUP de recharche d'objet
.namespace CloseupSearch_Object
SEARCHINTERFACEENDPOSX = -190
DELAYBEFORECLOSING = 4.5 -- en seconde
SPEEDINTERFACECLOSING = 4 --en pixel/frame
SPEEDINTERFACEOPENING = 15 --en pixel/frame
XRANGETESTTOOPEN = 50 --si le curseur rentre dans la zone on réouvre l'interface
XRANGETOCLOSE =  210 --plus large, si on est dessu il faut pas fermer

DISPLAYTIME = 10 -- on affiche ce temps et ensuite ca disparait

function OnEnable()
	Closeup.OnEnable()
	Closeup_ShowConsigne.Enabled = false
	
	Closeup_ResetButton.Enabled = false
	g_screenname = string.lower(string.gsub(self.Name,"_Closeup",""))
	g_utmiddleground = OPUnitFactory.GetUnit(g_screenname.."_background","Background")	
	local nb = ObjectManager.BuildObjects(g_screenname,{[0]=g_utmiddleground,[1]=g_utmiddleground,[2]=g_utmiddleground})
	
	local needconsigne = ObjectManager.LaunchNewResearch(g_screenname,SEARCHINCLOSEUP)
	
	AssertPopup(type(CURRENTCHAPTER) == "string", "attetion , pensez à définir currentchapter !!")
	
	if needconsigne == true then
		local keyw = "consignes_"..CURRENTCHAPTER.."_"..string.lower(self.Name)
		keyw = string.gsub(keyw,"_closeup","")
		DisplaySimpleText(keyw,DISPLAYTIME)
		SubtitlesText.Text = string.gsub(SubtitlesText.Text,"XX",nb)
	end
	
	
	Closeup_SkipButtonOff.Enabled = false
	Closeup_SkipButton.Enabled = false
	
	
	Closeup_SkipButton.Enabled = false
	Closeup_SkipButtonOff.Enabled = false
	if DebugOptions.interfaceVersion == 2 then
		LeftInterface.Enabled = true
	else
		SearchInterface.Enabled = true
	end
	SearchInterface.Position_X = 0
	
	-- self.timerinterface = GlobalFunctions.CreateTimer("interface"..self.Name,DELAYBEFORECLOSING,false,false,true, self)
	-- self.timerinterface.Enabled=true
	-- self.timerinterface.Start()
	
	self.Flags_AcceptAllInput = true
	OPInput.RegisterListener(self , EVENT_MOUSE_MOVE)
	self.interfacephase = "open"
	
	self.lastx = 500
end

function OnMouseMove(x,y)
	-- self.lastx = x
	-- if x < XRANGETESTTOOPEN then	
		-- if self.interfacephase == "close" then
			-- SpriteMover.Move(SearchInterface,Vector:new(0,0),SPEEDINTERFACEOPENING)
			-- self.interfacephase = "open"
			-- self.timerinterface = GlobalFunctions.CreateTimer("interface"..self.Name,DELAYBEFORECLOSING,false,false,true, self)
			-- self.timerinterface.Enabled=true
			-- self.timerinterface.Start()
		-- end
	-- end
end

function OnUpdate(dt)
	-- if self.lastx < XRANGETOCLOSE then
	--			on maintient le timer ouvert
		-- if self.timerinterface ~= nil then
			-- self.timerinterface.Time = DELAYBEFORECLOSING
		-- end
	-- end
end

function OnSearchEnd()
	-- AssertPopup(unit(self.ObjectFound) ~= nil, "objet récupéré non défini !")
	if unit(self.ObjectFound) ~= nil then
		InventoryManager.AddInInventory(self.ObjectFound)
	end
	
	-- Création d'un timer qui permet de laisser afficher le texte de fin de recherche avant de revenir à l'écran précédent
	SetVariables("CurrentGamePhase","BlockClick")
	local timertoquit = GlobalFunctions.CreateTimer("endsearch"..self.Name,1.5,false,false,true, self)
	timertoquit.Enabled=true
	timertoquit.Start()
	
end

function OnTimeOut(name)
	if string.find(name,"endsearch") then
		SetVariables("CurrentGamePhase","Closeup")
		NavigationManager.SwitchTo(nil,GameVariables.Get("last_SearchSite"))
	elseif string.find(name,"interface") then
		--on ferme l'interface
		--CloseInterface()
	end
end

function CloseInterface()
	SpriteMover.Move(SearchInterface,Vector:new(SEARCHINTERFACEENDPOSX,0),SPEEDINTERFACECLOSING)
	if self.timerinterface ~= nil then
		OPUnitFactory.CloseUnit(self.timerinterface)
	end
	self.timerinterface = nil
	self.interfacephase = "close"
	
	--on ferme par sécurité les menus
	Search_ButtonPellicule.CloseScroll()
	Search_ButtonInventory.CloseScroll()
	LeftInventory.CloseInventory()
end

function ResetGame()
	self.OnDisable()
	self.OnEnable()
end

function OnDisable()
		--on vire la recherche en cours
	Closeup.OnDisable()
	
	ApplyToClass("CloseupObject",OPUnitFactory.CloseUnit)
	--on vire la recherche en cours
	ObjectManager.CleanOldUnit()
	
	SubtitlesDisplay.HideSubtitles()
	
	--sécurité
	SpriteMover.StopMove(SearchInterface)
	SearchInterface.Position_X = 0
	
	--sécurité on close les takeobjecteffect particules
	ApplyToClass("TakeObjectEffect",OPUnitFactory.CloseUnit)
end

