------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Ian Eller
-- Updated: 09/22/2008, ice

dofile( "../../Assets/Scripts/key_name_mappings.lua" )

local MAX_TEXT = 100
local MAX_DISP = 20
local visible  = false
local disp_b = 1
local disp_e = disp_b + 1
local size   = 1
local wallOfText = Entity:Data()


local function Parse()
  Global:DoString( wallOfText[ size ] )

end


local function AddLine()
  if( size == MAX_TEXT ) 
  then
    size   = size - 1
    disp_b = disp_b - 1 
    disp_e = disp_e - 1 
    
    local incr = 1
    while( incr <= size )
    do
      wallOfText[ incr ] = wallOfText[ incr + 1 ]
      incr = incr + 1
      
    end
    
  end
  
  Parse()
  size = size + 1
  wallOfText[ size ] = ""
  
  disp_e = disp_e + 1
  if( disp_e <= size )
  then
    disp_e = size + 1
    
    if( size > MAX_DISP ) then
      disp_b = disp_e - MAX_DISP - 1
    end
    
  else
    if( size > MAX_DISP ) then
      disp_b = disp_b + 1
      
    end
    
  end
  
end


local function SetLine( text )
  wallOfText[ size ] = text
  
end


local function GetLine()
  return wallOfText[ size ]
  
end


local function GetAll()
  local it  = disp_b
  local it2 = disp_e;
  local str = ""
  
  while( it ~= it2 )
  do
    str = str .. wallOfText[ it ] .. "\n"
    it  = it + 1
    
  end
  
  return str;
  
end


local function ViewListDir( up )
  if( up ) then
    if( disp_b ~= 1 )
    then
      disp_b = disp_b - 1
      disp_e = disp_e - 1
      
    end
  else
    if( disp_e <= size )
    then
      disp_b = disp_b + 1
      disp_e = disp_e + 1
      
    end
    
  end

end


local function ConvertForTweak2( theKey, theValue )
  local text = "  " .. theKey .. ": " .. tostring( theValue )
  SetLine( text )
  AddLine()
  
end


local function ConvertForTweak( theKey, theTable )
  SetLine( theKey )
  AddLine()
  table.foreach( theTable, ConvertForTweak2 )
  
end


local function ConvertForCamera2( theKey, theValue )
  local text = "  " .. theKey .. ": " .. tostring( theValue )
  SetLine( text )
  AddLine()
  
end


local function ConvertForCamera( theKey, theTable )
  SetLine( theKey )
  AddLine()
  table.foreach( theTable, ConvertForTweak2 )
  
end


wallOfText[ size ] = ""
while( 1 ) 
do
  coroutine.yield( 1 )
  
  --[[  
  if( Global:IsTriggered( key_names_to_values( "F1" ) ) ) then
    visible = not visible
    Global:SetGameMode( not visible )
  
  end
  --]]
  
  if( visible )
  then

    if( Global:IsTriggered( key_names_to_values( "Enter" ) ) ) then
      AddLine()
    
    elseif( Global:IsTriggered( key_names_to_values( "Arrow Up" ) ) ) then
      ViewListDir( true )

    elseif( Global:IsTriggered( key_names_to_values( "Arrow Down" ) ) ) then
      ViewListDir( false )
    
    elseif( Global:IsTriggered( key_names_to_values( "Control Left" ) ) ) 
    then
      if( Global:IsPressed( key_names_to_values( "Shift Left" ) ) ) 
      then
        local myTable = Global:PrintTweakables()
        table.foreach( myTable, ConvertForTweak )
        
      else
        local myTable = { }
        myTable[ "pos" ] = Global:gCameraPosition()
        myTable[ "tar" ] = Global:gCameraTarget()
        table.foreach( myTable, ConvertForCamera )
        
      end

    else
      SetLine( Global:GetCurrentInput() )
    
    end
    
    Entity:VIS_SetText( GetAll() )
    
  else
    Entity:VIS_SetText( "" )
    
  end

end
