require "common"
require "ui_common"
require "hog"
require "vocab"
-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_DefaultStartAmount   = 10
local c_LabelFlySpeed        = 1000.0
local c_LocalDT              = 0.02
local c_LowestLabelBound     = 480
-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------

local objItemList       = nil
local objLabelTemplate  = nil
function UpdateLabelText()
    for j, t in ipairs( tTasks ) do
        if (t.nPieces > 1) then
            -- lookup plural form in vocabulary
            local sText = t.sText
            for i, v in ipairs( g_Vocab ) do
                if v[1] == sText then 
                    sText = v[2]
                end
            end
            set( t.objLabel, "text", sText .. "(" .. t.nPieces .. ")" )
        else
            set( t.objLabel, "text", t.sText )
        end
    end
end

function Init()
    tPieces          = children( obj( "items" ) )
    connect( "items", "onlbup", "OnMiss" )
    objItemList      = obj( "ItemList" )
    objLabelTemplate = obj( "ItemTemplate" )
    delObjects( children( objItemList ) )
    
    for i, f in ipairs( tPieces ) do
        local sText = get( f, "text" )
    	if (sText == "") then
    		set( f, "tag", -1 )
    	else
    	    local curTask = nil 
    	    for j, t in ipairs( tTasks ) do
    	        if (t.sText == sText) then
    	            t.nPieces = t.nPieces + 1
                    nToOpen = nToOpen + 1
    	            curTask = t
    	            set( f, "tag", j )
                    if (t.bHidden) then
                        set( f, "enabled", false )
                    end
    	            break
    	        end
    	    end
    	    if (curTask == nil) then
    	        local nTag = #tTasks + 1
    	        
    	        curTask = {}
    	        curTask.sText = sText
    	        curTask.nPieces = 1
                nToOpen = nToOpen + 1
    	        
    	        -- create label
    	        local objLabel = create( objLabelTemplate, "piece" .. i, objItemList )
    	        set( objLabel, "text", sText )
    	        set( objLabel, "tag", nTag )
    	        set( f, "tag", nTag )
    	        curTask.objLabel = objLabel
                
                if (nTag > c_DefaultStartAmount) then
                    curTask.bHidden = true
                    set( f, "enabled", false )
                    set( objLabel, "visible", false )
                else
                    curTask.bHidden = false
                    set( objLabel, "visible", true )
                end
    	        
    	        tTasks[nTag] = curTask
    	    end 
    	end
    end
    UpdateLabelText()
    UpdateStatusText()
end

function AllOpen()
    for j, t in ipairs( tTasks ) do
        if (t.nPieces > 0) 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.objLabel )
    
    nOpened = nOpened + 1
    UpdateStatusText()
	curTask.nPieces = curTask.nPieces - 1    
    if (curTask.nPieces == 0) then
        set( curTask.objLabel, "fgcolor", "0x77999999" )
        set( curTask.objLabel, "shadowcolor", "0" )
        curTask.bComplete = true
    end
    
    UpdateLabelText()
    -- unhide one hidden task
    for i, t in ipairs( tTasks ) do
        if (t.bHidden) then
            t.bHidden = false
            for j, f in ipairs( tPieces ) do
                if (get( f, "text" ) == t.sText) then
                    set( f, "enabled", true )
                end
            end
            local objFlyLabel = create( t.objLabel, "flylabel", obj( "flying_label" ) )
            set( objFlyLabel, "visible", true )
            --  animate new profile
            local nY = tonumber( get( t.objLabel, "top" ) )
            if (nY > c_LowestLabelBound) then
                --  remove some dead label
                for j, t in ipairs( tTasks ) do
                    if (t.bComplete and t.objLabel ~= nil) then
                        delete( t.objLabel )
                        t.objLabel = nil
                        break
                    end
                end
            end
            local sY = 800
            local cT = 0.0
            local cY = sY
            while cY > nY do
                cY = sY - cT*c_LabelFlySpeed
                set( objFlyLabel, "top", cY )
                wait( c_LocalDT )
                cT = cT + c_LocalDT 
            end
            set( t.objLabel, "visible", true )
            set( t.objLabel, "top", nY )
            delete( objFlyLabel )
            break
        end
    end
    UpdateLabelText()
    
    if (AllOpen()) then
        bWin = true
        bFinished = true
    end
end

--  game loop
Init()
HogInit()
while not bFinished do
    HogUpdate()
end
--log( "finished list. bFinished: " .. bFinished .. " bWin: " .. bWin )
HogFinish()
call( "sndWin", "play" )
