
.namespace s09_c02

function OnEnable()
	Closeup.OnEnable("MiniGame")
	Reset()
	if DebugOptions.DebugMode == true then
		InventoryManager.AddInInventory("InventoryClefs")
	end
end

function Reset()
	tabposX = {1040,1040,1040,1040,1040,1040}
	tabposY = {660,560,430,320,210,130}
	tabindicepos = {1,2,3,4,5,6}
	tabpiston ={1,2,3,2,3,4,1,2}
	tabclef1 = {2,4,3,5,3,4,4,1}
	tabclef2 = {2,4,3,4,2,3,4,2}
	tabclef3 = {1,2,3,2,3,4,1,2}
	tabclef4 = {2,4,3,4,2,2,4,1}
	tabclef5 = {4,2,3,2,3,4,2,4}
	tabclef6 = {3,4,2,5,3,1,4,2}
	goupille1.SetFrame(0)
	goupille2.SetFrame(0)
	goupille3.SetFrame(0)
	goupille4.SetFrame(0)
	goupille5.SetFrame(0)
	goupille6.SetFrame(0)
	goupille7.SetFrame(0)
	goupille8.SetFrame(0)

	for y=1,6 do
		clefpos = OPUnitFactory.GetUnit("clef"..tostring(y))
		clefpos.Position_X=1040
		clefpos.Position_Y=tabposY[y]
	end

	pistonposiniy = 461
	
	goupille1.Position_Y = pistonposiniy
	goupille2.Position_Y = pistonposiniy
	goupille3.Position_Y = pistonposiniy
	goupille4.Position_Y = pistonposiniy
	goupille5.Position_Y = pistonposiniy
	goupille6.Position_Y = pistonposiniy
	goupille7.Position_Y = pistonposiniy
	goupille8.Position_Y = pistonposiniy
	
	ecartdent = 6
	tailletab = 8
	n=6
	j=1
	itabtrans=8
	verif=0
	cleftenue=0
	clefaenfonce=0
	transtart=0
	tentatives=0
	cond1=0
	cond2=0
	tempsquipasse=0
	tempsquipasse2=0
	transgo=true -- bool pour savoir si la clé est en translation.
end

function OnUpdate()
		
	--OPLog.Log(" tabindicepos : "..tabindicepos[1].." "..tabindicepos[2].." "..tabindicepos[3].." "..tabindicepos[4].." "..tabindicepos[5].." "..tabindicepos[6])
	-- OPLog.Log("tentatives: "..tentatives)
	-- OPLog.Log("n: "..n)
	--OPLog.Log("verif : "..verif)
	
	if cleftenue>0 then
		ct = OPUnitFactory.GetUnit("clef"..tostring(cleftenue))
	end
	if clefaenfonce>0 then
		dt = OPUnitFactory.GetUnit("clef"..tostring(clefaenfonce))
	end
	
	-- if cleftenue>0 then -- la clefs suit le curseur
		-- cursorpos = GetCenter(Cursor)
		-- ct.Position_X=cursorpos.x - 180
		-- ct.Position_Y=cursorpos.y - 40
	-- end
	
	if transtart==1  then -- point de depart de la translation 
		dt.Position_X = 486 
		dt.Position_Y = 444
		if cond2==0 then
			cond2=1
		end
	end
	
	if cond2==1 then
		tempsdepart2 = OPTime.GetTime()
		cond2 = 2
	end
		
	if cond2==2 then
		tempsquipasse2 = OPTime.GetTime() - tempsdepart2
		-- OPLog.Log(" tempsquipasse "..tempsquipasse2)
	end
	
	if tempsquipasse2 > 1 then
		transtart=2
		tempsdepart2=0
		tempsquipasse2=0
		cond2=0
	end
	
	if transtart==2  then -- la translation commence

		if  dt.Position_X ~= 350 and transgo then
			dt.Position_X = dt.Position_X - 2
		end
		
			
		if dt.Position_X <= 460 then
			goupille1.Position_Y = pistonposiniy - (tabcleprise[itabtrans]*ecartdent) 
			if tabcleprise[itabtrans] == tabpiston[itabtrans] then
				goupille1.SetFrame(2)
			elseif tabcleprise[itabtrans] < tabpiston[itabtrans] or tabcleprise[itabtrans] > tabpiston[itabtrans] then
				goupille1.SetFrame(1)
			end
			
			
		end
		
		if dt.Position_X <= 445 then
			if itabtrans == 8 then
				itabtrans=7
			end
			goupille2.Position_Y = pistonposiniy - (tabcleprise[itabtrans+1]*ecartdent)
			if tabcleprise[itabtrans+1] == tabpiston[itabtrans+1] then
				goupille2.SetFrame(2)
			elseif tabcleprise[itabtrans+1] < tabpiston[itabtrans+1] or tabcleprise[itabtrans+1] > tabpiston[itabtrans+1] then
				goupille2.SetFrame(1)
			end
		end
		
		if dt.Position_X <= 430 then
			if itabtrans == 7 then
				itabtrans=6
			end
			goupille3.Position_Y = pistonposiniy - (tabcleprise[itabtrans+2]*ecartdent) 
			if tabcleprise[itabtrans+2] == tabpiston[itabtrans+2] then
				goupille3.SetFrame(2)
			elseif tabcleprise[itabtrans+2] < tabpiston[itabtrans+2] or tabcleprise[itabtrans+2] > tabpiston[itabtrans+2] then
				goupille3.SetFrame(1)
			end
		end
	
		if dt.Position_X <= 417 then
			if itabtrans == 6 then
				itabtrans=5
			end
			goupille4.Position_Y = pistonposiniy - (tabcleprise[itabtrans+3]*ecartdent) 
			if tabcleprise[itabtrans+3] == tabpiston[itabtrans+3] then
				goupille4.SetFrame(2)
			elseif tabcleprise[itabtrans+3] < tabpiston[itabtrans+3] or tabcleprise[itabtrans+3] > tabpiston[itabtrans+3] then
				goupille4.SetFrame(1)
			end
		end
		
		if dt.Position_X <= 400 then
			if itabtrans == 5 then
				itabtrans=4
			end
			goupille5.Position_Y = pistonposiniy - (tabcleprise[itabtrans+4]*ecartdent) 
			if tabcleprise[itabtrans+4] == tabpiston[itabtrans+4] then
				goupille5.SetFrame(2)
			elseif tabcleprise[itabtrans+4] < tabpiston[itabtrans+4] or tabcleprise[itabtrans+4] > tabpiston[itabtrans+4] then
				goupille5.SetFrame(1)
			end
		end
		
		if dt.Position_X <= 387 then
			if itabtrans == 4 then
				itabtrans=3
			end
			goupille6.Position_Y = pistonposiniy - (tabcleprise[itabtrans+5]*ecartdent)
			if tabcleprise[itabtrans+5] == tabpiston[itabtrans+5] then
				goupille6.SetFrame(2)
			elseif tabcleprise[itabtrans+5] < tabpiston[itabtrans+5] or tabcleprise[itabtrans+5] > tabpiston[itabtrans+5] then
				goupille6.SetFrame(1)
			end
		end
	
		if dt.Position_X <= 389 then
			if itabtrans == 3 then
				itabtrans=2
			end
			goupille7.Position_Y = pistonposiniy - (tabcleprise[itabtrans+6]*ecartdent)
			if tabcleprise[itabtrans+6] == tabpiston[itabtrans+6] then
				goupille7.SetFrame(2)
			elseif tabcleprise[itabtrans+6] < tabpiston[itabtrans+6] or tabcleprise[itabtrans+6] > tabpiston[itabtrans+6] then
				goupille7.SetFrame(1)
			end
		end
		
		if dt.Position_X <= 372 then
			if itabtrans == 2 then
				itabtrans=1
			end
			goupille8.Position_Y = pistonposiniy - (tabcleprise[itabtrans+7]*ecartdent) 
			if tabcleprise[itabtrans+7] == tabpiston[itabtrans+7] then
				goupille8.SetFrame(2)
			elseif tabcleprise[itabtrans+7] < tabpiston[itabtrans+7] or tabcleprise[itabtrans+7] > tabpiston[itabtrans+7] then
				goupille8.SetFrame(1)
			end
		end
		
		if dt.Position_X == 350 then
			transgo=false
		end
		
		if transgo==false then
			if tabcleprise[j]==tabpiston[j] and j <= tailletab then
				verif = verif + 1
				-- gt = OPUnitFactory.GetUnit("goupille"..tostring(j))
				-- gt.Position_Y = gt.Position_Y - (tabcleprise[j]*3) 
				j = j + 1 
			elseif tabcleprise[j]~=tabpiston[j] then
				--OPLog.Log(" Mauvaise CLE ")		
				if cond1 == 0 then
					tempsdepart = OPTime.GetTime()
					cond1 = 1
				end
			end	
		end
			
		if cond1 == 1 then
			tempsquipasse = OPTime.GetTime() - tempsdepart
			--OPLog.Log(" tempsquipasse "..tempsquipasse)
		end
		
		if tempsquipasse > 3  then
			
			dt.Position_X = tabposX[tabindicepos[clefaenfonce]] 
			dt.Position_Y = tabposY[tabindicepos[clefaenfonce]]
			goupille1.Position_Y = pistonposiniy
			goupille2.Position_Y = pistonposiniy
			goupille3.Position_Y = pistonposiniy
			goupille4.Position_Y = pistonposiniy
			goupille5.Position_Y = pistonposiniy
			goupille6.Position_Y = pistonposiniy
			goupille7.Position_Y = pistonposiniy
			goupille8.Position_Y = pistonposiniy
			goupille1.SetFrame(0)
			goupille2.SetFrame(0)
			goupille3.SetFrame(0)
			goupille4.SetFrame(0)
			goupille5.SetFrame(0)
			goupille6.SetFrame(0)
			goupille7.SetFrame(0)
			goupille8.SetFrame(0)
			itabtrans = 8
			clefaenfonce = 0
			transtart = 0
			tentatives = tentatives + 1
			PlaySoundEffect("gmp_badkey")
			cond1=0
			tempsquipasse=0
			transgo=true
			
			if tentatives == 1 then
				SequencePlayer.PlaySequence("GP_906",nil,nil)
			end
			
			if tentatives == 2 then
				SequencePlayer.PlaySequence("GP_907",nil,nil)
			end
			
			if tentatives == 3 then
				SequencePlayer.PlaySequence("GP_908",nil,nil)
			end
		end
		
		if verif == tailletab  then
			Itsawin()
			verif=0
		end
		
	
	end
	
	
	
	if tentatives >= 3 then  -- melange les clefs apres 3 tentatives
		PlaySoundEffect("int_closeupfail")
		if n > 2 then 
			--n is now the last pertinent index
			local k = math.random(n) -- 1 <= k <= n
			--Quick swap
			a = tabindicepos[n]
			tabindicepos[n] = tabindicepos[k]
			tabindicepos[k] = a
			n = n - 1
		elseif n==2 then
			tentatives = 0
			clef1.Position_X = tabposX[tabindicepos[1]] 
			clef1.Position_Y = tabposY[tabindicepos[1]]
			clef2.Position_X = tabposX[tabindicepos[2]] 
			clef2.Position_Y = tabposY[tabindicepos[2]]
			clef3.Position_X = tabposX[tabindicepos[3]] 
			clef3.Position_Y = tabposY[tabindicepos[3]]
			clef4.Position_X = tabposX[tabindicepos[4]] 
			clef4.Position_Y = tabposY[tabindicepos[4]]
			clef5.Position_X = tabposX[tabindicepos[5]] 
			clef5.Position_Y = tabposY[tabindicepos[5]]
			clef6.Position_X = tabposX[tabindicepos[6]] 
			clef6.Position_Y = tabposY[tabindicepos[6]]
			n = 6
		end
		
	end

	
end

function Itsawin() 
	SequencePlayer.PlaySequence("GP_910",self,nil)
end



function OnEnter() 
	SequencePlayer.Enabled = true
	
	if InventoryClefs.Equipped == false and SampleLevel.IsEqual("s09_c02_clefs",false) then		
		SequencePlayer.PlaySequence("GP_912",self,nil)
		clef1.Enabled=false
		clef2.Enabled=false
		clef3.Enabled=false
		clef4.Enabled=false
		clef5.Enabled=false
		clef6.Enabled=false
		gamestart=false
		return true
	elseif InventoryClefs.Equipped == false and SampleLevel.IsEqual("s09_c02_clefs",true) then
		SequencePlayer.PlaySequence("GP_913",self,nil)
		gamestart=true
		clefsBegin_hotspot.Enabled=false
		clef1.Enabled=true
		clef2.Enabled=true
		clef3.Enabled=true
		clef4.Enabled=true
		clef5.Enabled=true
		clef6.Enabled=true
	elseif InventoryClefs.Equipped == true and SampleLevel.IsEqual("s09_c02_clefs",false) then
		SequencePlayer.PlaySequence("GP_913",self,nil)
	end
end

function OnSequenceEnd(dialogue)
	if dialogue == "GP_912" then
		NavigationManager.SwitchTo(nil,"s09_SearchSite")
	end
	if dialogue == "GP_910" then
		s09_c02.Enabled = false
		NavigationManager.SwitchTo(nil,"s10_SearchSite")
		PlaySoundEffect("gmp_opendoor")
	end
	if dialogue == "AI_s09_c02" then
		SequencePlayer.Enabled = true
		return true
	end
end

function OnPickObject(objname)

end

function OnSearchEnd(typesearch)

end

function CondSpecificToFinish() 

end