------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Updated: 3/31/2009
-- Updated: 7/08/2009, Matt

Global:StatTrack( 0, true )

local GLOBAL_AddScarfSegment     = Global.AddScarfSegment
local GLOBAL_CreateEntityAt      = Global.CreateEntityAt
local GLOBAL_GetLanternPosition  = Global.GetLanternPosition
local GLOBAL_PlaySound2D         = Global.PlaySound2D
local GLOBAL_MusicLayer          = Global.MusicLayer
local GLOBAL_AddPopupTextToQueue = Global.AddPopupTextToQueue
local GLOBAL_AddPopupTextToQueueEX = Global.AddPopupTextToQueueEX
local GLOBAL_StandingLanternCollided = Global.StandingLanternCollided


local ENTITY_SHR_Refresh        = Entity.SHR_Refresh
local ENTITY_SHR_sPosition      = Entity.SHR_sPosition
local ENTITY_SPT_AddNumber      = Entity.SPT_AddNumber
local ENTITY_VIS_GetLightColor  = Entity.VIS_GetLightColor
local ENTITY_VIS_SetLightColor  = Entity.VIS_SetLightColor


local LIGHTFILE = "../../Assets/Xml/entities/e_StandingLanternLight.xml"
local EPSILON = .01
local SPEED = .25

local myData = Entity:Data()
local follow = 10
local light
local lightData = {}
local lightColor = {}
local spawned = false
local midSequ = false
local lit = false
local radiusSq = 100.0

local LINE_HEIGHT = 28.0 / 1024.0
local CHAR_WIDTH = 0.6 / 40.0 -- .87 / 40.0
local CHAR_TIME = 110
local stanza = ""
local width = 0
local time = 0

Entity:SPT_RefreshNumbers( myData )
Entity:SPT_RefreshStrings( myData )

------------------------------------------------------------------------
local function ParsePoem( str )
  if(str ~= nil) then
    for line in str:gmatch("[^~]+") do
      if(string.len(line) * CHAR_WIDTH > width) then
        width = string.len(line) * CHAR_WIDTH
      end
      stanza = stanza .. line .. '\n'
      time = time + string.len(line) * CHAR_TIME
    end
  end
end

ParsePoem( myData.poem )

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

  if( not lit )
  then
    ENTITY_SHR_Refresh(Entity, myData, true, true, false, false)
    Entity:SPT_RefreshNumbers( myData )
    Entity:SPT_RefreshStrings( myData )
    
    local lanternPos = GLOBAL_GetLanternPosition()
    local dx = lanternPos.x - myData.pos.x
    local dy = lanternPos.y - (myData.pos.y + 4.285) -- Offset sphere to light center
    local dz = lanternPos.z - myData.pos.z
    local distSq = dx * dx + dy * dy + dz * dz

	if( spawned )
	then
      if( follow > 0 )
      then
        follow = follow - 1
        ENTITY_SHR_sPosition( light, lanternPos.x,
                                     lanternPos.y,
                                     lanternPos.z )

      else
        ENTITY_SHR_Refresh( light, lightData, true, false, false, false )
        dx = lightData.pos.x - myData.pos.x
        dy = lightData.pos.y - (myData.pos.y + 4.285) -- Offset sphere to light center
        dz = lightData.pos.z - myData.pos.z
        distSq = dx * dx + dy * dy + dz * dz

        if( distSq < EPSILON )
        then
          local poetry = not midSequ
          GLOBAL_AddPopupTextToQueueEX( poetry, stanza, time, ( 1 - width ) / 2, .75 )
          if( not Global:ViewPoetry() ) then
            GLOBAL_AddPopupTextToQueue("Poetry gathered ...", 7000, .70, .03)
          end
          
          GLOBAL_AddScarfSegment()
          GLOBAL_StandingLanternCollided()
          ENTITY_SPT_AddNumber( Entity, "lit", 1 )
          ENTITY_SHR_sPosition( light, myData.pos.x,
                                       myData.pos.y + 4.285,
                                       myData.pos.z )
          ENTITY_VIS_SetLightColor( light, lightColor.r, lightColor.g, lightColor.b )

          print("FFHorde: ", myData.FFHorde)
          if( myData.FFHorde ~= nil )
          then
            -- Firefly sea
            Global:FireflySpawnMsg( 60, 0, 0, myData.pos.x, myData.pos.y, myData.pos.z, myData.FFHorde, 2 )
          end

          lit = true

        else
          local completeness = 1 - distSq / radiusSq
          local dist = math.sqrt( distSq )
          dx = dx / dist
          dy = dy / dist
          dz = dz / dist
          local speed = SPEED
          speed = math.min( speed, dist )
          ENTITY_SHR_sPosition( light, lightData.pos.x - speed * dx,
                                       lightData.pos.y - speed * dy,
                                       lightData.pos.z - speed * dz )
          ENTITY_VIS_SetLightColor( light, completeness * lightColor.r,
                                           completeness * lightColor.g,
                                           completeness * lightColor.b )

        end

      end
    elseif( distSq < radiusSq or myData.forcedPenetration )
    then
      Global:StatTrack( 0, false )
      light = GLOBAL_CreateEntityAt( LIGHTFILE,
                                     lanternPos.x, lanternPos.y, lanternPos.z,
                                     myData.rot.x, myData.rot.y, myData.rot.z) -- Note: particle systems don't rotate
      lightColor = ENTITY_VIS_GetLightColor( light )
      -- Hackiest hack in the history of hacks
      Editor:AttachToIsland( 0, light:GetMyPointer(), LIGHTFILE )
      
      if( myData.started0 ) 
      then
        local midder = Global:CreateEntity( "../../Assets/Xml/entities/game_MidSequence.xml" )
        midder:SPT_GiveNumber( "lantern", 1 )
        myData.started0 = nil
        midSequ = true

      elseif( myData.started1 ) 
      then
        Global:FindEntityByName( "Moon", false ):SPT_GiveNumber( "started1", 1 )
        myData.started1 = nil
        
      else
        GLOBAL_PlaySound2D("../../Assets/Sounds/lantern_light.ogg")
        GLOBAL_MusicLayer( myData.musicLayer, myData.musicAction, myData.musicFile )

      end
        
		  spawned = true

    end
    
  end

end