module SideBarRenderer
  include Renderer
  include AdminHelper
  include ActionView::Helpers::TextHelper

  def render(request,params,user,current_module_app)
    @belong_module_app = get_module_app
    @current_module_app = current_module_app
    @request = request
    @params = params
    @current_user = user
    if display? 
      content_tag :div, class: "sub-nav-block #{@icon_class}" do
        concat content_tag :h4, I18n.t(@head_label)
        concat (content_tag :ul, class: "nav nav-list" do
          @context_links.sort_by {| obj | obj.priority}.map{ |link|
            link.render(request, params, @current_module_app, @current_user, @belong_module_app)
          }.join.html_safe
        end)
      end
    end
  end

  def render_head(request, params, user, current_module_app)
    @belong_module_app = get_module_app
    @current_module_app = current_module_app
    @request = request
    @params = params
    @current_user = user
    if display?
      content_tag :li, class: (module_sidebar_active? ? 'active' : nil) do
        link_to eval(@head_link) do
          content_tag :span do
            content_tag(:i, nil, class: @icon_class)
          end
        end
      end 
    end
  end

  protected

  def display? #控制sidebar 要不要算圖
    if is_manager? || is_admin?  #如果是系統管理員 或 是模組管理員
      true
    elsif (@current_module_app.open rescue true) # 如果app 被設定成 開放
      true
     elsif is_member? #如果app 是封閉  那至少需要是 member
      true
    else 
      false
    end
  end

  def module_sidebar_active?
    active_for_controller? or active_for_app_auth? or active_for_ob_auths?or active_for_category? or active_for_tag?
  end

  def active_for_controller?
    @active_for_controllers.include? controller
  end

end