-- WARNING:
-- uses abstract layoutPennant puzzle!
-- ----------------------------------------------------------------------------
assert(LayoutPennant ~= nil)
setmetatable(layoutCryptHitchhiker, LayoutPennant)
-- ----------------------------------------------------------------------------
layoutCryptHitchhiker.mIOs =
{
	[objectBackgroundHhSliding] =
	{
		links = {objectHoHhSlidingGroup},
		cursor = kCursorDefault,
	}
}
layoutCryptHitchhiker.mHOs = 
{
	[objectHoSpecimen02] = 
	{
		siblings = {objectHoSpecimen02Lit, objectHoSpecimen02Pocket}
	},	
}
layoutCryptHitchhiker.mGroups =
{
	[objectHoHhSlidingGroup] =
	{
		children = {objectCryptHookmanLetterPiece, objectCryptHookmanLetterPiece02, objectCryptHookmanLetterPiece04,
					objectCryptHookmanLetterPiece08, objectCryptHoRotascopePieceCrypt, },
		inventory = true,
		shoppinglist = false,
	}
}
layoutCryptHitchhiker.mPuzzle = true
-- ----------------------------------------------------------------------------
layoutCryptHitchhiker.mPieces         =
{
    [objectIoHhSlidingPiece01] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	}, 
    	start 		= { col = 5, 	row = 1, 	}, 
    	solution 	= { col = 1, 	row = 1, 	required=true },
    },
    [objectIoHhSlidingPiece02] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 5, 	row = 2, 	},
    	solution 	= { col = 1, 	row = 2, 	required=true },
    },
    [objectIoHhSlidingPiece03] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 5, 	row = 3, 	},
    	solution 	= { col = 1, 	row = 3, 	required=true },
    },
    [objectIoHhSlidingPiece04] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 5, 	row = 4, 	},
    	solution 	= { col = 1, 	row = 4, 	required=true },
    },
    [objectIoHhSlidingPiece05] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 1, 	row = 1, 	},
    	solution 	= { col = 5, 	row = 1, 	required=true },
    },
    [objectIoHhSlidingPiece06] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1,	},
    	start 		= { col = 1, 	row = 2, 	},
    	solution 	= { col = 5, 	row = 2, 	required=true },
    },
    [objectIoHhSlidingPiece07] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	}, 
    	start 		= { col = 1, 	row = 3, 	},
    	solution 	= { col = 5, 	row = 3, 	required=true },
    },
    [objectIoHhSlidingPiece08] = 
    { 
    	dimensions 	= { cols = 1, 	rows = 1, 	}, 
    	start 		= { col = 1, 	row = 4, 	},
    	solution 	= { col = 5, 	row = 4, 	required=true },
    },
}        
layoutCryptHitchhiker.mSlots =
{
	settings = 
	{
    	parent 		= objectCryptHitchhikerBackground,
    	topLeftX 	= 110, 
    	topLeftY 	= 124, 
    	numRows 	= 4, 
    	numCols 	= 5, 
    	width 		= 68, 
    	height 		= 68,
    },
    blocked =
    {
    	{ col = 2, row = 1 },
    	{ col = 2, row = 2 },
    	{ col = 2, row = 4 },
    	{ col = 3, row = 1 },
    	{ col = 3, row = 4 },
    	{ col = 4, row = 1 },
    	{ col = 4, row = 2 },
    	{ col = 4, row = 4 },
    }
}
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onConstruct()
	
	LayoutPennant.onConstruct(self)
	LayoutPennant.setup(self, self.mSlots, self.mPieces, objectCryptHitchhikerReset)
	objectHoSpecimen02:ss(kInactive)

end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onInitialize()

	LayoutPennant.onInitialize(self)
	
	objectBackgroundHhSliding:ss(kActive)
	
	objectCryptHhOpen:ss(kInactive)
	
	objectCryptHhRedLight:ss(kIdle)
	objectCryptHhGreenLight:ss(kInactive)
	
	objectIoHhSlidingPiece01:ss(kIdle)
	objectIoHhSlidingPiece02:ss(kInactive)
	objectIoHhSlidingPiece03:ss(kIdle)
	objectIoHhSlidingPiece04:ss(kInactive)
	objectIoHhSlidingPiece05:ss(kInactive)
	objectIoHhSlidingPiece06:ss(kIdle)
	objectIoHhSlidingPiece07:ss(kInactive)
	objectIoHhSlidingPiece08:ss(kInactive)
	
	objectCryptHhLockTop:ss(kIdle)
	objectCryptHhLockBottom:ss(kIdle)
	objectCryptHhLockPin:ss(kIdle)
	
	objectCryptHitchhikerReset:ss(kIdle)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onUsedAll(object, props)

	LayoutPennant.onUsedAll(self, object, props)
	
	if (object == objectBackgroundHhSliding) then
		objectPoiHitchhikerPuzzle:ss(kInactive)
		objectPoiHitchhikerPuzzleComplete:ss(kActive)
		
		-- activate all sliding pieces
		for piece, settings in pairs(self.mPieces) do
			
			piece:ss(kActive)
			
		end
		
		objectCryptHitchhikerReset:ss(kActive)
		
		-- quick hack
		-- puzzle not finished
		-- self:solve()
	end

end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onShow()

	LayoutPennant.onShow(self)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onTick(time)

	LayoutPennant.onTick(self, time)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onSolveStart()

	LayoutPennant.onSolveStart(self)

end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onQuickPlay()
	objectHoHhSlidingGroup:ss(kFound)
end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:animSolved()
	objectCryptHhGreenLight:ss(kIdle)
	objectCryptHhRedLight:ss(kDone)
	objectCryptHhLockPin:animate("MovePinCryptHhOpen",
		{ delay = 2000 },
	    { y=objectCryptHhLockPin:y() + 100, duration=1000, style="easeInOut" }
	)
    objectCryptHhOpen:ss(kIdle)
	objectCryptHhOpen:setAlpha(0)
	objectCryptHhOpen:animate("FadeInCryptHhOpen",
		{ delay = 3500 },
		{ alpha = 1, duration = 1000 }
	)
	
	local fadeInObjects = 
	{
		-- TODO add fade in objects
		objectHoSpecimen02, objectHoSpecimen02Lit, objectHoSpecimen02Pocket,
	}
	
	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("SolveFinishCryptHh_blocking",
		{ delay = 2000 },
		{ command="playTrack track=UNLOCKED", target=self },
		{ delay = 3000 },
		{ call = layoutCryptHitchhiker.solveFinish }
	)	
end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:onSolveFinish()

	LayoutPennant.onSolveFinish(self)
	
	objectPoiHitchhikerPuzzle:ss(kDone)
	objectPoiHitchhikerPuzzleComplete:ss(kDone)
	objectPoiHitchhikerPuzzleOpen:ss(kActive)

	play(movieSettings[kMovieGhost])

end
-- ----------------------------------------------------------------------------
function layoutCryptHitchhiker:isSkippable()
	return objectHoHhSlidingGroup:isUsed()
end