-- ----------------------------------------------------------------------------
layoutBedroomPhoneNumbers.mExits =
{
}
layoutBedroomPhoneNumbers.mPOIs = 
{
}
layoutBedroomPhoneNumbers.mHOs = 
{
}
layoutBedroomPhoneNumbers.mIOs =
{
	[objectPhonenumberPieceplaced0] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced1] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced2] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced3] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced4] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced5] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced6] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced7] = 
	{
		cursors = kCursorPointer,
	},
	[objectPhonenumberPieceplaced8] = 
	{
		cursors = kCursorPointer,
	},
    [objectBedroomPhonenumbersBackground] =
    {
		cursor = kCursorDefault,
		links = {objectPhonenumberPiecesGroup}
	},		
}
layoutBedroomPhoneNumbers.mGroups = 
{
	[objectPhonenumberPiecesGroup] =
	{
		children = {objectPhonenumberPiece0, objectPhonenumberPiece1, objectPhonenumberPiece2, objectPhonenumberPiece3,
					objectPhonenumberPiece4, objectPhonenumberPiece5, objectPhonenumberPiece6, objectPhonenumberPiece7,
					objectPhonenumberPiece8 },
		inventory = true,
		shoppinglist = false,
	},
}
layoutBedroomPhoneNumbers.mPiecesFind =
{ 
    objectPhonenumberPiece0,
    objectPhonenumberPiece1,
    objectPhonenumberPiece2,
    objectPhonenumberPiece3,
    objectPhonenumberPiece4,
    objectPhonenumberPiece5,
    objectPhonenumberPiece6,
    objectPhonenumberPiece7,
    objectPhonenumberPiece8,
}                                        
layoutBedroomPhoneNumbers.mPiecesPlace =
{
    objectPhonenumberPieceplaced0,
    objectPhonenumberPieceplaced1,
    objectPhonenumberPieceplaced2,
    objectPhonenumberPieceplaced3,
    objectPhonenumberPieceplaced4,
    objectPhonenumberPieceplaced5,
    objectPhonenumberPieceplaced6,
    objectPhonenumberPieceplaced7,
    objectPhonenumberPieceplaced8
}
layoutBedroomPhoneNumbers.mPiecesPlacePositions = {}
layoutBedroomPhoneNumbers.mPieceSelected    = nil
layoutBedroomPhoneNumbers.mPuzzle = true
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onConstruct()
	Layout.onConstruct(self)
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onInitialize()
	Layout.onInitialize(self)
	
	-- activate background so we can drop stuff on it
	objectBedroomPhonenumbersBackground:ss(kActive)
	
	-- store original location and set inactive
	for i, piece in pairs(self.mPiecesPlace) do
	    local x = piece:x()
	    local y = piece:y()
	    
	    self.mPiecesPlacePositions[piece] = { x=x, y=y }
	    
	    piece:ss(kInactive)
	end
	
	self:swapPieces(self.mPiecesPlace[1], self.mPiecesPlace[4])
	self:swapPieces(self.mPiecesPlace[2], self.mPiecesPlace[8])
	self:swapPieces(self.mPiecesPlace[3], self.mPiecesPlace[2])
	self:swapPieces(self.mPiecesPlace[4], self.mPiecesPlace[6])
	self:swapPieces(self.mPiecesPlace[5], self.mPiecesPlace[7])
	self:swapPieces(self.mPiecesPlace[9], self.mPiecesPlace[5])
	
	self.mPieceSelected = nil
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onSolve()
    Layout.onSolve(self)
    
    for i, piece in ipairs(self.mPiecesFind) do
        piece:ss(kDone)
    end
    
    for i, piece in ipairs(self.mPiecesPlace) do
        piece:ss(kIdle)
    end
    
    -- reset all pieces to correct position
    for i, piece in ipairs(self.mPiecesPlace) do
        local point = self.mPiecesPlacePositions[piece]
        
        piece:setX(point["x"])
        piece:setY(point["y"])
    end

end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onSolveStart()

    Layout.onSolveStart(self)
    self:animSolved()
    
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onSolveFinish()

    Layout.onSolveFinish(self)

	-- show the number near the phone
    objectPhonenumber:ss(kIdle)
    
    -- remove it here
    for i, piece in ipairs(self.mPiecesPlace) do
		piece:ss(kDone)
    end
    
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onMouseDown(x, y)
    
    local result = Layout.onMouseDown(self, x, y)
    
    if (self:isPuzzling()) then
        for i, piece in ipairs(self.mPiecesPlace) do
            if (piece:isInside(x, y)) then
                if (self.mPiecesFind[i]:isDone()) then
                    if (self:hasPieceSelected()) then
                        self:swapPieces(self.mPieceSelected, piece)
                        self:deselectPiece()
                        playAudioTrack("MOVE_PAPER")
                    
                        if (self:checkSolved()) then
                            self:solve()
                        end
                    else
                        self:selectPiece(piece)
                    end
                    
                	return true, false, false                    
                end
                
                break
            end
        end
    end
    
    return result
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:swapPieces(piece1, piece2)
    local x = piece1:x()
    local y = piece1:y()
    
    piece1:setX(piece2:x())
    piece1:setY(piece2:y()) 
    
    piece2:setX(x)
    piece2:setY(y)
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:checkSolved()
    
    for i, piece in ipairs(self.mPiecesFind) do
        if (not piece:isDone()) then
            return false
        end
    end
    
    for i, piece in ipairs(self.mPiecesPlace) do
        
        local point = self.mPiecesPlacePositions[piece]
    
        if (piece:x() ~= point["x"]) then
            return false
        end
        
        if (piece:y() ~= point["y"]) then
            return false
        end
    end
    
    return true
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:hasPieceSelected()
    return self.mPieceSelected ~= nil
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:selectPiece(piece)
    self.mPieceSelected = piece
    self.mPieceSelected:setColor( {alpha=1, red=0.74, green=0.45, blue=0.29 } )
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:deselectPiece()
    if (self:hasPieceSelected()) then
        self.mPieceSelected:setColor( {alpha=1, red=1, green=1, blue=1 } )
        self.mPieceSelected = nil
    end
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:animSolved()

    local delay = 0
    
    for waveIdx=1,2 do
        
        for i, piece in ipairs(self.mPiecesPlace) do
        
            piece:animate(
                    { delay=delay				},
                    { scale=1.05, duration=200	},
                    { scale=1,    duration=400	}
            )
            
            if (i % 3 == 0) then
                delay = delay + 200
            end
        end
        
        delay = delay + 400
    end

	layoutBedroomPhoneNumbers:animate("ClosePhone_blocking",
		{ delay = delay },
		{ call = layoutBedroomPhoneNumbers.hide },
		{ call = layoutBedroomPhoneNumbers.solveFinish }
	)	

end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onUsedAll(object, props)

	Layout.onUsedAll(self, object, props)
	
	if (object == objectBedroomPhonenumbersBackground) then
	    -- activate puzzle pieces
	    for i, piece in ipairs(self.mPiecesPlace) do
	        piece:ss(kActive)
	    end
	    -- find pieces to done
	    for i, piece in ipairs(self.mPiecesFind) do
	        piece:ss(kDone)
	    end	    
	end
end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:onQuickPlay()

	Layout.onQuickPlay(self)
	
    objectPhonenumberPiece0:ss(kFound)
    objectPhonenumberPiece1:ss(kFound)
    objectPhonenumberPiece2:ss(kFound)
    objectPhonenumberPiece3:ss(kFound)
    objectPhonenumberPiece4:ss(kFound)
    objectPhonenumberPiece5:ss(kFound)
    objectPhonenumberPiece6:ss(kFound)
    objectPhonenumberPiece7:ss(kFound)
    objectPhonenumberPiece8:ss(kFound)

end
-- ----------------------------------------------------------------------------
function layoutBedroomPhoneNumbers:isSkippable()
	return objectPhonenumberPiecesGroup:isUsed()
end