<%
   uuid_map = {}
   [@users, @groups].each do |obj_list|
     obj_list.each { |o| uuid_map[o.uuid] = o }
   end
   perm_name_desc_map = {}
   perm_desc_name_map = {}
   perms_json = []
   ['Read', 'Write', 'Manage'].each do |link_desc|
     link_name = "can_#{link_desc.downcase}"
     perm_name_desc_map[link_name] = link_desc
     perm_desc_name_map[link_desc] = link_name
     perms_json << {value: link_name, text: link_desc}
   end
   perms_json = perms_json.to_json
   choose_filters = {
     "groups" => [["group_class", "=", nil]],
   }
   choose_filters.default = []
   owner_icon = fa_icon_class_for_uuid(@object.owner_uuid)
   if owner_icon == "fa-users"
     owner_icon = "fa-folder"
     owner_type = "parent project"
   else
     owner_type = "owning user"
   end
%>

<div class="pull-right">
  <% ["users", "groups"].each do |share_class| %>

  <%= link_to(send("choose_#{share_class}_path",
      title: "Share with #{share_class}",
      by_project: false,
      preview_pane: false,
      multiple: true,
      limit: 10000,
      filters: choose_filters[share_class].to_json,
      action_method: 'post',
      action_href: share_with_project_path,
      action_name: 'Add',
      action_data: {selection_param: 'uuids[]', success: 'tab-refresh'}.to_json),
      class: "btn btn-primary btn-sm", remote: true, method: 'get') do %>
  <i class="fa fa-fw fa-plus"></i> Share with <%= share_class %>&hellip;
  <% end %>

  <% end %>
</div>

<p>Permissions for this project are inherited from the <%= owner_type %>
  <i class="fa fa-fw <%= owner_icon %>"></i>
  <%= link_to_if_arvados_object @object.owner_uuid, friendly_name: true %>.
</p>

<table id="project_sharing" class="topalign table" style="clear: both; margin-top: 1em;">
  <tr>
    <th>User/Group Name</th>
    <th colspan="2">Project Access</th>
  </tr>

  <% @share_links.andand.each do |link|
       shared_with = uuid_map[link.tail_uuid]
       if shared_with.nil?
         link_name = link.tail_uuid
       elsif shared_with.respond_to?(:full_name)
         link_name = shared_with.full_name
       else
         link_name = shared_with.name
       end %>
  <tr data-object-uuid="<%= link.uuid %>">
    <td>
      <i class="fa fa-fw <%= fa_icon_class_for_uuid(link.tail_uuid) %>"></i>
      <%= link_to_if_arvados_object(link.tail_uuid, link_text: link_name) %>
    </td>
    <td><%= link_to perm_name_desc_map[link.name], '#', {
      "data-emptytext" => "Read",
      "data-placement" => "bottom",
      "data-type" => "select",
      "data-url" => url_for(action: "update", id: link.uuid, controller: "links", merge: true),
      "data-title" => "Set #{link_name}'s access level",
      "data-name" => "[name]",
      "data-pk" => {id: link.tail_uuid, key: "link"}.to_json,
      "data-value" => link.name,
      "data-clear" => false,
      "data-source" => perms_json,
      "data-tpl" => "<select id=\"share_change_level\"></select>",
      "class" => "editable form-control",
      } %>
    </td>
    <td>
      <%= link_to(
          {action: 'destroy', id: link.uuid, controller: "links"},
          {title: 'Revoke', class: 'btn btn-default btn-nodecorate', method: :delete,
           data: {confirm: "Revoke #{link_name}'s access to this project?",
                  remote: true}}) do %>
      <i class="fa fa-fw fa-trash-o"></i>
      <% end %>
    </td>
  </tr>
  <% end %>
</table>