require "common"
-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------
local bFinished = false
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
function MakeRandomCode()
	return "" .. math.random( 9 ).. math.random( 9 ).. math.random( 9 ).. math.random( 9 )
end

local sCode = "XXXX"

function clearChar( s, idx )
	return s:sub( 1, idx - 1 ) .. " " .. s:sub( idx + 1, -1 )
end
set ( "TutorialSwitch", "activeidx", "11" )
set ( "Hint", "enabled", "false")

-------------------------------------------------------------------------------
-- local methods
-------------------------------------------------------------------------------
function GetCodeFudge( code )
	local nX, nY = 0, 0
	local sCurCode = "" .. sCode
	local sUserCode = code
	for i = 1, 4 do
		local sDigit = string.sub( sUserCode, i, i )
		if (string.sub( sCurCode, i , i ) == sDigit) then
			nX = nX + 1
			sCurCode  = clearChar( sCurCode, i )
			sUserCode = clearChar( sUserCode, i )
		end
	end
	for i = 1, 4 do
		local sDigit = string.sub( sUserCode, i, i )
		if (sDigit ~= " ") then
			if (string.find( sCurCode, sDigit ) ~= nil) then
				nY = nY + 1
				sCurCode = sCurCode:gsub( sDigit, " " )  
			end
		end
	end
	return string.rep( "X", nX ) .. string.rep( "Y", nY ) .. string.rep( "0", 4 - nX - nY )
end

function ClearHistory()
	local tHistory = children( obj( "mg_code_history" ) )
	for i, h in ipairs( tHistory ) do
		local tLabels = children( h )
		set( tLabels[1], "text", "" )
		set( tLabels[2], "text", "" )
	end
end

function Start()
	sCode = MakeRandomCode()
	ClearHistory()
	set( obj( "code", "toolpanel" ), "text", "" )
	set( obj( "code", "lockpanel" ), "text", "" )
    set ( "g1", "text", "?" )
    set ( "g2", "text", "?" )
    set ( "g3", "text", "?" )
    set ( "g4", "text", "?" )
    set ( "c1", "pressed", false )
    set ( "c2", "pressed", false )
    set ( "c3", "pressed", false )
    set ( "c4", "pressed", false )
    set ( "c5", "pressed", false )
    set ( "c6", "pressed", false )
    set ( "c7", "pressed", false )
    set ( "c8", "pressed", false )
    set ( "c9", "pressed", false )
end

function AddHistory( code, fudge )
	local tHistory = children( obj( "mg_code_history" ) )
	for i, h in ipairs( tHistory ) do
		local tLabels = children( h )
		if (get( tLabels[1], "text") == "") then
			set( tLabels[1], "text", code )
			set( tLabels[2], "text", fudge )
			return true
		end
	end    
	return false
end

function OnLockButton( button )
	local objCode = obj( "code", button )
	local sCurCode = get( objCode, "text" )
	if (#sCurCode < 4) then
		sCurCode = sCurCode .. get( button, "name" )
		set( objCode, "text", sCurCode )
	end
end

function OnCodeDeleteDigit( button )
	local objCode = obj( "code", button )
	local sCurCode = get( objCode, "text" )
	if (#sCurCode > 0) then
		sCurCode = string.sub( sCurCode, 1, -2 )
		set( objCode, "text", sCurCode )
	end
end

function OnEnterCode( button )
	local objCode = obj( "code", button )
	local sCurCode = get( objCode, "text" )
	if (#sCurCode == 4) then
		if (hasParent( button, "lockpanel" )) then
			--  entering into lock panel
			if (sCurCode == sCode) then
				-- correct code
				bFinished = true
			else
				-- wrong code
				Start()
			end
		else
			--  entering into tool panel
			local sCodeFudge = GetCodeFudge( sCurCode )
			AddHistory( sCurCode, sCodeFudge )
			set( obj( "code", "toolpanel" ), "text", "" )
		end
	end
end

function OnGuessMore( num )
	local sText = get( num, "text" )
	if (sText == "?") then
		set( num, "text", "1" )
		return
	end
	local n = tonumber( sText )
	n = n + 1
	set( num, "text", n )
	if (n == 10) then 
		set( num, "text", "?" )
	end
end

function OnGuessLess( num )
	local sText = get( num, "text" )
	if (sText == "?") then
		set( num, "text", "9" )
		return
	end
	local n = tonumber( sText )
	n = n - 1
	set( num, "text", n )
	if (n == 0) then 
		set( num, "text", "?" )
	end
end

function OnClear( button )
	set( obj( "code", button ), "text", "" )
end
-------------------------------------------------------------------------------
--  game loop
-------------------------------------------------------------------------------
Start()
while not bFinished do
    wait( 0.5 )
end
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
set ( "Hint", "enabled", "true")
call( "mg_code", "close" )
call( "OnWin", "play" )


