require "common"

-------------------------------------------------------------------------------
--  helper functions
-------------------------------------------------------------------------------
function setLocation( locName, charPos, charDir )
    log( "setLocation " .. locName )
    local setloc = obj( "setLocationDelegate" )
	local sCurLoc = get( setloc, "location" )
	if (sCurLoc == setloc) then
		return
	end
    set( setloc, "location", locName )
    if charPos ~= nil then
        set( setloc, "charpos", charPos )
    end
    if charDir ~= nil then
        set( setloc, "chardir", charDir )
    end   
    set( setloc, "visible", "true" )
end

function fade( duration, bIn )
    local objFade = obj( "FadeObject" )
    set( objFade, "visible", "true" )
    local fDelta = 0.04
    
    if bIn then
        for t = 0.0, duration, fDelta do
            set( objFade, "bgalpha", t )
            wait( fDelta )
        end
    else
        for t = 0.0, duration, fDelta do
            set( objFade, "bgalpha", 1.0 - t )
            wait( fDelta )
        end
    end
    set( objFade, "visible", "false" )
end

function showMainMenu( bShow )
    local objMenu = obj( "MainMenu" )
    if bShow then
        set( objMenu, "visible", "true" )
    else
        set( objMenu, "visible", "false" )
    end
end

function FSM( t )
  t.sendEvent = function( self, eventName, eventItem )
    local event = { name = eventName, item = eventItem }
    self.tEventQueue[#(self.tEventQueue) + 1] = event
    --log( "Sent Event: " .. event.name )
  end
  
  t.tEventQueue = {}
  
  t.setState = function( self, state )
    if self.curState ~= nil then
        self.prevState = self.curState.sName
    end
    self.curState = self[state]
    if self.curState ~= nil then
        self.curState.sName = state
        local activity = self.curState.activity
        if activity ~= nil then 
            activity() 
        end
    end
  end

  t.run = function( self, state )
    local fDeltaTime = 0.02
    self:setState( state )    
    local bRunning   = (self.curState ~= nil)
    while bRunning do
        -- take next event from the queue
        local event = self.tEventQueue[1]
        for i, f in ipairs( self.curState ) do
            local item   = nil
            local bGuard = false
            local bEvent = false
     
            if event ~= nil then
                item = event.item 
                bEvent = (f.event ~= nil and f.event == event.name)
                if bEvent then
                    bGuard = (f.guard == nil or (f.guard ~= nil and f.guard( item ) == true))
                end
            elseif f.event == nil then
                bGuard = (f.guard ~= nil and f.guard() == true)
                bEvent = bGuard
            end
            
            if bGuard and bEvent then
                if self.curState ~= nil and self.curState.exitaction ~= nil then
                    self.curState.exitaction()
                end
                --  do action
                if f.action ~= nil then 
                    f.action( item ) 
                end
                -- switch state
                if f.state ~= nil then
                    self:setState( f.state )
                    --log( "Switched to State: " .. f.state )
                end
                do break end
            end
        end
        -- queue shift         
        if event ~= nil then
            --log( "Removing Event: " .. event.name )
            for i, f in ipairs( self.tEventQueue ) do
                self.tEventQueue[i] = self.tEventQueue[i + 1]
            end
        end
        
        if self.curState == nil then 
            --log( "Switched to nil state!" )
            bRunning = false 
        end
        wait( fDeltaTime )
    end
      
  end

  return t
end

function messageBox( sText, sCaption, sType )
    local objMsgBox = obj( "MsgBox" )
    local objText = obj( sText, objMsgBox )
    local objTextLabel = obj( "DialogText", objMsgBox )
    set( objTextLabel, "text", get( objText, "text" ) )
	
	local objCaption = obj( sCaption, objMsgBox )
    local objCaptionLabel = obj( "Caption", objMsgBox )
	set( objCaptionLabel, "text", "" )
	if objCaption ~= nil then
		set( objCaptionLabel, "text", get( objCaption, "text" ) )
	end
	
	set( "YesNo", "visible", false )
	set( "OK", "visible", false )
    set( "Play", "visible", false )
	
	if (sType == "YesNo") then 
		set( "YesNo", "visible", true )
	elseif (sType == "Play") then
		set( "Play", "visible", true )
	else
		set( "OK", "visible", true )
	end
	
    set( "game", "enabled", "false" )
	call( objMsgBox, "Show" )
	-- spin until response
	while true do
		wait( 0.5 )
		if (get( objMsgBox, "visible" ) == "false") then
			break
		end
	end
    set( "game", "enabled", "true" )
	return get( "result", "text" )
end


