------ All content © 2009 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Justin Whitney
-- Created: 08/27/2009


local ASSETS_XML = "../../Assets/Xml/"

local GLOBAL_CreateEntityAt          = Global.CreateEntityAt
local GLOBAL_TimeElapsed             = Global.TimeElapsed

local ENTITY_SHR_gPosition           = Entity.SHR_gPosition
local ENTITY_SHR_sPosition           = Entity.SHR_sPosition
local ENTITY_SHR_sVelocity           = Entity.SHR_sVelocity
local ENTITY_SPT_RefreshNumbers      = Entity.SPT_RefreshNumbers
local ENTITY_SPT_AddNumber           = Entity.SPT_AddNumber
local ENTITY_SPT_GiveNumber          = Entity.SPT_GiveNumber
local ENTITY_SPT_GiveString          = Entity.SPT_GiveString

local ENTITY = Entity
local pos = ENTITY_SHR_gPosition( ENTITY )
 
------------------------------------------------------------------------------
-- CONSTANTS
                 
local STRINGS  = { 
                   { ["poem"] = "Can you remember what you are never told?",                ["musicFile"] = "1" },
                   { ["poem"] = "Frail, gossamer silence,~ Placed away and forgotten ...",  ["musicFile"] = "1" },
                   { ["poem"] = "Can I fill the hidden spaces~ With all the words I know?", ["musicFile"] = "1" }
                 }

local NUMBERS  = {
                   { ["lit"] = 0, ["musicLayer"] = 1, ["musicAction"] = 3, ["FFHorde"] = 150 },
                   { ["lit"] = 0, ["musicLayer"] = 2, ["musicAction"] = 3, ["FFHorde"] = 80 },
                   { ["lit"] = 0, ["musicLayer"] = 3, ["musicAction"] = 3, ["FFHorde"] = 40 }
                 }
                 
local PIECES_1 = { 
                   { ["y"] =  33.00, ["file"] = "entities/e_StandingLantern.xml", ["strings"] = STRINGS[1], ["numbers"] = NUMBERS[1] },
                   { ["y"] =   0.00, ["file"] = "island_parts/umbrella_tear.xml"},
                   { ["y"] =  25.50, ["file"] = "island_parts/umbrella_in.xml" },
                   { ["y"] =   5.00, ["file"] = "island_parts/umbrella_out.xml" }
                 }
                    
local PIECES_2 = { 
                   { ["y"] = -32.50, ["file"] = "entities/e_StandingLantern.xml", ["strings"] = STRINGS[2], ["numbers"] = NUMBERS[2] },
                   { ["y"] = -20.00, ["file"] = "island_parts/umbrella_in.xml" },
                   { ["y"] = -20.00, ["file"] = "island_parts/umbrella_mid.xml" },
                   { ["y"] = -40.00, ["file"] = "island_parts/umbrella_in.xml" } 
                 }

local PIECES_3 = {
                   { ["y"] =   5.00, ["file"] = "entities/e_StandingLantern.xml", ["strings"] = STRINGS[3], ["numbers"] = NUMBERS[3] },
                   { ["y"] = - 2.50, ["file"] = "island_parts/umbrella_in.xml" }
                 }
                 
local ENTITIES = {
                   nil,
                   PIECES_2,
                   nil,
                   PIECES_3,
                   nil,
                   nil,
                   nil
                 }
                 
local YZERO    = pos.y
local SPEED    = -0.002
local BARRIERS = { 
                   PIECES_1[1], 
                   -65.00, 
                   PIECES_2[1], 
                   -95.00, 
                   PIECES_3[1],
                   -105.00,
                   nil 
                 }
                 
local VEL      = {
                   nil,
                   { [PIECES_1[2]] = SPEED },
                   nil,
                   { [PIECES_1[2]] = SPEED },
                   nil,
                   { [PIECES_1[2]] = SPEED, [PIECES_3[1]] = SPEED, [PIECES_3[2]] = SPEED },
                   nil
                 }
                 
local MY_VEL   = {
                    0.0,
                    SPEED,
                    0.0,
                    SPEED,
                    0.0,
                    SPEED,
                    0.0
                 }

------------------------------------------------------------------------------
-- VARS
local state = 1
local yOff  = 0
local dt    = 0
local temp
 
------------------------------------------------------------------------------
-- FUNCTIONS
local function MovePieces( pieces )
  if( pieces ~= nil ) then
    for key,val in pairs( pieces ) do
      temp = ENTITY_SHR_gPosition( key.ptr )
      temp.y = temp.y + val * dt
      ENTITY_SHR_sPosition( key.ptr, temp.x, temp.y, temp.z )
    end
  end
end

local function AddPairs( entPtr, pairTable, func )
  if( pairTable ~= nil ) then
    for key,val in pairs( pairTable ) do
      func( entPtr, key, val ) 
    end
  end
end

------------------------------------------------------------------------------
local function AddEntities( ents )
  if( ents ~= nil ) then
    for i,ent in ipairs( ents ) do
      if( ent.file == "entities/e_StandingLantern.xml") then
        ent.ptr = GLOBAL_CreateEntityAt( ASSETS_XML .. ent.file, pos.x, pos.y + ent.y, pos.z, -90, 0, 0 )
      else
        ent.ptr = GLOBAL_CreateEntityAt( ASSETS_XML .. ent.file, pos.x, pos.y + ent.y, pos.z, 0, 0, 0 )
      end
      AddPairs( ent.ptr, ent.strings, ENTITY_SPT_GiveString )
      AddPairs( ent.ptr, ent.numbers, ENTITY_SPT_AddNumber )
      -- Hackiest hack in the history of hacks
      Editor:AttachToIsland( 0, ent.ptr:GetMyPointer(), ent.file )
    end 
  end
end

------------------------------------------------------------------------------
local function NextState()
  if( VEL[state] ~= nil ) then
    for key,val in pairs( VEL[state] ) do
      ENTITY_SHR_sVelocity( key.ptr, 0, 0, 0 )   --stop moving objects from last state 
    end
  end
  state = state + 1
  ENTITY_SHR_sVelocity( ENTITY, 0, MY_VEL[state], 0 )
  AddEntities( ENTITIES[state] )                 --Add the necessary entities for the state
  if( VEL[state] ~= nil ) then
    for key,val in pairs( VEL[state] ) do
      ENTITY_SHR_sVelocity( key.ptr, 0, val, 0 ) --start moving objects this state
    end
  end
end

-----------------------------------------------------------------------------
AddEntities( PIECES_1 )

while( 1 ) 
do
  coroutine.yield()
  dt = GLOBAL_TimeElapsed()
  pos = ENTITY_SHR_gPosition( ENTITY )
  pos.y = pos.y + MY_VEL[state] * dt
  MovePieces( VEL[state] )
  
  if( type(BARRIERS[state]) == "table" ) then
    ENTITY_SPT_RefreshNumbers( BARRIERS[state].ptr, BARRIERS[state].numbers )
    if( BARRIERS[state].numbers.lit ~= nil and BARRIERS[state].numbers.lit ~= 0 ) then
      NextState()
    end
  elseif( type(BARRIERS[state]) == "number" and pos.y < YZERO + BARRIERS[state] ) then
    NextState()
  end
  
  ENTITY_SHR_sPosition( ENTITY, pos.x, pos.y, pos.z )
  
end
