require "common"
-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_Side 		= 5
local c_NumShuffles = 30
local c_SwapSpeed 	= 1200
local c_DeltaT 	    = 0.01
local c_Skins  	    = { "mg_15_1", "mg_15_2", "mg_15_3" }
-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------
local tPieces 			= children( obj( "pieces" ) )
local objFull 			= obj( "full" )
local objFrame 			= obj( "frame" )
local bFinished  		= false
local objSelPiece  		= nil
set ( "TutorialSwitch", "activeidx", "7" )
local nSkin = tonumber( get( "skin", "value" ) )
local sSkin = c_Skins[nSkin]
setObjects( tPieces, "skinpack", sSkin )
set( objFull, "skinpack", sSkin )
set( objFullComplete, "skinpack", sSkin )

setObjects( tPieces, "visible", "true" )
connectObjects( tPieces, "onlbup", "OnPieceClicked" )
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
set ( "Pause",  "enabled", "false")
set ( "Hint",   "enabled", "false")
set ( "Bonus",  "enabled", "false")
set ( "mg_15_help", "visible", "true")

if nSkin == 1 then
    set ( "mg_15_help", "normalframe", "0" )
    set ( "mg_15_help", "hoverframe", "1" )
    set ( "15helper", "startframe", "2" )   
    elseif nSkin == 2 then
        set ( "mg_15_help", "normalframe", "3" )
        set ( "mg_15_help", "hoverframe", "4" )
        set ( "15helper", "startframe", "5" )
    elseif nSkin == 3 then
        set ( "mg_15_help", "normalframe", "6" )
        set ( "mg_15_help", "hoverframe", "7" )
        set ( "15helper", "startframe", "8" )
end 

-------------------------------------------------------------------------------
-- local methods
-------------------------------------------------------------------------------
--  returns piece by its tag
function getByTag( nTag )
	for i, p in ipairs( tPieces ) do
		if nTag == tonumber( get( p, "tag" ) ) then
			return p
		end
	end
	return nil
end

--  swaps two pieces, if bAnimate is true, the swapping process is animated - otherwise they are swapped instantly
function swapPieces( nA, nB, bAnimate )
	if nA == -1 or nB == -1 then
		return 
	end
	local objA = getByTag( nA )
	local objB = getByTag( nB )
	local xA, yA = parseVec2( get( objA, "position" ) )
	local xB, yB = parseVec2( get( objB, "position" ) )
	set( objA, "tag", -1 )
	set( objB, "tag", -1 )
	local swapTime = vec2dist( xA, yA, xB, yB )/c_SwapSpeed
	if bAnimate == true and swapTime > 0 then
		local nIdxA = tonumber( get( objA, "index" ) )
		local nIdxB = tonumber( get( objB, "index" ) )
		bringToFront( objA )
		bringToFront( objB )
		for cT = 0, swapTime, c_DeltaT do
			local t = cT/swapTime
			set( objA, "position", lerp( xB, xA, t )  .. "," .. lerp( yB, yA, t ) )
			set( objB, "position", lerp( xA, xB, t )  .. "," .. lerp( yA, yB, t ) )
			wait( c_DeltaT )
		end
		if (nIdxA < nIdxB) then
			set( objA, "index", nIdxA )
			set( objB, "index", nIdxB )
		else
			set( objB, "index", nIdxB )
			set( objA, "index", nIdxA )
		end
	end
	set( objA, "position", xB .. "," .. yB )
	set( objB, "position", xA .. "," .. yA )
	set( objA, "tag", nB )
	set( objB, "tag", nA )
end

-- returns true if every piece is on the proper place
function AllDone()
	for i, p in ipairs( tPieces ) do
		if tonumber( get( p, "tag" ) ) + 1 ~= getIndex( p, tPieces ) then
			return false
		end
	end
    log( "AllDone!" )
	return true
end

function OnPieceClicked( item )
	if (objSelPiece == nil) then
		objSelPiece = item
		set( objFrame, "visible", true )
		set( objFrame, "position", get( item, "positionabs" ) )
		return
	end
	local nSel = tonumber( get( objSelPiece, "tag" ) )
	local nCur = tonumber( get( item, "tag" ) )
	set( objFrame, "visible", false )	
	swapPieces( nSel, nCur, true )
	bFinished = AllDone()
	objSelPiece = nil
end

for i, p in ipairs( tPieces ) do
	set( p, "tag", i - 1 )
    set( p, "skinframe", i - 1 )
end

for i = 1, c_NumShuffles do
	local nA = math.random( #tPieces ) - 1
	local nB = math.random( #tPieces ) - 1
	swapPieces( nA, nB )
end

-------------------------------------------------------------------------------
--  game loop
-------------------------------------------------------------------------------
while not bFinished do
    wait( 0.5 )
end
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
set ( "Pause",  "enabled", "true")
set ( "Hint",   "enabled", "true")
set ( "Bonus",  "enabled", "true")
set ( "mg_15_help", "visible", "false")
call( "15CompletteEffect", "play")
wait( 5.0 )
call( "OnWin", "play" )

