
.namespace Spriteliste

g_sparkle_unit=nil


function OnEnable()

	if self.scene<10 then
		namelist="s0"
	else
		namelist="s"
	end
	if string.find(GetCurrentManager().Name,"_SearchSite") then
		scenename = string.gsub(GetCurrentManager().Name, "_SearchSite", "")	
	else
		scenename = GetCurrentManager().Name
		--string.lower(string.sub(GetCurrentManager().Name,0,3))
	end
	if not SampleLevel.IsEqual(scenename.."_search_"..tostring(self.typeliste),"unstarted") then 
		self.Enabled = false
	end
	local ut = OPUnitFactory.CreateUnit("Sparkle")
	g_sparkle_unit=ut
	ut.Parent = self
	ut.Position_X = self.Dimension_Width/2 
	ut.Position_Y = self.Dimension_Height/2 
end

function OnWbpMouseLUp()
	if self.CanClickOn() then
		--OPLog.Log("click sur "..self.Name)
		-- AssertPopup(self.OnActivate ~= nil , "erreur activate non défini sur"..self.Name)
		-- local res = self.OnActivate()
		-- if scenename == 
		
		if res == true then -- s'il y a eu un changement, on sauvegarde
			SaveManager.saveGame("Profil"..ProfilConfig.Get("CurrentProfil"))
		end
		-- OPLog.Log("onactivate "..self.Name)
	
		listeouverte=0
	
		for i=1,6  do
			nonnul=0
			if not SampleLevel.IsRegistered(scenename.."_search_"..tostring(i))  then
				nonnul=1
				-- OPLog.Log("nonnul "..nonnul)
			end
			
			if nonnul==0  then
				if SampleLevel.IsEqual(scenename.."_search_"..tostring(i),"started") then 
					listeouverte=listeouverte + 1
					-- OPLog.Log("listeouverte "..listeouverte)
				end
			end
		end
		
		if listeouverte==0 then
			if string.find(GetCurrentManager().Name,"_SearchSite") then
				@ObjectManager.LaunchNewResearch(scenename,self.typeliste)
			else
				@ObjectManager.LaunchNewResearch(string.lower(string.sub(GetCurrentManager().Name,0,3)).."_cup",self.typeliste)
			end	
			self.Enabled = false
			PlaySoundEffect("gmp_grabpaper")
			if GetCurrentManager().OnTakeList ~= nil then
				GetCurrentManager().OnTakeList(self.typeliste)
			end
			
			-- OPUnitFactory.CloseUnit(self.g_sparkle_unit)
			return true
		end
		
		
		return false
	end
end

function OnUpdate()
	
	for i=1,6  do
		nonnul=0
		if not SampleLevel.IsRegistered(scenename.."_search_"..tostring(i))  then
			nonnul=1
			-- OPLog.Log("nonnul "..nonnul)
		end
		
		if nonnul==0  then
			if SampleLevel.IsEqual(scenename.."_search_"..tostring(i),"started") then 
				listeouverte=listeouverte + 1
				-- OPLog.Log("listeouverte "..listeouverte)
			end
		end
	end
	
end

function SetCursor()
	if self.CanClickOn() then
		if self.GetSpecificCursor ~= nil then
			local frame,dest = self.GetSpecificCursor()
			Cursor.ChangeCursor(frame,dest)
		else
			Cursor.ChangeCursor(3)
		end
		return true
	end
	return false
end


function CanClickOn()
	local res = self.Flags_Visible == true
	res = res and (IsEqualVariables("CurrentGamePhase","SearchSite") or IsEqualVariables("CurrentGamePhase","Closeup") 
	or (IsEqualVariables("CurrentGamePhase","Tutorial") and NewTutorialManager.g_state == "waittouseobject" ))
	
	
	if GetCurrentManager().CanClickOnList ~= nil then
		res = res and GetCurrentManager().CanClickOnList(self.Name)
	end	
	
	return res
end

function OnDisable()
	-- OPLog.Log("Closing : "..g_sparkle_unit.Name)
	-- OPUnitFactory.CloseUnit(g_sparkle_unit)
	
end