require "common"
require "ui_common"
require "hog"
-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_DefaultStartAmount   = 5
local c_ProfileFlySpeed      = 1000.0
local c_LocalDT              = 0.02

-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------
local objItemList       = nil
local objProfileList    = nil
set ( "TutorialSwitch", "activeidx", "3" )
function UpdateProfiles()
    for j, t in ipairs( tTasks ) do
        if (t.bComplete) then
            set( t.objProfile, "fgcolor", "0x33000000" )
        else
            set( t.objProfile, "fgcolor", "0xAAFFFFFF" )
        end
    end
end

function Init()
    tPieces          = children( obj( "items" ) )
    connect( "items", "onlbup", "OnMiss" )
    objItemList      = obj( "ItemList" )
    objProfileList   = obj( "ProfileList" )
    delObjects( children( objItemList ) )
    local tProfiles = children( objProfileList )
    
    for i, f in ipairs( tPieces ) do
        local sText = get( f, "text" )
    	if (sText == "") then
    		set( f, "tag", -1 )
    	else
    	    local nTag = #tTasks + 1
    	    
    	    curTask = {}
    	    curTask.bComplete = false
            nToOpen = nToOpen + 1
            
            local objProfile = nil
            for j, p in ipairs( tProfiles ) do
                if sText == get( p, "text" ) then
                    objProfile = p
                end
    	    end
            curTask.objProfile = objProfile
            curTask.objItem    = f
            curTask.bComplete  = false
            curTask.sText      = sText
            
            if objProfile ~= nil then
                set( f, "tag", nTag )
                set( objProfile, "tag", nTag )
                
                if (nTag > c_DefaultStartAmount) then
                    curTask.bHidden = true
                    set( f, "enabled", false )
                    set( objProfile, "visible", false )
                else
                    curTask.bHidden = false
                    set( objProfile, "visible", true )
                end
                tTasks[nTag] = curTask
            end
    	end
    end
    UpdateProfiles()
    UpdateStatusText()
end

function AllOpen()
    for j, t in ipairs( tTasks ) do
        if not t.bComplete then
            bAllOpen = false
            return false
        end
    end
    return true
end

function OnPieceClicked( piece )
	if bPicking then
		return
	end
    
    local nTag = tonumber( get( piece, "tag" ) )
	if (nTag == -1) then return end;
	
    local curTask = tTasks[nTag]
    
	-- animate piece
    set( piece, "visible", false )
	HogAnimatePiece( piece, curTask.objProfile )
	
    nOpened = nOpened + 1
    UpdateStatusText()
	curTask.bComplete = true    
    UpdateProfiles()
    
    -- unhide one hidden task
    for i, t in ipairs( tTasks ) do
        if (t.bHidden) then
            t.bHidden = false
            set( t.objItem, "enabled", true )
            set( t.objProfile, "visible", true )
            --  animate new profile
            local nY = tonumber( get( t.objProfile, "top" ) )
            local sY = 800
            local cT = 0.0
            local cY = sY
            while cY > nY do
                cY = sY - cT*c_ProfileFlySpeed
                set( t.objProfile, "top", cY )
                wait( c_LocalDT )
                cT = cT + c_LocalDT 
            end
            set( t.objProfile, "top", nY )
            break
        end
    end
    UpdateProfiles()
    
    if (AllOpen()) then
        bFinished = true
        bWin = true
    end
end

--  game loop
Init()
HogInit()
while not bFinished do
    HogUpdate()
end
 
HogFinish()

