-- WARNING:
-- uses abstract layoutRotascope puzzle!
-- ----------------------------------------------------------------------------
assert(LayoutRotascope ~= nil)
setmetatable(layoutCryptRotascope, LayoutRotascope)
-- ----------------------------------------------------------------------------
layoutCryptRotascope.mIOs =
{
	[objectBackgroundRotascopeCrypt] =
	{
		links = {objectHoRotascopePiece01},
		cursor = kCursorDefault,
	}
}
layoutCryptRotascope.mHOs = 
{
	[objectCryptHoRotascopePieceCrypt] = 
	{
		siblings = {objectCryptHoRotascopePieceCryptLit, objectCryptHoRotascopePieceCryptPocket},
	},
	[objectCryptHookmanLetterPiece04] = 
	{
		siblings = {objectCryptHookmanLetterPiece04Lit, objectCryptHookmanLetterPiece04Pocket},
	},		
}
-- ----------------------------------------------------------------------------
layoutCryptRotascope.mMissingPiece = nil
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onConstruct()

	local pivots = 
	{
		objectCryptIoRotateAreaCenter, objectCryptIoRotateAreaLeft, objectCryptIoRotateAreaRight,
		objectCryptIoRotateAreaTop,
	}
	
	local elipses = 
	{
		objectCryptRotascopePiece01, objectCryptRotascopePiece02, objectCryptRotascopePiece03, 
		objectCryptRotascopePiece04, objectCryptRotascopePiece05, objectCryptRotascopePiece06, 
		objectCryptRotascopePiece07, objectCryptRotascopePiece08, objectCryptRotascopePiece09,	
	}
	
	local pins = 
	{
		objectCryptTurningScrewCenter, objectCryptTurningScrewLeft, objectCryptTurningScrewRight,
		objectCryptTurningScrewTop,
	}
	
	LayoutRotascope.onConstruct(self)
	LayoutRotascope.setup(self, pivots, elipses, pins, objectCryptRotascopeReset)
	self.mColor = false

	objectCryptHoRotascopePieceCrypt:ss(kInactive)
	objectCryptHookmanLetterPiece04:ss(kInactive)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onInitialize()

	LayoutRotascope.onInitialize(self)
	
	objectCryptRotascopeOpen:ss(kInactive)
	objectCryptGreenLight:ss(kInactive)
	
	-- ok!
	-- this puzzle is missing a piece
	self.mMissingPiece = objectCryptRotascopePiece05
	self.mMissingPiece:ss(kInactive)
	
	-- disable input on pivots
	for i, pivot in ipairs(self.mPivots) do
		pivot:ss(kIdle)
	end	
	
	-- disable input on reset
	objectCryptRotascopeReset:ss(kIdle)
	
	-- activate the background to drag on
	objectBackgroundRotascopeCrypt:ss(kActive)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onShow()

	LayoutRotascope.onShow(self)

	if (self:isSolved()) then
		if (objectCryptHoRotascopePieceCrypt:isHidden()) then
			objectCryptHoRotascopePieceCrypt:ss(kActive)
		end	
		if (objectCryptHookmanLetterPiece04:isHidden()) then
			objectCryptHookmanLetterPiece04:ss(kActive)
		end
	end

end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onQuickPlay()

	objectHoRotascopePiece01:ss(kFound)

end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onSolve()

	LayoutRotascope.onSolve(self)

	objectBackgroundRotascopeCrypt:ss(kIdle)
	objectCryptRedLight:ss(kDone)
	objectCryptGreenLight:ss(kActive)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onSolveStart()

	LayoutRotascope.onSolveStart(self)

	self.mMissingPiece:ss(kActive)
	
	objectCryptLockPin:animate("MovePin",
		{ delay = 2000 },
		{ dx = 0, dy = 45, duration = 1000, style="easeInOut" }
	)
	
	objectCryptRotascopeOpen:ss(kIdle)
	objectCryptRotascopeOpen:setAlpha(0)
	objectCryptRotascopeOpen:animate("FadeInRotascopeOpen",
		{ delay = 3500 },
		{ alpha = 1, duration = 1000 }
	)

	local fadeInObjects = 
	{
		objectCryptHoRotascopePieceCrypt,objectCryptHoRotascopePieceCryptLit, objectCryptHoRotascopePieceCryptPocket,
		objectCryptHookmanLetterPiece04, objectCryptHookmanLetterPiece04Lit, objectCryptHookmanLetterPiece04Pocket,
	}
	
	for i, object in ipairs(fadeInObjects) do
		if (object:isInactive()) then
			object:ss(kIdle)
		end
		if (object:isHidden()) then
			object:ss(kActive)
		end
		object:setAlpha(0)
		object:animate("FadeIn" .. object:name(),
			{ delay = 3500 },
			{ alpha = 1, duration = 1000 }
		)			
	end	

	self:animate("BlockCryptRotascope_blocking",
		{ delay = 2000 },
		{ command="playTrack track=UNLOCKED", target=self },
		{ delay = 3000 },
		{ call = layoutCryptRotascope.solveFinish }
	)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onAnimationFinish(animationName)

	LayoutRotascope.onAnimationFinish(self, animationName)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onUsedAll(object, props)

	LayoutRotascope.onUsedAll(self, object, props)

	if (object == objectBackgroundRotascopeCrypt) then
		-- show missing piece
		self.mMissingPiece:ss(kActive)
		
		-- enable input on pivots
		for i, pivot in ipairs(self.mPivots) do
			pivot:ss(kActive)
		end
		
		-- enable input on reset
		objectCryptRotascopeReset:ss(kActive)
	end

end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:onSolveFinish()

	LayoutRotascope.onSolveFinish(self)
	
	objectPoiRotascopeCryptClosed:ss(kDone)
	objectPoiRotascopeCryptOpen:ss(kActive)
	
	think("MESSAGE_OPEN_CRYPT_ROTASCOPE")

end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:isSkippable()
	return objectHoRotascopePiece01:isUsed()
end
-- ----------------------------------------------------------------------------
function layoutCryptRotascope:playRotateSound()
	playAudioTrack("ROTATE_DISC")
end