require "common"
require "ui_common"
require "hog"
-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_ScrollSpeed  = 250
local c_ScrollBorder = 20
local c_BLScroll     = 650

-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------
local nScrollDir    = 0
local objLPane      = obj( "left" )
local objL          = obj( "back", objLPane )
local objRPane      = obj( "right" )
local objR          = obj( "back", objRPane )
local nLScroll      = tonumber( get( objLPane, "left" ) ) + c_ScrollBorder
local nRScroll      = tonumber( get( objRPane, "left" ) ) + tonumber( get( objRPane, "width" ) ) - c_ScrollBorder
local nWidth        = tonumber( get( objL, "width" ) )
local nScrolLeftBound = nWidth - tonumber( get( objLPane, "width" ) )
set ( "TutorialSwitch", "activeidx", "5" )
function IsTaskPiece( piece )
    return  (get( piece, "enabled" ) == "true") and 
            (string.find( get( piece, "name" ), "fish" ) == nil)
end 

function Init()
    local tPiecesL = children( objL )
    local tPiecesR = children( objR )
    tPieces = {}
    tTasks = {}
    if tPiecesL ~= nil then
        for i, p in ipairs( tPiecesL ) do
            if IsTaskPiece( p ) then
                tPieces[#tPieces + 1] = p
            end
        end
    end
    if tPiecesR ~= nil then
        for i, p in ipairs( tPiecesR ) do
            if IsTaskPiece( p ) then
                tPieces[#tPieces + 1] = p
            end
        end
    end
    nToOpen = #tPieces
    nOpened = 0
    UpdateStatusText()
    connect( "items", "onlbup", "OnMiss" )
end

function OnPieceClicked( piece )
    set( piece, "visible", "false" )
    HogAnimatePiece( piece, piece )
    nOpened = nOpened + 1
    if (nOpened == nToOpen) then
        bWin = true
        bFinished = true
    end
    UpdateStatusText()
end

function DoScroll()
    local mx, my = getCursorPos()
    if (mx < nLScroll and my < c_BLScroll) then
        nScrollDir = 1.0
        setCursor( "gLeft" )
    elseif (mx > nRScroll) then
        nScrollDir = -1.0
        setCursor( "gRight" )
    else
        if mx > 590 then
            setCursor( "drochright" )
        elseif mx < 590 then
            setCursor( "drochleft" )
        end
        nScrollDir = 0.0
    end
    
    local scrollDelta = nScrollDir*c_ScrollSpeed*c_LoopDT
    local cx = math.floor( tonumber( get( objL, "left" ) ) )
    cx = clamp( cx + scrollDelta, -nScrolLeftBound, 0.0 )
    set( objL, "left", cx )
    set( objR, "left", cx )
end

function PickPiece( mx, my )
    log( "PickPiece: " .. mx .. " " .. my )
    local objProbe = obj( "hog_probe" )
    for i, p in ipairs( tPieces ) do
        if (get( p, "visible" ) == "true" and get( p, "enabled" ) == "true") then
            set( objProbe, "position", mx .. "," .. my )
            set( objProbe, "skinpack", get( p, "skinpack" ) )
            set( objProbe, "skinframe", get( p, "skinframe" ) )
            local alpha = tonumber( get( objProbe, "alpha" ) )
            if (alpha > 0) then
                OnPieceClicked( p )
            end
        end
    end
end

function OnLClick()
    local mx, my = getCursorPos()
    local cx, cy = parseVec2( get( objL, "positionabs" ) )
    mx = mx - cx
    my = my - cy
    PickPiece( mx, my )
end

function OnRClick()
    local mx, my = getCursorPos()
    local cx, cy = parseVec2( get( objR, "positionabs" ) )
    mx = mx - cx
    my = my - cy
    PickPiece( mx, my )
end

function ShowHint()
    local objPiece = HogGetHintPiece()
    --  snap the viewport, if needed
    local px, py = parseVec2( get( objPiece, "pivot" ) )
    local bx, by = parseVec2( get( objL, "position" ) )
    local fx = bx + px
    log( "pivot factor: " .. fx )
    if fx < 0 then 
        set( objL, "left", 0 )
        set( objR, "left", 0 )
    elseif fx > nScrolLeftBound then 
        set( objL, "left", -nScrolLeftBound )
        set( objR, "left", -nScrolLeftBound )
    end
    
    local objEffHint = obj( "effHint" )
    call( objEffHint, "play" )
    while (get( objEffHint, "playing" ) == "true" ) do
        wait( 0.02 )
        local sx, sy = parseVec2( get( objPiece, "positionabs" ) )
        set( objEffHint, "position", "" .. sx .. "," .. sy )
    end
end

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