-- WARNING:
-- uses abstract LayoutJigsaw puzzle!
-- ----------------------------------------------------------------------------
assert(LayoutJigsaw ~= nil)
setmetatable(layoutBoathouseCardJigsaw, LayoutJigsaw)
-- ----------------------------------------------------------------------------
layoutBoathouseCardJigsaw.mGroups =
{
	[objectHoCardPiecesGroup] =
	{
		children = {objectHoCardPiece02, objectHoCardPiece03, objectHoCardPiece04,
					objectHoCardPiece05, objectHoCardPiece06, objectHoCardPiece07,
					objectHoCardPiece08, objectHoCardPiece09},
		inventory = true,
		shoppinglist = false,
	}
}
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onConstruct()

	local pieces = 
	{
		objectIoCardPiece01, objectIoCardPiece02, objectIoCardPiece03, 
		objectIoCardPiece04, objectIoCardPiece05, objectIoCardPiece06, 
		objectIoCardPiece07, objectIoCardPiece08, objectIoCardPiece09,	
	}	

	LayoutJigsaw.onConstruct(self)
	LayoutJigsaw.setup(self, pieces, nil, nil, nil, objectHoCardPiecesGroup, objectBackgroundBoathouseCardJigsaw, objectIoCardJigsawRotateButton)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onInitialize()

	LayoutJigsaw.onInitialize(self)
	
	objectBackgroundBoathouseCardJigsaw:ss(kIdle)
	objectCardJigsawSolved:ss(kInactive)
	objectIoCardPiece01:ss(kIdle)
	
	-- hide tape
	for i = 1, #(self.mPieces) do
		for j = 1, #(self.mPieces) do
			local varName = "objectTape" .. i .. "to" .. j
			local tape = _G[varName]
			if (tape) then
				tape:ss(kInactive)
			end
		end
	end	
	
	-- set initial
	for i, piece in ipairs(self.mPieces) do
		piece:relocatePivotPoint("center")
	end
	
	objectIoCardPiece01:setRotation(0)
	objectIoCardPiece02:setRotation(math.pi)
	objectIoCardPiece03:setRotation(0)
	objectIoCardPiece04:setRotation(math.pi * 1.5)
	objectIoCardPiece05:setRotation(math.pi * 1.5)
	objectIoCardPiece06:setRotation(math.pi * 0.5)
	objectIoCardPiece07:setRotation(math.pi * 1.5)
	objectIoCardPiece08:setRotation(0)
	objectIoCardPiece09:setRotation(math.pi * 0.5)
	
	for i, piece in ipairs(self.mPieces) do
		piece:relocatePivotPoint("topleft")
	end		
	
	objectIoCardPiece01:setX(55)
	objectIoCardPiece01:setY(99)	
	objectIoCardPiece02:setX(431)
	objectIoCardPiece02:setY(466)	
	objectIoCardPiece03:setX(426)
	objectIoCardPiece03:setY(291)
	objectIoCardPiece04:setX(388)
	objectIoCardPiece04:setY(120)
	objectIoCardPiece05:setX(501)
	objectIoCardPiece05:setY(286)	
	objectIoCardPiece06:setX(283)
	objectIoCardPiece06:setY(136)		
	objectIoCardPiece07:setX(370)
	objectIoCardPiece07:setY(254)
	objectIoCardPiece08:setX(241)
	objectIoCardPiece08:setY(49)	
	objectIoCardPiece09:setX(462)
	objectIoCardPiece09:setY(192)														
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onSolve()

	LayoutJigsaw.onSolve(self)

	objectCardJigsawSolved:ss(kIdle)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onSolveStart()

	LayoutJigsaw.onSolveStart(self)

	self:animate("CloseCardJigsaw_blocking",
		{ delay = 200 },
		{ call = layoutBoathouseCardJigsaw.hide },
		{ delay = 500 },
		{ call = function() layoutBoathouseEnigma:animCardJigsaw() end },
		{ delay = layoutBoathouseEnigma.kAnimCardJigsawDelay },
		{ call = layoutBoathouseCardJigsaw.solveFinish }
	)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:isSkippable()
	return objectHoCardPiecesGroup:isUsed()
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onQuickPlay()
	objectHoCardPiecesGroup:ss(kFound)
	objectBackgroundBoathouseCardJigsaw:ss(kActive)
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:onMouseDown(x, y)

	local r1, r2, r3 = LayoutJigsaw.onMouseDown(self, x, y)
	
	-- place tape
	for i = 1, #(self.mPieces) do
		for j = 1, #(self.mPieces) do
			local varName = "objectTape" .. i .. "to" .. j
			local tape = _G[varName]
			if (tape) then
				if (tape:isInactive()) then
					if (self.mPieces[i]:isIdle() and self.mPieces[j]:isIdle()) then
						tape:ss(kIdle)
					end			
				end
			end
		end
	end
	
	return r1, r2, r3

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:playPlacedSound()
	playAudioTrack("MANUAL_PAGE")
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:playConnectSound()
	playAudioTrack("MANUAL_PAGE")
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardJigsaw:updateOrder()
	
	LayoutJigsaw.updateOrder(self)
	
	-- update tape order
	for i, piece1 in ipairs(self.mPieces) do
		for j, piece2 in ipairs(self.mPieces) do
			local varName = "objectTape" .. i .. "to" .. j
			local tape = _G[varName]
			if (tape and tape:isIdle()) then
				if (isSiblingAfter(piece1, piece2)) then
					tape:becomeChildOf(piece2)
				else
					tape:becomeChildOf(piece1)
				end				
			end
		end
	end
	
end