<%
   @project=@resource unless @project || selected_section==Navigation::SECTION_HOME
   period_param = "period=#{u(params[:period])}" if params[:period]
   displayed_resource = @resource || @project
   display_only_root = @issue
   resource_id=displayed_resource ? (displayed_resource.is_a?(Fixnum) ? displayed_resource : displayed_resource.permanent_id) : nil
%>


<script>
  window.navbarOptions.set({
    contextId: '<%= @project.id -%>',
    contextKey: '<%= @project.key -%>',
    contextUuid: '<%= @project.uuid -%>',
    contextComparable: <%= controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'comparable') -%>,
    canFavoriteContext: <%= logged_in? && displayed_resource && !display_only_root ? 'true' : 'false' -%>,
    isContextFavorite: <%= logged_in? && current_user.favourite?(resource_id) ? 'true' : 'false' -%>,
    <% if @snapshot && @snapshot.version.present? %>
    contextVersion: '<%= @snapshot.version -%>',
    <% end %>
    <% if @snapshot %>
    contextDate: '<%= @snapshot.created_at.strftime('%FT%T%:z') -%>',
    <% end %>
    contextDashboards: [
      <% dashboards = ActiveDashboard.user_dashboards(current_user, false)
         dashboards.each_with_index do |active_dashboard, index| %>
      {
        url: '/dashboard/index?id=<%= @project.key -%>&did=<%= active_dashboard.dashboard_id -%><%= "&"+period_param if period_param -%>',
        name: '<%= escape_javascript active_dashboard.dashboard.name(true) -%>'
      }<% if index < dashboards.size - 1 -%>, <% end %>
      <% end %>
    ],
    contextExtensions: [
      <% project_metrics = @project.last_snapshot.metric_keys.to_java(:string) if @project.last_snapshot
         pages = controller.java_facade.getPages(Navigation::SECTION_RESOURCE.key, @project.scope, @project.qualifier, @project.language, project_metrics)
         pages.each_with_index do |page, index|
           page_url = (page.isController() ? "#{page.getId()}?id=#{@project.id}" : "/plugins/resource/#{@project.id}?page=#{page.getId()}")
      %>
      {
        url: '<%= page_url -%>',
        label: '<%= escape_javascript message(page.getId() + '.page', :default => page.getTitle()) -%>'
      }<% if index < pages.size - 1 -%>, <% end %>
      <% end %>
    ]
  });

  <% if (has_role?(:admin, displayed_resource) || profiles_administrator?) && !displayed_resource.entity? %>
  <% is_admin = has_role?(:admin, @project) %>
  window.navbarOptions.set({
    contextConfiguration: [
      <% if (is_admin && controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'configurable')) %>
      {
        url: '/project/settings/<%= @project.id -%>',
        name: '<%= escape_javascript message('project_settings.page') -%>'
      },
      <% end %>
      <% if (@project.project?) %>
      {
        url: '/project/profile/<%= @project.id -%>',
        name: '<%= escape_javascript message('project_quality_profiles.page') -%>'
      },
      <% end %>
      <% if (@project.project?) %>
      {
        url: '/project/qualitygate/<%= @project.id -%>',
        name: '<%= escape_javascript message('project_quality_gate.page') -%>'
      },
      <% end %>
      <% if is_admin %>
      {
        url: '/manual_measures/index/<%= @project.id -%>',
        name: '<%= escape_javascript message('manual_measures.page') -%>'
      },
      <% end %>
      <% if (is_admin && @project.project?) %>
      {
        url: '/action_plans/index/<%= @project.id -%>',
        name: '<%= escape_javascript message('action_plans.page') -%>'
      },
      <% end %>
      <% if (is_admin && @project.project?) %>
      {
        url: '/project/links/<%= @project.id -%>',
        name: '<%= escape_javascript message('project_links.page') -%>'
      },
      <% end %>
      <% if is_admin && controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'hasRolePolicy') %>
      {
        url: '/project_roles/index/<%= @project.id -%>',
        name: '<%= escape_javascript message('permissions.page') -%>'
      },
      <% end %>
      <% if is_admin && controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'modifiable_history') %>
      {
        url: '/project/history/<%= @project.id -%>',
        name: '<%= escape_javascript message('project_history.page') -%>'
      },
      <% end %>
      <% if is_admin && controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'updatable_key') %>
      {
        url: '/project/key/<%= @project.id -%>',
        name: '<%= escape_javascript message('update_key.page') -%>'
      },
      <% end %>
      <% if is_admin && controller.java_facade.getResourceTypeBooleanProperty(@project.qualifier, 'deletable') %>
      {
        url: '/project/deletion/<%= @project.id -%>',
        name: '<%= escape_javascript message('deletion.page') -%>'
      },
      <% end %>
      <% if is_admin
          controller.java_facade.getPages(Navigation::SECTION_RESOURCE_CONFIGURATION.key, @project.scope, @project.qualifier, @project.language, nil).each do |page|
            page_url = "#{page.getId()}?resource=#{@project.id}"
      %>
      {
        url: '<%= page_url -%>',
        name: '<%= escape_javascript message(page.getId() + '.page', :default => page.getTitle()) -%>'
      },
      <% end end %>
    ]
  });
  <% end %>

  window.navbarOptions.set({
    contextBreadcrumbs: [
      <%
       resource_link = {:controller => params[:controller], :action => params[:action]}
       if display_only_root
         resource_link = {:controller => 'dashboard', :action => 'index'}
       end

       # ======== Path for resources ========

       if displayed_resource
         if displayed_resource.last_snapshot
           resources=[]
           s=displayed_resource.last_snapshot
           while (s!=nil) do
             resources.insert(0, s.project)
             s=s.parent
           end
         else
           resources = [displayed_resource]
         end
    %>
      <%
         resources.each do |resource|
         root = "/#{u resource_link[:controller]}/#{u resource_link[:action]}?"
         path = ''
         query = request.query_parameters
         query[:id] = resource.key
         query.each do |key, value|
           path += '&' unless path.empty?
           path += "#{u key}=#{u value}"
         end
      %>
      {
        q: '<%= resource.qualifier -%>',
        url: '<%= root + path -%>',
        name: '<%= escape_javascript resource.name -%>'
      },
      <%
         end
         end
      %>
    ]
  });
</script>
