
.namespace LaunchingCard

SHIFTPOSPARTX = 38
SHIFTPOSPARTY = 2

SCALECOEFFX = 0.042 --on multiplie cela par la taille de la liste 
SCALECOEFFY = 0.067


function Unactivate()
	if self.part ~= nil then
		self.part.Enabled = false
	end
	self.active = false
end


function OnEnable()
	--self.Image = "media/common/graph/interface/InGame/card_1.png"
	AssertPopup(self.SearchName ~= "", "recherche non défini sur "..self.Name)
	
	--todo à changer pour le datarray du niveau courant
	self.active = SampleLevel.IsEqual(self.SearchName,"unstarted")
	
	self.Flags_Visible = self.active
	
	--sécurité
	self.Color.a = 255
	
	if self.active then
		--self.blinkunit = GlobalFunctions.StartBlinkUnit(self)

		CreateParticlesForList()
	end
		
	self.firstframe = true
end

function Activate()
	AssertPopup(SampleLevel.IsEqual(self.SearchName,"unstarted") , "on active une liste déjà commencé !!!")
	self.active = true
	CreateParticlesForList()
end

function CreateParticlesForList()
	if self.part ~= nil then
		self.part.Enabled = true
		return
	end

	self.part = OPUnitFactory.CopyUnit(FX_list)
	self.part.Position_X = self.Dimension_Width /2
	self.part.Position_Y = self.Dimension_Height /2
	self.part.Parent = self
	self.part.Enabled = true
	
	--plu d'actualités
	-- if self.PartsColor.r +self.PartsColor.g + self.PartsColor.b > 0 then
		-- self.part.ColorEnd.r = self.PartsColor.r
		-- self.part.ColorEnd.g = self.PartsColor.g
		-- self.part.ColorEnd.b = self.PartsColor.b
		-- self.part.ColorMiddle.r = self.PartsColor.r
		-- self.part.ColorMiddle.g = self.PartsColor.g
		-- self.part.ColorMiddle.b = self.PartsColor.b
		-- self.part.ColorStart.r = self.PartsColor.r
		-- self.part.ColorStart.g = self.PartsColor.g
		-- self.part.ColorStart.b = self.PartsColor.b
	-- end
	-- self.part.ScaleEnd_X = self.Dimension_Width * SCALECOEFFX
	-- self.part.ScaleEnd_Y = self.Dimension_Height * SCALECOEFFY
end

function OnUpdate()
	if self.firstframe then
		self.firstframe = false
		--on attend une frame pour que les layers soient bien en place
		
		--on le vire pour les blink
		--self.Layer = unit(self.Parent.Layer.Name.."01") -- un peu bricolage ? | pour s'assurer que les particules passent derriére
	end
end

function OnDisable()
	if self.part ~= nil then
		OPUnitFactory.CloseUnit(self.part)
		self.part = nil
	end

end

function OnWbpMouseLDown()
	if not self.CanClickOn() then
		return
	end

	if self.active then
		if ObjectManager.SearchActivated() then
			PlaySoundEffect("int_lieuselection01")
			local n= math.random(3)
			DisplaySimpleText("liste_c00_liste_0"..n)
			return
		else
			PlaySoundEffect("int_liste01")
			local s = string.gsub(self.SearchName,"_search_","")
			local screenname = string.sub(s,1,3)
			local typesearch = string.sub(s,4,4)
			ObjectManager.LaunchNewResearch(screenname,tonumber(typesearch))
			
			if self.blinkunit ~= nil then
				OPUnitFactory.CloseUnit(self.blinkunit)
				self.blinkunit = nil
			end
			
			self.active = false
			
			SpriteFader.Fade(self,false,1)
			if IsEqualVariables("CurrentGamePhase","Tutorial") then
				TutorialManager.OnListClick()
			end
			
			if self.part ~= nil then
				self.part.CreationPerSec = 0
			end
			
			--on indique à l'écran en cours qu'on a lancé la liste
			if self.Parent.Parent.OnSearchLaunched then
				self.Parent.Parent.OnSearchLaunched(self.Name)
			end

			--hack pour que les soustitres ne se cachent pas immédiatement
			SubtitlesDisplay.g_skipnextonmouselup = true
			
			return true
		end
	end
	
	return false
end

function SetCursor()
	if self.CanClickOn() then
		OPUnitFactory.GetUnit("Cursor","Cursor").ChangeCursor(CURSOR_AREA_HOTSPOT)
		return true
	end	
	return false
end


function CanClickOn()
	local res = (IsEqualVariables("CurrentGamePhase","SearchSite") or 
		(IsEqualVariables("CurrentGamePhase","Tutorial") and TutorialManager.g_state == "waittoopenlist" and self.Name == "s01_liste_02") )
	
		and self.active
	
	

	return res
end