-- WARNING:
-- uses abstract layoutPennant puzzle!
-- ----------------------------------------------------------------------------
assert(LayoutPennant ~= nil)
setmetatable(layoutCryptHookman, LayoutPennant)
-- ----------------------------------------------------------------------------
layoutCryptHookman.mExits =
{
}
layoutCryptHookman.mPOIs = 
{
}
layoutCryptHookman.mHOs = 
{
	[objectCryptHookmanLetterPiece] = 
	{
		siblings = {objectCryptHookmanLetterPieceLit, objectCryptHookmanLetterPiecePocket},
	},
	[objectCryptHookmanLetterPiece08] = 
	{
		siblings = {objectCryptHookmanLetterPiece08Lit, objectCryptHookmanLetterPiece08Pocket},
	},	
}
layoutCryptHookman.mIOs =
{
    [objectCryptHookmanBackground] =
    {
		cursor = kCursorDefault,
		links = {objectHoHmSlidingGroup}
	},
}
-- ----------------------------------------------------------------------------
layoutCryptHookman.mBigPieceParent = objectCryptHookmanBackground
layoutCryptHookman.mBigPiece       = objectCryptHookmanPiece9
layoutCryptHookman.mBigPieceCopy   = nil
layoutCryptHookman.mPieces         =
{
    [objectCryptHookmanPiece1] = 
    { 
    	-- 5
    	dimensions 	= { cols = 1, 	rows = 2, 	}, 
    	start 		= { col = 1, 	row = 2, 	}, 
    	solution 	= { col = 1, 	row = 1, 	required=false },
    },
    [objectCryptHookmanPiece2] = 
    { 
     	-- 6
    	dimensions 	= { cols = 1, 	rows = 2, 	},
    	start 		= { col = 2, 	row = 2, 	},
    	solution 	= { col = 2, 	row = 1, 	required=false },
    },
    [objectCryptHookmanPiece3] = 
    { 
    	 -- 7
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 1, 	row = 4, 	},
    	solution 	= { col = 4, 	row = 3, 	required=false },
    },
    [objectCryptHookmanPiece4] = 
    { 
    	-- 8
    	dimensions 	= { cols = 1, 	rows = 1, 	},
    	start 		= { col = 2, 	row = 4, 	},
    	solution 	= { col = 3, 	row = 3, 	required=false },
    },
    [objectCryptHookmanPiece5] = 
    { 
     	-- 4
    	dimensions 	= { cols = 2, 	rows = 1, 	},
    	start 		= { col = 3, 	row = 4, 	},
    	solution 	= { col = 3, 	row = 4, 	required=false },
    },
    [objectCryptHookmanPiece6] = 
    { 
    	-- 3
    	dimensions 	= { cols = 2, 	rows = 1,	},
    	start 		= { col = 3, 	row = 5, 	},
    	solution 	= { col = 3, 	row = 5, 	required=false },
    },
    [objectCryptHookmanPiece7] = 
    { 
    	-- 2
    	dimensions 	= { cols = 2, 	rows = 1, 	}, 
    	start 		= { col = 3, 	row = 1, 	},
    	solution 	= { col = 3, 	row = 1, 	required=false },
    },
    [objectCryptHookmanPiece8] = 
    { 
    	-- 1
    	dimensions 	= { cols = 2, 	rows = 1, 	}, 
    	start 		= { col = 1, 	row = 1, 	},
    	solution 	= { col = 3, 	row = 2, 	required=false },
    },
    [objectCryptHookmanPiece9] = 
    { 
     	-- A
    	dimensions 	= { cols = 2, 	rows = 2, 	},
    	start 		= { col = 3, 	row = 2, 	},
    	solution 	= { col = 1, 	row = 4, 	required=true  },
    },
}           
layoutCryptHookman.mSlots =
{
	settings = 
	{
    	parent 		= objectCryptHookmanBackground,
    	topLeftX 	= 364, 
    	topLeftY 	= 84, 
    	numRows 	= 5, 
    	numCols 	= 4, 
    	width 		= 68, 
    	height 		= 68,
    }
}
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onConstruct()
 	LayoutPennant.onConstruct(self)	
    LayoutPennant.setup(self, self.mSlots, self.mPieces, objectCryptHookmanReset)
	objectCryptHookmanLetterPiece:ss(kInactive)    
	objectCryptHookmanLetterPiece08:ss(kInactive)
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onInitialize()
	LayoutPennant.onInitialize(self)
	objectCryptHookmanOpen:ss(kInactive)
	objectCryptHookmanGreenLight:ss(kInactive)
	objectCryptHookmanBackground:ss(kActive)
	objectCryptHookmanReset:ss(kIdle)
	for piece, settings in pairs(self.mPieces) do
		piece:ss(kInactive)
	end
	self.mBigPiece:ss(kActive)
	self:initBigPieceCopy()
	self.mBigPiece:setVisible(false)
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onShow()
	LayoutPennant.onShow(self)
	if (self:isSolved()) then	
		if (objectCryptHookmanLetterPiece:isHidden()) then
			objectCryptHookmanLetterPiece:ss(kActive)
		end
		if (objectCryptHookmanLetterPiece08:isHidden()) then
			objectCryptHookmanLetterPiece08:ss(kActive)
		end		
	end
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onUsedAll(object, props)
	LayoutPennant.onUsedAll(self, object, props)
	if (object == objectCryptHookmanBackground) then
	    -- activate puzzle pieces
	    for piece, settings in pairs(self.mPieces) do
	        piece:ss(kActive)
	    end
	    -- activate reset button
	    objectCryptHookmanReset:ss(kActive)
	    -- change crypt
		objectPoiHookmanPuzzleClosed:ss(kDone)
		objectPoiHookmanPuzzleClosedFull:ss(kActive)		    
	end
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onQuickPlay()
	
	LayoutPennant.onQuickPlay(self)
	
	objectHoHmSlidingGroup:ss(kFound)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onSolveStart()
	
	LayoutPennant.onSolveStart(self)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onMouseMoved(x, y)
	LayoutPennant.onMouseMoved(self, x, y)
	self:updateBigPieceCopy()
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:setPiece(piece, slot)
	LayoutPennant.setPiece(self, piece, slot)
	self:updateBigPieceCopy()
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:initBigPieceCopy()
    -- creat a copy of big piece
    self.mBigPieceCopy = self.mBigPieceParent:newChildSprite( { name = "SPRITE_BIGPIECECOPY", x=0, y=0, image=self.mBigPiece:getImageName(), section=self.mBigPiece:getSectionName() } )
    self.mBigPieceCopy.globalX = self.globalX
    self.mBigPieceCopy.globalY = self.globalY
    self.mBigPieceCopy.localX  = self.localX
    self.mBigPieceCopy.localY  = self.localY
    self:updateBigPieceCopy()
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:updateBigPieceCopy()
    if (self.mBigPieceCopy ~= nil) then
        local x = self.mBigPiece:globalX(0, 0)
        local y = self.mBigPiece:globalY(0, 0)
        self.mBigPieceCopy:setX(self.mBigPieceParent:localX(x, y))
        self.mBigPieceCopy:setY(self.mBigPieceParent:localY(x, y))
    end
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:animSolved()
	objectCryptHookmanGreenLight:ss(kIdle)
	objectCryptHookmanRedLight:ss(kDone)
	objectCryptHookmanLockpin:animate("MovePinCryptHookmanOpen",
		{ delay = 2000 },
	    { x=objectCryptHookmanLockpin:x() + 100, duration=1000, style="easeInOut" }
	)
    objectCryptHookmanOpen:ss(kIdle)
	objectCryptHookmanOpen:setAlpha(0)	
	objectCryptHookmanOpen:animate("FadeInCryptHookmanOpen",
		{ delay = 3500 },
		{ alpha = 1, duration = 1000 }
	)
	
	local fadeInObjects = 
	{
		objectCryptHookmanLetterPiece,objectCryptHookmanLetterPieceLit, objectCryptHookmanLetterPiecePocket,
		objectCryptHookmanLetterPiece08, objectCryptHookmanLetterPiece08Lit, objectCryptHookmanLetterPiece08Pocket,
	}
	
	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("SolveFinishCryptHookman_blocking",
		{ delay = 2000 },
		{ command="playTrack track=UNLOCKED", target=self },
		{ delay = 3000 },
		{ call = layoutCryptHookman.solveFinish }
	)	
end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:onSolveFinish()

	LayoutPennant.onSolveFinish(self)
	
	objectPoiHookmanPuzzleClosed:ss(kDone)
	objectPoiHookmanPuzzleClosedFull:ss(kDone)	
	objectPoiHookmanPuzzleOpen:ss(kActive)
	
	think("MESSAGE_OPEN_CRYPT_HOOKMAN")

end
-- ----------------------------------------------------------------------------
function layoutCryptHookman:isSkippable()
	return objectHoHmSlidingGroup:isUsed()
end