local map = ...
local game = map:get_game()

local expected_jump_time
local finished = false

function hero:on_state_changed(state)

  if state == "jumping" then
    assert_equal(sol.main.get_elapsed_time(), expected_jump_time)
  end
end

function map:on_opening_transition_finished()

  sol.timer.start(map, 6000, function()
    assert_equal(finished, true)
    sol.main.exit()
  end)

  -- Built-in states (like the default "free" state) have no way to
  -- customize the jumper delay themselves: they always use the delay
  -- of the jumper being triggered.
  assert_equal(hero:get_state(), "free")
  assert_equal(jumper_1:get_delay(), 200)
  hero:set_animation("walking")
  game:simulate_command_pressed("down")

  function jumper_1_before_sensor:on_activated()
    assert_equal(hero:get_state(), "free")
    expected_jump_time = sol.main.get_elapsed_time() + jumper_1:get_delay()
  end
end

function jumper_1_after_sensor:on_activated()

  assert_equal(hero:get_state(), "jumping")

  jumper_2:set_delay(600)
  assert_equal(jumper_2:get_delay(), 600)
  hero:set_animation("walking")
end

function jumper_2_before_sensor:on_activated()
  assert_equal(hero:get_state(), "free")
  expected_jump_time = sol.main.get_elapsed_time() + jumper_2:get_delay()
end

function jumper_2_after_sensor:on_activated()

  assert_equal(hero:get_state(), "jumping")

  jumper_3:set_delay(300)
  assert_equal(jumper_3:get_delay(), 300)
  hero:set_animation("walking")
end

function jumper_3_before_sensor:on_activated()
  assert_equal(hero:get_state(), "free")
  expected_jump_time = sol.main.get_elapsed_time() + jumper_3:get_delay()
end

function jumper_3_after_sensor:on_activated()

  assert_equal(hero:get_state(), "jumping")
  finished = true
  sol.main.exit()
end
