# frozen_string_literal: true

# Module for building side menu
module SidemenuHelper
  def build_side_menu
    ret = nav_link('home', '/', 'home', 'fa-home')
    ret += divider
    ret += heading('Admin')
    ret += nav_link('courses', '/admin/courses', 'admin/courses', 'fa-tv')
    submenu = ['users.user_management',
               ['users.accounts', '/admin/users', 'admin/users', 'fa-user-circle'],
               'users.finance',
               ['users.orders', '/home/index', 'home', 'fa-file-alt'],
               ['users.discount_codes', '/home/index', 'home', 'fa-search-dollar']]
    ret += nav_link_submenu(['users.menu_name', 'fa-users', 'Users'],
                            submenu,
                            ['admin/users', 'home'])
    ret.html_safe
  end

  # divider for menu
  def divider
    # <hr class="sidebar-divider">
    tag.hr class: 'sidebar-divider'
  end

  # heading for menu
  def heading(name)
    # <div class="sidebar-heading">Addons</div>
    content_tag(:div, name, class: 'sidebar-heading')
  end

  # link without submenu
  def nav_link(text, link, for_active, icon)
    nav_class = check_link_active(for_active) ? 'nav-item active' : 'nav-item'
    content_tag(:li, nav_link_to(text, icon, link), class: nav_class)
  end

  # link_to for navigation
  def nav_link_to(text, icon, link, name = nil, expand = nil)
    icon_text = icon.nil? ? '' : "<i class=\"fas fa-fw #{icon}\"></i>"
    if name.nil?
      link_to(raw("#{icon_text}<span>#{I18n.t(text)}</span>"),
              link, class: 'nav-link')
    else
      link_to(raw("#{icon_text}<span>#{I18n.t(text)}</span>"),
              link, class: "nav-link#{expand ? '' : ' collapsed'}",
                    data: { toggle: 'collapse', target: "#collapse#{name}" },
                    aria: { expanded: expand.to_s, controls: "collapse#{name}" })
    end
  end

  # create link with sub_menu
  # @params:
  # a_name [Array] - [name of menu, icon, div_id]
  # submenu [Array] - array of :
  #   -> [name of link {str}, link{str}, active{str or arr}]
  #   -> nil - to add divider to menu
  #   -> 'String' - to add header to menu
  # for_active - all links in this submenu
  def nav_link_submenu(a_name, submenu, for_active)
    content_tag :li, class: 'nav-item' do
      expand = check_link_active(for_active)
      concat nav_link_to(a_name[0], a_name[1], '#', a_name[2], expand)
      concat content_tag(:div, inner_submenu(submenu),
                       id: "collapse#{a_name[2]}",
                       class: "collapse#{expand ? ' show' : ''}",
                       aria: { labelledby: "heading#{a_name[2]}" },
                       data: { parent: '#accordionSidebar' })
    end
  end

  # creates inner_submenu
  # submenu [Array] - array of :
  #   -> [name of link {str}, link{str}, active{str or arr}]
  #   -> nil - to add divider to menu
  #   -> 'String' - to add header to menu
  def inner_submenu(submenu)
    content_tag :div, class: 'bg-white py-2 collapse-inner rounded' do
      submenu.each do |sub|
        concat submenu_element(sub)
      end
    end
  end

  def submenu_element(sub)
    if sub.nil?
      # divider
      tag.div class: 'collapse-divider'
    elsif sub.class.to_s == 'String'
      # header
      content_tag :h6, I18n.t(sub), class: 'collapse-header'
    elsif sub.class.to_s == 'Array'
      # link (menu item)
      activecl = check_link_active(sub[2]) ? ' active' : ''
      icon = sub[3].nil? ? '' : "<i class=\"fas fa-fw #{sub[3]}\"></i>"
      nm = I18n.t(sub[0])
      link_to(raw("#{icon} #{nm}"), sub[1], class: "collapse-item#{activecl}")
    end
  end

  # check if this link is active
  def check_link_active(for_active)
    is_active = false
    if for_active.class.to_s == 'String'
      is_active = controller_path.to_s.include?(for_active)
    elsif for_active.class.to_s == 'Array'
      for_active.each do |fa|
        is_active = true if controller_path.to_s.include?(fa)
        break if is_active
      end
    end
    is_active
  end
end
