<div id="<%= category[:name].gsub("'", '') %>_panel" class="row panel">
  <%= render partial: 'content/form/images/gallery',  locals: { content: content } if category[:name] == 'gallery' %>
  <%= render partial: 'content/display/contributors', locals: { content: content } if category[:name] == 'contributors' %>
  <%= render partial: 'content/display/documents',    locals: { content: content } if category[:name] == 'documents' %>

  <% category[:fields].select { |field| !field[:hidden] }.each do |serialized_field| %>
    <% if serialized_field[:label].start_with?("Private") %>
      <% next unless user_signed_in? && (
         (content.raw_model.is_a?(Universe) && content.user == current_user) ||
         (content.raw_model.respond_to?(:universe) && content.universe      && content.universe.user == current_user) ||
         (content.raw_model.respond_to?(:universe) && content.universe.nil? && content.user == current_user)
       )
      %>
    <% end %>

    <%
      value = case serialized_field[:type]
        when 'universe'
          Universe.find_by(id: serialized_field[:value].to_i)

        when 'name', 'text_area'
          serialized_field[:value]

        when 'link'
          if serialized_field[:old_column_source].present? && content.raw_model.respond_to?(serialized_field[:old_column_source].to_sym)
            serialized_field[:value]
          else
            # todo uhhhh is this just a safety guard or are there actual cases of bad data triggering here?
            nil
          end

        when 'tags'
          content.page_tags

        else
          raise "unknown field type = " + serialized_field[:type].inspect
      end
    %>
    <% next if value.blank? %>

    <div class="row">
      <div class="col s3 m3 l3 right-align flow-text grey-text">
        <%= serialized_field[:label] %>
      </div>

      <% if serialized_field[:type] == 'universe' %>
        <div class="col s9 m9 l9 flow-text">
          <%= link_to(value.name_field_value, value) if value.is_a?(Universe) %>
        </div>

      <% elsif serialized_field[:type] == 'link' %>
        <% klass = serialized_field[:value].klass %>
        <div class="col s9 m9 l9 flow-text">
          <% value.each do |li| %>
            <div class="chip">
              <%= link_to li do %>
                <span class="<%= klass.color %>-text">
                  <i class="material-icons left">
                    <%= klass.icon %>
                  </i>
                </span>
                <%= li.name_field_value %>
              <% end %>
            </div>
          <% end %>
        </div>

      <% elsif serialized_field[:type] == 'tags' %>
        <div class="tags-container">
          <% value.each do |tag| %>
            <% if user_signed_in? && @content.user == current_user %>
              <%=
                link_to send(
                  "page_tag_#{content.raw_model.class.name.downcase.pluralize}_path",
                  slug: PageTagService.slug_for(tag)
                ) do
              %>
                <span class="new badge <%= @content.class.color %> left" data-badge-caption="<%= tag %>"></span>
              <% end %>
            <% else %>
              <span class="new badge <%= @content.class.color %> left" data-badge-caption="<%= tag %>"></span>
            <% end %>
          <% end %>
        </div>

      <% else # serialized_field[:type] == text_area, name %>
        <div class="col s9 m9 l9 markdownable">
          <%=
            simple_format ContentFormatterService.show(
              text: value,
              viewing_user: current_user
            )
          %>&nbsp;
        </div>
      <% end %>
    </div>
  <% end %>
</div>
