<div class="navbar-fixed">
  <nav class="navbar nav-extended <%= 'logged-in' if user_signed_in? %>" style="background-color: <%= @navbar_color.presence || '#2196F3' %>">
    <div class="nav-wrapper">
      <ul>
        <li class="left">
          <a class="sidenav-trigger show-on-large left tooltipped" href="#" data-target="sidenav-left" data-tooltip="Notebook.ai navigation">
            <i class="material-icons">menu</i>
          </a>
        </li>
        <li class="left">
          <a class="sidenav-trigger show-on-large left tooltipped" href="#" data-target="document-sidenav-left" data-tooltip="Your documents">
            <i class="material-icons">file_copy</i>
          </a>
        </li>

        <ul id="about-this-document-dropdown" class="dropdown-content">
          <li style="padding: 0 20px" class="<%= Document.color %> lighten-5">
            <h5>
              About this document
            </h5>
            <p>
              Created <%= time_ago_in_words @document.created_at %> ago
            </p>
            <p class="clearfix">
              Last edited <%= time_ago_in_words @document.updated_at %> ago
            </p>
            <p class="clearfix"></p>
          </li>
          <li class="<%= Document.color %> lighten-5">
            <%= link_to @document.user, class: User.color + '-text' do %>
              <i class="material-icons left"><%= User.icon %></i>
              <span class="black-text">Written by</span>
              <%= @document.user.name %>
            <% end %>
          </li>
          <li class="<%= Document.color %> lighten-5">
            <%= link_to '#universe-picker-modal', class: Universe.color + '-text modal-trigger' do %>
              <i class="material-icons left"><%= Universe.icon %></i>
              <span class="black-text">Set in</span>
              <span class="js-content-universe-name">
                <%= @document.universe.nil? ? 'No Universe Set' : @document.universe.name %>
              </span>
            <% end %>
          </li>
          <li class="divider"></li>
          <li>
            <%= link_to @document, target: '_blank' do %>
              <i class="material-icons left <%= Document.text_color %>"><%= Document.icon %></i>
              Read-only view
            <% end %>
          </li>
          <li>
            <%= link_to plaintext_document_path(@document), target: '_blank' do %>
              <i class="material-icons green-text left">title</i>
              Plaintext view
            <% end %>
          </li>
          <li>
            <%= link_to analysis_document_path(@document) do %>
              <i class="material-icons left orange-text">bar_chart</i>
              Analyze this document
            <% end %>
          </li>
          <li>
            <%= link_to '#', class: 'modal-trigger', data: { target: 'document-notes-modal' } do %>
              <i class="material-icons left <%= Document.text_color %>">edit</i>
              Document notes
            <% end %>
          </li>
          <li>
            <%=
              link_to '#', class: 'share' do
            %>
              <i class="material-icons blue-text left">share</i>
              Share this document
            <% end %>
          </li>
          <li>
            <%= link_to document_document_revisions_path(@document) do %>
              <i class="material-icons green-text left">content_copy</i>
              Revision log
            <% end %>
          </li>
          <% if current_user.can_delete?(@document) %>
            <li class="divider"></li>
            <li>
              <%= link_to @document,
                method: :delete,
                data: {
                  confirm: "Are you sure? This will permanently delete this entire document!"
                } do
              %>
                <i class="material-icons left red-text">delete</i>
                Delete this document
              <% end %>
            </li>
          <% end %>
        </ul>
        <li class="right">
          <%=
            link_to '#', class: 'tooltipped dropdown-trigger', data: {
              position: 'bottom',
              tooltip: 'About this document',
              target: 'about-this-document-dropdown'
            } do
          %>
            <i class="material-icons white-text left"><%= Document.icon %></i>
            <span class="hide-on-med-and-down">About</span>
          <% end %>
        </li>
        <li class="right">
          <a class="sidenav-trigger show-on-large left tooltipped" href="#" data-target="document-entities-right" data-tooltip="Entities in this document">
            <i class="material-icons white-text left">book</i>
            <span class="hide-on-med-and-down">Entities</span>
          </a>
        </li>
        <li class="right">
          <%= link_to analysis_document_path(@document), class: "tooltipped orange btn", data: { tooltip: 'Analyze this document', position: 'bottom' } do %>
            <i class="material-icons hide-on-large-only">bar_chart</i>
            <i class="material-icons left hide-on-med-and-down">bar_chart</i>
            <span class="hide-on-med-and-down">Analyze</span>
          <% end %>
        </li>
        <li class="left" style="margin-left: 20px">
          <%= yield :navbar_left_content %>
        </li>
        <li class="right">
          <%= yield :navbar_right_content %>
        </li>
      </ul>
    </div>
  </nav>
</div>

<%# Modals %>
<%= render partial: 'content/share', locals: { shared_content: @document } %>
<div id="universe-picker-modal" class="modal modal-fixed-footer">
  <div class="modal-content">
    <h4>Set document universe</h4>
    <p>
      What universe does this document exist in? You can change this at any time.
    </p>
    <div class="row">
      <%= form_for @document, method: :put, remote: true do |f| %>
        <div class="col s12 m12 l4">
          <p class="grey-text">No universe</p>
          <p>
            <label class="js-autosave-trigger">
              <%= f.radio_button :universe_id, "nil", class: 'with-gap', checked: @document.universe_id.nil? %>
              <span>No universe</span>
            </label>
          </p>
        </div>
        <% if @linked_entities.any? %>
          <% linked_entity_universes = @linked_entities.values.flatten.map { |de| de.entity.try(:universe) }.uniq.compact %>
          <% if linked_entity_universes.any? %>
            <div class="col s12 m12 l4">
              <p class="grey-text"><%= pluralize linked_entity_universes.count, 'suggested universe' %></p>
              <% linked_entity_universes.each do |universe| %>
                <p>
                  <label class="js-autosave-trigger">
                    <%= f.radio_button :universe_id, universe.id, class: 'with-gap', checked: @document.universe_id == universe.try(:id) %>
                    <span><%= universe.name %></span>
                  </label>
                </p>
              <% end %>
            </div>
          <% end %>
        <% end %>
        <% if @linkables_cache.fetch('Universe', []).any? %>
          <div class="col s12 m12 l4">
            <p class="grey-text">Your universes</p>
            <%# we can't use linkables_cache here since it can be scoped to @current_universe and therefore only return that universe %>
            <% current_user.universes.each do |universe| %>
              <p>
                <label class="js-autosave-trigger">
                  <%= f.radio_button :universe_id, universe.id, class: 'with-gap', checked: @document.universe_id == universe.id %>
                  <span><%= universe.name %></span>
                </label>
              </p>
            <% end %>
          </div>
        <% end %>
        <div class="col s12">
          <p class="grey-text">
            Changes are saved automatically.
          </p>
        </div>
      <% end %>
    </div>
  </div>
  <div class="modal-footer">
    <a href="#!" class="modal-close waves-effect waves-green btn-flat">Close</a>
  </div>
</div>

<%= content_for :javascript do %>
  var universe_picker_form = $('#universe-picker-modal').find('form');

  universe_picker_form.find('.js-autosave-trigger').click(function () {
    var selected_universe_name = $(this).text().trim();

    // console.log('name is now ' + selected_universe_name);

    $('.js-content-universe-name').text(selected_universe_name);
    $('#universe-picker-modal').find('form').submit();
  });
<% end %>