<%
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
%>

<div class="header py-4">
  <div class="container">
    <div class="d-flex">
      <%= link_to (current_user ? home_page : root_path), class: "header-brand" do %>
        <% begin %>
          <%= image_tag(logo_image, class: "header-brand-img", alt:"") %>
        <% rescue %>
          <%= t("administrator.site_settings.branding.invalid") %>
        <% end %>
      <% end %>

      <div class="d-flex ml-auto">
        <% if current_user %>

          <% if current_user.has_role? :super_admin %>
            <% admins_page = params[:controller] == "admins" && params[:action] == "index" ? "active" : "" %>
            <%= link_to admins_path, class: "px-3 mx-1 mt-1 header-nav #{admins_page}" do %>
              <i class="fas fa-home pr-1 "></i><span class="d-none d-sm-inline-block"><%= t("header.dropdown.home") %></span>
            <% end %>
          <% else %>
            <% rooms_page = params[:controller] == "rooms" && params[:action] == "show" ? "active" : "" %>
            <%= link_to current_user.main_room, class: "px-3 mx-1 mt-1 header-nav #{rooms_page}" do %>
              <i class="fas fa-home pr-1 "></i><span class="d-none d-sm-inline-block"><%= t("header.dropdown.home") %></span>
            <% end %>

            <% if current_user.highest_priority_role.get_permission("can_create_rooms") %>
              <% all_rec_page = params[:controller] == "users" && params[:action] == "recordings" ? "active" : "" %>
              <%= link_to get_user_recordings_path(current_user), class: "px-3 mx-1 mt-1 header-nav #{all_rec_page}" do %>
                <i class="fas fa-video pr-1"></i><span class="d-none d-sm-inline-block"><%= t("header.all_recordings") %></span>
              <% end %>
            <% end %>
          <% end %>

          <div class="dropdown">
            <a href="#" class="nav-link pr-0" data-toggle="dropdown">
              <% if current_user.image.blank? || !valid_url?(current_user.image) %>
                <span class="avatar"><%= current_user.name.first %></span>
              <% else %>
                <span id="user-avatar" class="avatar d-none"><%= current_user.name.first %></span>
                <%= image_tag(current_user.image, id: "user-image", class: "avatar") %>
              <% end %>
              <span class="ml-2 d-none d-lg-block">
                <span class="text-default username"><%= current_user.name %></span>
              </span>
            </a>
            <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow" x-placement="bottom-end">
              <%= link_to edit_user_path(current_user), class: "dropdown-item"  do %>
                <i class="dropdown-icon fas fa-id-card mr-3"></i><%= t("header.dropdown.settings") %>
              <% end %>
              <% highest_role = current_user.highest_priority_role %>
              <% if highest_role.get_permission("can_manage_users") || highest_role.name == "super_admin" %>
                <%= link_to admins_path, class: "dropdown-item" do %>
                  <i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
                <% end %>
              <% elsif highest_role.get_permission("can_manage_rooms_recordings")%>
                <%= link_to admin_rooms_path, class: "dropdown-item" do %>
                  <i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
                <% end %>
              <% elsif highest_role.get_permission("can_edit_site_settings") %>
                <%= link_to admin_site_settings_path, class: "dropdown-item" do %>
                  <i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
                <% end %>
              <% elsif highest_role.get_permission("can_edit_roles")%>
                <%= link_to admin_roles_path, class: "dropdown-item" do %>
                  <i class="dropdown-icon fas fa-user-tie mr-3"></i><%= t("header.dropdown.account_settings") %>
                <% end %>
              <% end %>
              <div class="dropdown-divider"></div>
              <% if Rails.configuration.help_url.present? %>
                <a class="dropdown-item" href="<%= Rails.configuration.help_url %>" target="_blank" rel="noopener">
                  <i class="dropdown-icon far fa-question-circle"></i> <%= t("header.dropdown.help") %>
                </a>
              <% end %>
              <% if Rails.configuration.report_issue_url.present? %>
                <a class="dropdown-item" href="<%= Rails.configuration.report_issue_url %>" target="_blank" rel="noopener">
                  <i class="dropdown-icon fas fa-exclamation mr-3"></i><%= t("errors.internal.report") %>
                </a>
              <% end %>
              <%= link_to logout_path, class: "dropdown-item" do %>
                <i class="dropdown-icon fas fa-sign-out-alt"></i> <%= t("header.dropdown.signout") %>
              <% end %>
            </div>
          </div>
        <% else %>
          <% allow_greenlight_accounts = allow_greenlight_accounts? %>
          <% if allow_greenlight_accounts %>
            <%= link_to t("login"), signin_path, :class => "btn btn-outline-primary mx-2 sign-in-button" %>
          <% elsif Rails.configuration.loadbalanced_configuration %>
            <%= link_to t("login"), omniauth_login_url(:bn_launcher), :class => "btn btn-outline-primary mx-2 sign-in-button" %>
          <% else %>
            <%= link_to t("login"), signin_path, :class => "btn btn-outline-primary mx-2 sign-in-button" %>
          <% end %>

          <% if allow_user_signup? && allow_greenlight_accounts %>
            <%= link_to t("signup.title"), signup_path, :class => "btn btn-outline-primary mx-2 sign-in-button" %>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>
</div>
