-------------------------------------------------------------------------------
-- ****************************************************************************
--  Entity: Final Boss
--  State: Jump out of Water (3)
--  Phase: 3 of 3
-- ****************************************************************************
-------------------------------------------------------------------------------

--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                              Variables                              /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]

-- Define the table that will contain the state
MegaWhiteShark_Phase3_JumpOutOfWater = {}

-- Name of state
local STATE_NAME = "MegaWhiteShark_Phase3_JumpOutOfWater" -- a string
registerStateFSM( STATE_NAME )  -- do NOT touch this!!

-- Name of the state to change to when the necesary conditions are met
local OUT_STATE = "MegaWhiteShark_Phase3_Bite"
  
-- Animation
local ANIMATION_NAME = "bite" -- a string
local ANIMATION_TIME = (450 - 410)/30.0 -- nş frames / framerate = time duration
ANIMATION_TIME = ANIMATION_TIME - 0.05 -- to avoid cuts in animation
local timer_animation = CTimer( ANIMATION_TIME * 3 ) -- do NOT touch this!!

-- Timers
local TIME_TO_DIE = 3.0
local timer_Die = CTimer( TIME_TO_DIE ) -- do NOT touch this!!

local HAS_RECEIVED_HIT = false

-- Es una variable global que afecta a als events tambe!
HAS_DESTROYED_FINAL_LAKE_PLANT = false


--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                               Functions                             /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]
local function checkIfLakePlantIsDestroyed( entity )

  -- si el finalBoss ha sobrepassat tocant amb la boca la lakeplant
  -- ens la carreguem
  local entity_pos = getEntityPositionByID( entity:ID() )
  local plant_pos = getEntityPosition( MWS_LAST_LAKE_PLANT )
  
  -- fer-ho amb timers
  
  if( entity_pos.y > plant_pos.y ) then

    -- OPCIONAL: forçar que el player tambe salti o posar bafarada per a que ho faci!
    forcePlayerJump()
    
    -- 1) Activate Slow Motion
    setSlowMotion( true )
    
    -- Activate and desativate music
    playSound( "Animated_entities/Common_events/Hit_impacts/water_splash_slow", entity_pos );
    playSound( "Animated_entities/Final_Boss/finalBite", entity_pos );
    playAmbient("Game_situations/heart_timeToAttack"); 
    stopAmbient("Game_situations/heart_timeToWait", false );

    -- TODO: setSlowMotion( true, durada del Slow Motion, velocitat inicial, velocitat final )
    
    -- Activem el control al player 
    activateInput(true);
    enableCinematicBlackBands( false );
    
    enableFinalHitGUI( true );
    
    -- fer invisible la plataforma
    invalidateEntityByName( MWS_LAST_LAKE_PLANT )
    
    -- marcar lakeplant final com a destruida
    HAS_DESTROYED_FINAL_LAKE_PLANT = true
  end

end

--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                               States                                /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]

-------------------------------------------------------------------------------
-- Enter
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_JumpOutOfWater["Enter"] = function(entity)

  -- ja no estem buceando!
  entity:setDiveMode( false )
  
  -- El boss ha d'activar el final bite per a que tot funcioni be
  setFinalBossFinalBite( true )
  
  -- 2) set the finalBoss just under the lakeplant
  local final_lake_plant_pos = getEntityPosition( MWS_LAST_LAKE_PLANT )
  final_lake_plant_pos.y = final_lake_plant_pos.y - 5
  setEntityPosition( entity:ID(), final_lake_plant_pos )
  
  -- 3) block WASD for the player
  lockPlayerMovement()
  
  -- tenir en compte que el TIMER ha de retornar el temps en camara lenta tambe? o simplement
  -- es pot incrementar el temps que dura el timer multiplicat per 1/(factor de ralentitzacio)
  -- play animation and restart the timer
  playAnimationAction( entity:ID(), ANIMATION_NAME )
  timer_animation:restart()
  timer_Die:restart()

  HAS_RECEIVED_HIT = false
  HAS_DESTROYED_FINAL_LAKE_PLANT = false
  
  -- prova de posar la camara a seguir el player
  setCameraMode( 4, "", "trackerCamera_LastHit_Picat", 50.0 )
  --setCameraMode( 4, "", "trackerCamera_LastHit_Picat_Vertical", 50.0 )
  --setCameraMode( 0, "", "", 0.0 );
  
  -- Si esta en aquest estat recuperem a partir d'aqui
  is_in_Stage4_finalBoss = true;
  
  -- tirar particules de Mega Chof!
  throwAreaParticles( "unhide_water_big", getEntityPosition( MWS_LAST_LAKE_PLANT ), 2, 6 );
  
  -- activate collions in finalZone bonus
  setEntityValidByName("platform_vertical_bonusZone", true );
  setEntityVisibleByName("platform_vertical_bonusZone", false );
  
  setEntityValidByName("platform_horitzontal_bonusZone", true );
  setEntityVisibleByName("platform_vertical_bonusZone", false );
   
  setEntityValidByName("platform_horitzontal_stopPicat", true);

end


-------------------------------------------------------------------------------
-- Execute
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_JumpOutOfWater["Execute"] = function(entity)

  -- empitjar el boss cap amunt en direccio player
  local player_pos = getPlayerPosition()
  local enemy_pos = getEntityPositionByID( entity:ID() )
  local direction = player_pos - enemy_pos
  --direction.y = 0.0 -- this will push the final boss only in direction XZ
  
  local dist_to_player_sq = distanceSquared( player_pos, enemy_pos )
  local speed = 10--math.sqrt( dist_to_player_sq )
  
  -- empitjar al boss en direccio cap al player
  pushEntity( entity:ID(), direction, speed )
  
  -- si el Boss NO ha destrossat la ultima lakeplant... comprovar si l'ha de destrossar
  if not( HAS_DESTROYED_FINAL_LAKE_PLANT ) then
    checkIfLakePlantIsDestroyed( entity )
  end

  -- si el player ha pegat al boss, rebre el hit :)
  if( HAS_RECEIVED_HIT ) then
    changeEntityState( entity:ID(), "MegaWhiteShark_Phase3_ReceiveHit" )
  end

  -- si ha passat el temps, mossegar al player
  if( timer_animation:isOutOfTime() ) then
    setSlowMotion( false )
    changeEntityState( entity:ID(), OUT_STATE )
  end
end


-------------------------------------------------------------------------------
-- Exit
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_JumpOutOfWater["Exit"] = function(entity)
  setFinalBossFinalBite( false )
  unlockPlayerMovement()
  enableFinalHitGUI( false );
  
  -- Recover sound state
  stopSound( "Animated_entities/Final_Boss/finalBite" );
  stopSound( "Animated_entities/Common_events/Hit_impacts/water_splash_slow");
  stopAmbient("Game_situations/heart_timeToAttack", false );
  
end


-------------------------------------------------------------------------------
-- OnMessage
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_JumpOutOfWater["OnMessage"] = function(entity, message)

  -- Receive Hit
  if ( message.Msg == Telegram.Receive_Hit ) then
    HAS_RECEIVED_HIT = true
    return true
  end
  
  -- if message NOT handled, return false
  return false
  
end


-------------------------------------------------------------------------------
-- GetNameOfCurrentState
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_JumpOutOfWater["GetNameOfCurrentState"] = function()
  return STATE_NAME
end
