# frozen_string_literal: true

# application controller
module ApplicationHelper
  # icon, name, controller,
  def nav_menu
    ret = ''
    ret += menu_item('far fa-circle', 'Dotacje', '/grants', 'grants') if role?('dotations')
    ret += menu_item('far fa-circle', 'Wydatki', '/expenses', 'expenses') if role?('expenses')
    ret += menu_item('far fa-circle', 'Wielkość firmy', '/company_sizes', 'company_sizes') if role?('company_sizes')
    ret += menu_item('far fa-circle', 'Tagi', '/tags', 'tags') if role?('tags')
    ret += menu_item('far fa-circle', 'Działalności', '/company_activities', 'company_activities') if role?('company_activities')
    ret += menu_item('far fa-circle', 'Przedsięwzięcia', '/projects', 'projects') if role?('projects')
    ret += menu_item('far fa-circle', 'Eksperci', '/experts', 'experts') if role?('experts')
    ret += menu_item('far fa-circle', 'Partnerzy', '/partners', 'partners') if role?('partners')
    ret += menu_item('far fa-user', 'Użytkownicy', '/settings/users', 'users') if admin?

    ret
  end

  def menu_item(icon, name, url, check_url)
    active_to_add = ''
    if check_url.class.to_s == 'String'
      active_to_add = controller_path.to_s.include?(check_url) ? ' active' : ''
    elsif check_url.class.to_s == 'Array'
      check_url.each do |fa|
        if controller_path.to_s.include?(fa)
          active_to_add = ' active'
          break
        end
      end
    end
    ret = '<li class="nav-item"><a href="' + url + '" class="nav-link' + active_to_add + '">'
    ret += '<i class="' + icon + ' nav-icon"></i>'
    ret += '<p>' + name + '</p></a></li>'
    ret
  end

  def admin?
    role = Role.where(name: 'admin').first
    current_user.roles.include?(role)
  end

  def role?(role_name)
    role = Role.where(name: role_name).first
    admin? || current_user.roles.include?(role)
  end

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end

end
