require "common"
require "hog_common"

-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_FramesPerNum = 3
local c_FrameTime = 0.04
local c_nFrames = c_FramesPerNum*9
local bFinished = false
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
set ( "Hint", "enabled", "false")
set ( "TutorialSwitch", "activeidx", "9" )
-------------------------------------------------------------------
-- local methods
-------------------------------------------------------------------------------
function SetNum( wheel, n, bAnimate )
	local nCurN = tonumber( get( wheel, "tag" ) )
	local nEndFrame = (n - 1)*c_FramesPerNum + 1
	local nBegFrame = (nCurN - 1)*c_FramesPerNum + 1
	local objCrystal = obj( "crystal", wheel )
	if (bAnimate == true) then
		if (nEndFrame - nBegFrame > c_nFrames/2) then nBegFrame = nBegFrame + c_nFrames end
		if (nBegFrame - nEndFrame > c_nFrames/2) then nEndFrame = nEndFrame + c_nFrames end
		local dn = 1
		if (nEndFrame < nBegFrame) then dn = -1 end
		for nFrame = nBegFrame, nEndFrame, dn do
			local cFrame = (nFrame - 1)%c_nFrames + 1
			set( objCrystal, "skinframe", cFrame )
			wait( c_FrameTime )
		end
	end
	set( objCrystal, "skinframe", nEndFrame%c_nFrames )
	set( wheel, "tag", n )
end

function getN( wheel )
	return tonumber( get( wheel, "tag" ) )
end

function getWheel( n )
	local tWheels = children( "wheels" )
	for i, w in ipairs( tWheels ) do
		if (tonumber( get( w, "tag" ) ) == n) then
			return w
		end
	end
	return nil
end

function AllCorrect()
	local tWheels = children( "wheels" )
	local cSum = 0
	-- rows
	for i = 0, 2 do
		local curSum = getN( tWheels[i*3 + 1] ) + getN( tWheels[i*3 + 2] ) + getN( tWheels[i*3 + 3] )
		log( curSum )
		if (cSum == 0) then cSum = curSum end
		if (cSum ~= curSum) then
			return false
		end
	end
	-- columns
	for i = 1, 3 do
		local curSum = getN( tWheels[i] ) + getN( tWheels[3 + i] ) + getN( tWheels[6 + i] )
		if (cSum ~= curSum) then
			return false
		end
	end
	return true
end

function Shuffle( ctx )
	local tWheels = children( "wheels" )
	local tNums = {1,2,3,4,5,6,7,8,9}
	shuffle( tNums )
	for i, w in ipairs( tWheels ) do
		local n = tNums[i]
		set( w, "tag", n )
		SetNum( w, n, false )
	end
end

function OnWheelClicked( wheel )
	local x, y = parseVec2( get( wheel, "positionabs" ) )
	local mx, my = getCursorPos()
	local angle = (math.atan2( my - y, mx - x ) + 4.5*math.pi)/(math.pi*2.0)
	local n = math.floor( 9.0*angle + 0.5 ) + 1
	n = (n - 1)%9 + 1
	local nCurN = tonumber( get( wheel, "tag" ) )
	if (nCurN == n) then
		return
	end
	local curWheel = getWheel( n )
	SetNum( wheel, n, true )
	SetNum( curWheel, nCurN, true )
	assert( wheel ~= curWheel )
	bFinished = AllCorrect()
end

function main()
	Shuffle()
	connectObjects( children( "wheels" ), "onlbup", "OnWheelClicked" )
    bFinished = false
	while not bFinished do
	    wait( 0.5 )
	end
end

-------------------------------------------------------------------------------
--  game loop
-------------------------------------------------------------------------------
main()
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
set ( "Hint", "enabled", "true")
call( "mg_square", "close" )
call( "OnWin", "play" )
