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

function map:on_opening_transition_finished()

  -- By default enemies are not invincible.
  assert(not enemy:is_invincible())

  -- set_invincible(true) with no duration means indefinitely invincible.
  enemy:set_invincible(true, 0)
  assert(enemy:is_invincible())

  sol.timer.start(map, 100, function()
    assert(enemy:is_invincible())

    -- set_invincible(false) removes invincibility immediately.
    enemy:set_invincible(false)
    assert(not enemy:is_invincible())

    -- set_invincible(true) with a duration expires after the given time.
    enemy:set_invincible(true, 50)
    assert(enemy:is_invincible())

    sol.timer.start(sol.main, 100, function()
      assert(not enemy:is_invincible())

      -- set_invincible() with no arguments defaults to true with unlimited duration
      -- (for backward compatibility with quests made before duration was added).
      enemy:set_invincible()
      assert(enemy:is_invincible())

      sol.timer.start(sol.main, 600, function()
        assert(enemy:is_invincible())
        enemy:set_invincible(false)
        assert(not enemy:is_invincible())

        -- Invincibility is paused correctly when the game is suspended.
        enemy:set_invincible(true, 200)
        game:set_suspended(true)
        sol.timer.start(sol.main, 400, function()
          assert(enemy:is_invincible())
          game:set_suspended(false)
        end)
        sol.timer.start(sol.main, 450, function()
          assert(enemy:is_invincible())
        end)
        sol.timer.start(sol.main, 700, function()
          assert(not enemy:is_invincible())

          -- Unlimited invincibility (duration 0) must survive a suspend/resume
          -- cycle: vulnerable_again_date is a sentinel (0 = never expires) and
          -- must not be turned into a real date by the time spent suspended.
          enemy:set_invincible(true, 0)
          game:set_suspended(true)
          sol.timer.start(sol.main, 300, function()
            game:set_suspended(false)
            sol.timer.start(sol.main, 100, function()
              assert(enemy:is_invincible())

              -- Backward compatibility with quests made before the duration
              -- parameter existed: set_invincible() with no duration used to
              -- be equivalent to set_no_attack_consequences(), so an explicit
              -- set_attack_consequence() call afterwards must still be able
              -- to make the enemy vulnerable again to that specific attack,
              -- while attacks that were not overridden stay ignored.
              assert(enemy:get_attack_consequence("sword") == "ignored")
              assert(enemy:get_attack_consequence("arrow") == "ignored")
              enemy:set_attack_consequence("sword", 1)
              assert(not enemy:is_invincible())
              assert(enemy:get_attack_consequence("sword") == 1)
              assert(enemy:get_attack_consequence("arrow") == "ignored")

              sol.main.exit()
            end)
          end)
        end)
      end)
    end)
  end)
end
