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

-- #2022: custom_state:on_update() must not be called while the game
-- (and therefore the state) is suspended.

local state = sol.state.create("test_2022")
state:set_gravity_enabled(false)  -- Avoid the free state taking over.

local update_count = 0
function state:on_update()
  update_count = update_count + 1
end

function map:on_opening_transition_finished()

  hero:start_state(state)

  sol.timer.start(sol.main, 100, function()
    local count_before_pause = update_count
    assert(count_before_pause > 0, "state:on_update() should have been called while running")

    game:set_suspended(true)

    later(function()
      -- The state is suspended: on_update() must not have been called again.
      assert_equal(update_count, count_before_pause)

      game:set_suspended(false)

      later(function()
        -- The state is resumed: on_update() must be called again.
        assert(update_count > count_before_pause, "state:on_update() should resume being called after unsuspending")
        sol.main.exit()
      end)
    end)
  end)
end
