{#-
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-#}
{% import 'allura:templates/jinja_master/lib.html' as lib with context %}
{% import g.theme.jinja_macros as theme_macros with context %}
{% set n = c.project.neighborhood %}

{% if c.project %}
{% set files_appconfig = c.project.app_config_by_tool_type('files') %}
{% endif %}

{% if not c.project or (n.neighborhood_project == c.project and not n.show_title) %}
  <div id="nav_menu_missing"></div>
{% else %}
    {% if not c.project.is_user_project %}
    {{ theme_macros.breadcrumbs(c.project, c.app) }}
    {% endif %}
    {{ theme_macros.project_header_right(c.project, c.app) }}
    {% if c.project.is_user_project %}
      {{lib.gravatar(c.project.user_project_of, size=48, className='project_icon')}}
    {% elif c.project.icon %}
      <img class="project_icon" alt="Project Logo" {{ lib.project_icon_srcs(c.project) }}>
    {% endif %}
        <h1 class="project_title">
            <a href="{{c.project.url()}}" class="project_link">
                {%- if c.project.is_user_project -%}
                    {{ c.project.user_project_of.display_name }}
                {%- elif not c.project.is_nbhd_project -%}
                    {{ c.project.name }}
                {%- endif -%}
            </a>
        </h1>
    {% if files_appconfig and c.project.app_instance(files_appconfig).has_linked_download() %}
        <a href="{{ files_appconfig.url() }}download_file" >
          <button class="btn-home btn-danger" id="download_button" >Download</button>
        </a> 
    {% endif %}
    {% set status = c.project.troves_by_type('developmentstatus')|sort(attribute='fullname') %}
    {% set status = status[-1] %}
    {% if status and status.shortname not in ['production', 'mature'] %}
    <span id="dev-status" class="{{ status.shortname }}">{{ status.shortname }}</span>
    {% endif %}
    <h2 class="project_summary{% if c.project.icon %} with-icon{% endif %}">
        {{c.project.summary}}
    </h2>
    {% if not c.project.is_user_project %}
    <div class="brought-by{% if c.project.icon %} with-icon{% endif %}">
        Brought to you by:
        {% set admins = c.project.admins()|sort(attribute='username') %}
        {% for admin in admins[:5] %}
            {% if loop.last and admins|length > 5 -%}
                and <a href="{{ c.project.url() }}_members/">{{ admins|length - 4 }} others</a>
            {% else %}
                <a href="{{ admin.url() }}">{{ admin.username }}</a>{{ ',' if not loop.last }}
            {% endif %}
        {%- endfor -%}
      <p id="detailed_ratings"><span id=stars></span></p>
    </div>
    {% endif %}
{% endif %}

{% if c.project.rating %}{# hide if ratings haven't been used (project/site may not use ForgeFeedback) #}
<script>
 document.getElementById("stars").innerHTML = getStars("{{c.project.rating}}");

function getStars(ratings) {

  // Round to nearest half
  ratings = Math.round(ratings * 2) / 2;
  let output = [];

  // Append all the filled whole stars
  var i;
  for (i = ratings; i >= 1; i--) {
    output.push('<i class="fa fa-star" aria-hidden="true" style="color: orange;"></i>&nbsp;');
  }

  // If there is a half a star, append it
  if (i === .5) {
    output.push('<i class="fa fa-star-half-o" aria-hidden="true" style="color: orange;"></i>&nbsp;');
  }

  // Fill the empty stars
  for (let i = (5 - ratings); i >= 1; i--) {
    output.push('<i class="fa fa-star-o" aria-hidden="true" style="color: orange;"></i>&nbsp;');
  }
  return output.join('');

 }
 </script>
{% endif %}
