    -------------------------------------------------------------------------------
--  Script for mini-game "puzzle"
-------------------------------------------------------------------------------
require "common"

-- constants
local c_SnapDist = 20
local c_MoveBias = 5

-- initialization
local objPieces  = obj( "pieces" )
local tPieces = children( objPieces )
local tBgPieces = children( obj( "bgpieces" ) )
local objCurPiece = nil
local bFinished = false
local bPieceMoved = false
local pivotX, pivotY = 0, 0
local startX, startY = 0, 0
connectObjects( tPieces, "onlbdown", "OnPiecePicked" )
connectObjects( tPieces, "onlbup", "OnPieceUp" )
setObjects( tPieces, "enabled", true )
set ( "Hint",   "enabled", "false")
set ( objPieces, "visible", "false" )
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
set ( "TutorialSwitch", "activeidx", "6" )
wait ( 0.5 )
for i = 1, 30 do
    wait (0.15)
    set ( i, "fgcolor", "0xffffffff" )
end

wait (5)

for i = 1, 30 do
    wait (0.15)
    set ( i, "fgcolor", "0xff222222" )
end

set ( objPieces, "visible", "true" )

function OnPieceUp( piece )
	if (objCurPiece ~= nil and bPieceMoved) then
		OnPiecePicked( piece )
	end
end

function FindBgPiece( piece )
    call( "sndPuzzle", "play" )
    local pieceIdx = get( piece, "skinframe" )
    for i, p in ipairs( tBgPieces ) do
        if (get( p, "skinframe" ) == pieceIdx) then
            return p
        end
    end
    return nil
end

function OnPiecePicked( piece )
    local pieceIdx = getIndex( piece, tPieces )
    local objBgPiece = FindBgPiece( piece )
    local bgx, bgy = parseVec2( get( objBgPiece, "positionabs" ) )
    local px, py = parseVec2( get( piece, "positionabs" ) )
    
    if (objCurPiece == nil) then
        objCurPiece = piece
        startX, startY = getCursorPos()
        pivotX = px - startX
        pivotY = py - startY
		bPieceMoved = false
        showCursor( false )
        bringToFront( piece )
    elseif (piece == objCurPiece) then 
        objCurPiece = nil
        showCursor( true )
        local px, py = parseVec2( get( piece, "positionabs" ) )
        if math.abs( px - bgx ) < c_SnapDist and math.abs( py - bgy ) < c_SnapDist then 
            set( piece, "positionabs", bgx .. "," .. bgy ) 
            set( piece, "enabled", false )
            local sx, sy = parseVec2( get( piece, "positionabs" ) )
            sx = sx+100
            sy = sy+80
            local objSuccessEff = obj( "SuccessEff" )
            set( objSuccessEff, "position", "" .. sx .. "," .. sy )
            call( objSuccessEff, "play" )
        end 
        bFinished = reduce( 
            function( val, arg ) 
                return val and (get( arg, "enabled" ) == "false")
            end, 
            true, 
            tPieces )
    end
end

log( "started" )

--  mini-game loop
while not bFinished do
    wait( 0.01 )
    if (objCurPiece ~= nil) then
        local x, y = getCursorPos()
		bPieceMoved = (vec2dist( x, y, startX, startY ) > c_MoveBias)
        set( objCurPiece, "positionabs", x + pivotX .. "," .. y + pivotY ) 
    end
end

--  finish game
set ( "Hint", "enabled", "true")
call( "PuzzleCompletteEffect", "play")
wait( 5.0 )
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
call( "mg_puzzle", "close" )
call( "OnWin", "play" )