<%
  content_type_name  = content_type.to_s.titleize                 # "Character"
  content_type_class = content_class_from_name(content_type_name) # Character
  content_type_pluralized = content_type_name.downcase.pluralize  # characters

  category_ids_for_this_content_type = AttributeCategory.where(entity_type: content_type_name.downcase, user_id: current_user).pluck(:id)
  name_field = AttributeField.find_by(field_type: 'name', attribute_category_id: category_ids_for_this_content_type)

  content_ids = content_list.pluck(:id)
  if name_field
    list_name_lookup_cache = Hash[
      name_field.attribute_values.where(
        entity_type: content_type_name
      ).pluck(:entity_id, :value)
    ]
  else
    list_name_lookup_cache = {}
  end
%>

<div class="hoverable card">
  <div class="card-image">
    <% images = ImageUpload.where(content_type: content_type_name, content_id: content_list.pluck(:id)) %>
    <%
      category_image = if rand(3) < 2 && images.any?
        images.sample.src(:original)
      else
        asset_path("card-headers/#{content_type_pluralized}.jpg")
      end
    %>
    <div class="activator mp-iucl-view-list" style="height: 300px; background: url('<%= category_image %>'); background-size: cover;"></div>
    <span class="card-title bordered-text" style="font-size: 200%">
      <% if defined?(card_title) && !card_title.blank? %>
        <%= card_title %>
      <% else %>
        <%= pluralize(content_list.count, content_type.to_s) %>
      <% end %>
    </span>
    <% if user_signed_in? %>
      <%= link_to new_polymorphic_path(content_type), class: "btn-floating btn-large hoverable halfway-fab waves-effect waves-light #{content_type_class.color} tooltipped #{'grey' unless current_user.can_create?(content_type_class)}",
        data: {
          position: 'bottom',
          tooltip: current_user.can_create?(content_type_class) ? "Create a#{'n' if %w(a e i o u).include?(content_type.to_s[0]) && content_type.to_s != 'universe'} #{content_type.to_s}"
                                                                : "#{content_type.to_s.pluralize.titleize} require a Premium subscription to create.",
          enterDelay: 50
        } do %>
        <i class="material-icons">add</i>
      <% end %>
    <% end %>

    <% if defined?(@content) && @content.is_a?(Universe) && content_type != :universe && content_type.to_s.downcase != 'universe' %>
      <%= link_to send("#{content_type_pluralized}_universe_path", { id: @content.id }), 
        class: "btn-floating hoverable halfway-fab waves-effect waves-light grey black-text tooltipped mp-iucl-view-all", 
        style: "right: 90px",
        data: {
          position: 'bottom',
          tooltip: "View all #{content_type_pluralized} in this universe",
          enterDelay: 50
        } do %>
        <i class="material-icons">reorder</i>
      <% end %>
    <% else %>
      <%= link_to polymorphic_path(content_type_class), 
        class: "btn-floating hoverable halfway-fab waves-effect waves-light grey black-text tooltipped mp-iucl-view-all", 
        style: "right: 90px",
        data: {
          position: 'bottom',
          tooltip: "View all #{content_type_pluralized}",
          enterDelay: 50
        } do %>
        <i class="material-icons">reorder</i>
      <% end %>
    <% end %>
  
  </div>
  <div class="card-content">
  </div>
  <div class="card-reveal">
    <span class="card-title">
      <% if content_list.any? %>
        <i class="material-icons <%= content_type_class.text_color %>" style="position: relative; top: 3px">
          <%= content_type_class.icon %>
        </i>
      <% end %>
      <% if defined?(@content) && @content.is_a?(Universe) && content_type != :universe && content_type.to_s.downcase != 'universe' %>
        <%= link_to content_type_pluralized.titleize, send("#{content_type_pluralized}_universe_path", { id: @content.id }), class: "#{content_type_class.text_color}" %>
      <% else %>
        <%= content_type_pluralized.titleize %>
      <% end %>
      <i class="material-icons right">close</i>
    </span>
    <% if content_list.any? %>
      <table class="content-table bordered">
        <tr>
          <th>Name</th>
          <th class="hide-on-small-only"><!-- Description --></th>
          <th></th>
        </tr>
        <% content_list.each do |content| %>
        <tr>
          <td>
            <%= link_to polymorphic_path(content.page_type.downcase, id: content.id) do %>
              <i class="material-icons left <%= content_type_class.text_color %>"><%= content_type_class.icon %></i>
              <%= list_name_lookup_cache[content.id].presence || content.name.presence || "Untitled #{content.page_type}" %>
            <% end %>
          </td>
          <td class="hide-on-small-only"><%= truncate(content.description, length: 200) %></td>
          <td>
            <div class="secondary-content">
              <%= link_to edit_polymorphic_path(content.page_type.downcase, id: content.id) do %>
                <i class="material-icons">edit</i>
              <% end %>
            </div>
          </td>
        </tr>
        <% end %>
      </table>
      <div class="center" style="margin-top: 30px">
        <% if defined?(@content) && @content.is_a?(Universe) && content_type != :universe && content_type.to_s.downcase != 'universe' %>
          <%= link_to "Browse #{content_type_pluralized}", send("#{content_type_pluralized}_universe_path", { id: @content.id }), class: "btn #{content_type_class.color} lighten-1" %>
        <% else %>
          <div class="center">
            <% button_text = content_list.any? ? "Create another #{content_type}" : "Create one" %>
            <%= link_to button_text, new_polymorphic_path(content_type), class: "btn #{content_type_class.color}" if user_signed_in? %>
          </div>
        <% end %>
      </div>
    <% elsif content_list.empty? %>
      <div class="row">
        <div class="col s4">
          <h1><i class="material-icons <%= content_type_class.text_color %> text-lighten-2" style="font-size: 150%">
            <%= content_type_class.icon %>
          </i></h1>
        </div>
        <div class="col s8">
          <p>
            <%= t("content_descriptions.#{content_type}") %>
          </p>
          <div class="center hide-on-small-only">
            <% button_text = content_list.any? ? "Create another #{content_type}" : "Create one" %>
            <%= link_to button_text, new_polymorphic_path(content_type), class: "btn #{content_type_class.color}" if user_signed_in? %>
          </div>
        </div>
        <div class="center hide-on-med-and-up">
          <% button_text = content_list.any? ? "Create another #{content_type}" : "Create one" %>
          <%= link_to button_text, new_polymorphic_path(content_type), class: "btn #{content_type_class.color}" if user_signed_in? %>
        </div>
      </div>
    <% end %>
    <br />

  </div>
</div>
