local mod = get_mod("RejectInvitesWhileInMission")

mod.in_mission = false

mod:hook(CLASS.PartyImmateriumManager, "_handle_immaterium_invite", function (func, _self, party_id, invite_token, inviter_account_id)
  if not mod:is_enabled() then return end

  if not mod.in_mission then
    func(_self, party_id, invite_token, inviter_account_id)
  else
    _self:_decline_party_invite(party_id, invite_token)
  end
end)

mod:hook_safe(CLASS.StateGameplay, "on_enter", function(_self, parent, params, creation_context, ...)
  if not mod:is_enabled() then return end

  local mission_name = params.mission_name or "NONE"
  local mission_id = params.mechanism_data and params.mechanism_data.backend_mission_id

  --Entering an actual mission with ID
  if mission_id or (mission_name ~= "NONE" and mission_name ~= "hub_ship") then
    mod.in_mission = true
  end
end)

mod:hook_safe(CLASS.GameModeManager, "rpc_game_mode_end_conditions_met", function(...)
  if not mod:is_enabled() then return end

  mod.in_mission = false
end)

mod:hook_safe("GameplayStateRun", "on_enter", function(...)
  if not mod:is_enabled() then return end

  if Managers.state and Managers.state.game_mode
  and Managers.state.game_mode:game_mode_name() == "hub" then
    mod.in_mission = false
  end
end)

mod:hook_safe(CLASS.MainMenuView, "on_enter", function(self, profile, widget)
  if not mod:is_enabled() then return end

  mod.in_mission = false
end)