------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
--------------------------------------------------------------------------------
-- Fuctions!
-- This might look redundant, but avoids costly global lookup
--------------------------------------------------------------------------------

local GLOBAL_TouchRenderPass         = Global.TouchRenderPass
local GLOBAL_SetBlendFour            = Global.SetBlendFour
local GLOBAL_TimeElapsed             = Global.TimeElapsed
local GLOBAL_SetGameMode             = Global.SetGameMode
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr

local ENTITY_GetMyPointer            = Entity.GetMyPointer
local ENTITY_SPT_RefreshNumbers      = Entity.SPT_RefreshNumbers
local ENTITY_SPT_RefreshStrings      = Entity.SPT_RefreshStrings

--------------------------------------------------------------------------------
-- Variables
--------------------------------------------------------------------------------
local myData = {}
local bool isTransitioning = false
local slideTime = 0
local transitionTime = 0
local scales = {}
local indx = 0

local TIME_PER_SLIDE
local TRANSITION_TIME
local LAST_SLIDE

--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
local function Clamp(value, min, max)
  if value < min then value = min end
  if value > max then value = max end
  return value
end

--==============================================================================
local function AddText( text, time, height )
  local len = string.len( text )
  local CHAR_WIDTH = ( 0.6 / 40.0 ) * len

  Global:AddPopupTextToQueue( text, time, ( 1 - CHAR_WIDTH ) / 1.65, height )

end

--------------------------------------------------------------------------------
-- Initialization
--------------------------------------------------------------------------------
ENTITY_SPT_RefreshNumbers( Entity, myData )
ENTITY_SPT_RefreshStrings( Entity, myData )

TIME_PER_SLIDE  = myData.TimePerSlide
TRANSITION_TIME = myData.TransitionTime
LAST_SLIDE      = myData.SlideCount + 1
slideTime       = TIME_PER_SLIDE

GLOBAL_TouchRenderPass( "enable " .. myData.ShowName )
scales[0] = 0 --dummy value never gets used
scales[1] = 0
scales[2] = 0
scales[3] = 0
scales[4] = 0
scales[5] = 0 --dummy value never gets used

GLOBAL_SetGameMode( false )

--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------
while( 1 ) 
do
  coroutine.yield()

  local elapsedTime = GLOBAL_TimeElapsed()

  if( not isTransitioning )
  then
    slideTime = slideTime + elapsedTime

    if( slideTime >= TIME_PER_SLIDE )
    then
      slideTime = 0
      isTransitioning = true

    end

  else -- Update the slide alpha for the transition
    transitionTime = transitionTime + elapsedTime
    
    local t = transitionTime / TRANSITION_TIME
    t = Clamp(t, 0, 1)
    
    scales[indx] = 1 - t
    if( indx + 1 ~= LAST_SLIDE )
    then
      scales[indx + 1] = t

    end

    -- We are done transitioning
    if( transitionTime >= TRANSITION_TIME )
    then
      indx = indx + 1
      transitionTime = 0
      isTransitioning = false

      -- Do some text
      local text = "Text" .. indx
      if( myData[ text ] ~= nil ) and ( indx ~= LAST_SLIDE )
      then
        local height = "Height" .. indx
        AddText( myData[ text ], TIME_PER_SLIDE, myData[ height ] or .75 )

      end

    end

  end

  GLOBAL_SetBlendFour( scales[1], scales[2], scales[3], scales[4] )

  if( indx == LAST_SLIDE - 1 )
  then
    --GLOBAL_SetGameMode( true )

  elseif( indx == LAST_SLIDE )
  then
    GLOBAL_TouchRenderPass( "disable " .. myData.ShowName )
    GLOBAL_SetBlendFour( 0, 0, 0, 0 )
    GLOBAL_SetGameMode( true )
    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( Entity ) )

  end

end
