module OrbitBackendHelper

#========================================================================================================================
# from bulletin_helper.rb
#========================================================================================================================  

  def show_reject_reason(object)
    by_object = object.is_rejected
    by_user = (((object.create_user_id == current_user.id) rescue nil) or is_manager? or is_admin?)
    by_object && by_user
  end

  def show_form_status_field(object)
    #by_object = (!object.is_expired? and object.is_pending?)
    by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?)
    by_user
  end

  def show_approval_link(object)
    by_object = (!object.is_expired? and object.is_pending?)
    by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?)
    by_object and  by_user
  end

  def show_delete_link(object)
    if !current_user.nil?
      if object.class.fields.include?('create_user_id')
        by_object = (object.create_user_id == current_user.id ) 
        by_user = (is_manager? or is_admin?)
        by_object or by_user  
      else
        true
      end
    else
      false
    end
  end

  # def show_bulletin_title_at_index (bulletin)
  #   if bulletin.is_checked?
  #     link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
  #   else
  #     bulletin.title
  #   end  
  # end

#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================

  def is_filter_active?(type, id)
    ' active' if (@filter[type].include?(id.to_s) rescue nil)
  end

  def is_sort_active?(field)
    res = t(field[:translation])
    if params[:sort] && params[:sort].eql?(field[:sort])
      res << " " + content_tag(:b, nil, class: (params[:direction].eql?('asc') ? "icons-arrow-up-3" : "icons-arrow-down-4 "))
    end
    res.html_safe
  end

  def show_toggle_archive_btn(object)
    object.disable ? t(:disable) : t(:enable) 
  end

  def sortable_options(column)
    direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
    {:sort => column, :direction => direction}
  end


  # ===============================================================
  # associated:
  #   
  # TODO: link for other types
  # ===============================================================
  def get_value(object, field, authorization, approvable)
    res = ''
    case field[:type]
    when 'associated'
      sorted_objects = get_sorted_objects(object.send(field[:db_field]), field[:model_field]) rescue ''
    when 'id'
      sorted_objects = get_sorted_objects(field[:model].find(object.send(field[:db_field])), field[:model_field]) rescue ''
    when 'status'
      status = []
      if object.is_top?
        status << content_tag(:span, t(:top), class: "label label-success")
      end
      if object.is_hot?
        status << content_tag(:span, t(:hot), class: "label label-important")
      end
      if object.is_hidden?
        status << content_tag(:span, t(:hidden), class: "label")
      end
      if @approvable
        if object.is_pending?
          status << content_tag(:span, t(:pending), class: "label")
        end
        if object.is_checked?
          status << content_tag(:span, t(:passed), class: "label")
        end
        if object.is_rejected?
          status << content_tag(:span, t(:rejected), class: "label")
        end
      end
      res << status.join(' ')
    when 'tags'
      sorted_objects = get_sorted_objects(object.tags, 'name') rescue ''
    when 'standalone'
      res << (field[:need_value] ? eval(field[:display_option]) : field[:display_option])
    when 'image'
      image = (field[:version] ? object.send(field[:db_field]).send(field[:version]) : object.send(field[:db_field]))
      res << image_tag(image)
    else
      sorted_values = object.send(field[:db_field])
      sorted_values = sorted_values.sort if sorted_values.is_a?(Array)
    end
    res << display_value_from_object(object, sorted_values, field[:display_option]).to_s if sorted_values
    res << display_value_from_objects(sorted_objects, field[:model_field], field[:display_option]).to_s if sorted_objects
    if field[:quick_edit]
      res << (content_tag :div, class: "quick-edit" do
                content_tag :ul, class: "nav nav-pills" do
                  @quick_edit.each do |quick|
                    concat get_quick_link(quick, object, authorization, approvable)
                  end
                end
              end)
    end
    res.html_safe
  end

  def index_table
    content_tag :table, class: "table main-list" do
      concat (content_tag :thead do
        (content_tag :tr, class: "sort-header" do
          @fields.each do |field|
            concat (content_tag :th, "data-hide" => field[:hide], class: "#{field[:header_class]} #{'active' if (params[:sort] && params[:sort].eql?(field[:sort]))}" do
              if field[:sort]
                link_to is_sort_active?(field), url_for({:filter => @filter}.merge(sortable_options(field[:sort]).merge(:sort_options => field[:db_field])))
              else
                t(field[:translation])
              end
            end)
          end
        end)
      end)
      concat (content_tag :tbody do
        (@objects.each do |object|
          authorization = !@authorization || (@authorization && is_authorized(object))
          approvable = !@approvable || (@approvable && is_approvable(object))
          concat (content_tag :tr do
            (@fields.each do |field|
              concat(content_tag :td, get_value(object, field, authorization, approvable))
            end)
          end)
        end)
      end)
    end
  end

  def set_default_index(&block)
    @approvable = @module_app.is_approvable
    @authorization = @module_app.is_authorizable
    @fields = []
    @filter_fields =[]
    @filterable = false
    @index_footer = nil
    @quick_edit = []
    block.call
  end

  protected

  def display_value(value)
    case value.class.name
    when 'DateTime'
      display_date_time(value)
    when 'Date'
      display_date(value)
    when 'Time'
      display_time(value)
    else
      value
    end
  end

  def display_value_from_object(object, value, display_option)
    if display_option
      eval(display_option)
    else
      display_value(value)
    end
  end

  def display_value_from_objects(objects, field, display_option)
    if display_option
      objects = Array(objects).map do |object|
        eval(display_option)
      end
      objects.join(' ')
    else
      objects = Array(objects).map do |object|
        display_value(object.send(field)) rescue ''
      end
      objects.join(', ')
    end
  end

  # ===============================================================
  # type:
  #   check "get_value"
  # associated_value:
  #   check "get_value"
  # db_field:
  #   name of the field(s) in the database
  # translation:
  #   key in the yml translation files
  # hide:
  #   - no value: always shown
  #   - all: alway hidden
  #   - phone: hidden for phone
  # sort:
  #   unique value used for keeping the sorted column
  # link:
  #   if provided, the values will be shown as link
  #   (only available for type 'field' for now)
  # ===============================================================
  def field(args={})
    if !args.blank? && args[:translation] && args[:db_field]
      case args[:type]
      when 'associated'
        @fields << args if args[:model_field] || args[:display_option]
      when 'id'
        @fields << args if args[:model] && (args[:model_field] || args[:display_option])
      when 'image'
        args[:header_class] ||= 'span2'
        @fields << args
      else
        @fields << args
      end
    elsif !args.blank? && args[:type].eql?('tags')
      args[:translation] ||= 'tags'
      args[:display_option] ||= "content_tag(:span, object.name, class: 'label label-warning')"
      @fields << args
    elsif !args.blank? && args[:type].eql?('standalone')
      @fields << args
    end
  end

  def filter_field(args={})
    if !args.blank? && args[:type] && args[:values] && args[:translation]
      case args[:type]
      when 'array'
        @filter_fields << args
      when 'objects'
        @filter_fields << args if args[:object_field]
      end
      @filterable ||= true
    end
  end

  # ===============================================================
  # args:
  #   - paginate: default true, there'es no need to specify it
  #   - link: path to the new action
  # TODO: links to other actions
  # ===============================================================
  def footer(args={})
    paginate = args.has_key?(:paginate) ? args[:paginate] : true
    link = (is_manager? || is_sub_manager? rescue nil) && args.has_key?(:link) ? true : false
    if paginate || link
      @index_footer = content_tag :div, class: "bottomnav clearfix" do
        concat content_tag :div, link_to(content_tag(:i, nil, :class => 'icon-plus') + ' ' + t(:add), args[:link].blank? ? '#' : send(args[:link]), :class => (args[:link_class] || 'btn btn-primary'), data: args[:link_data]), class: "action pull-right" if link
        concat content_tag :div, paginate(@objects, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]}), class: "pagination pagination-centered" if paginate
      end
    end
  end

  def get_quick_link(quick, object, authorization, approvable)
    case quick[:type]
    when 'approval'
      if show_approval_link(object)
        content_tag :li, link_to(t(quick[:translation] || :approval_), eval("#{quick[:link]}('#{object.id}')"), class: "preview_trigger #{quick[:class]}")
      end
    when 'authorization'
      if is_admin? || is_manager?
        content_tag :li, link_to(t(quick[:translation] || :authorization_), eval("#{quick[:link]}"), class: "preview_trigger #{quick[:class]}")
      end
    when 'edit'
      if authorization && approvable
        content_tag :li, link_to(t(quick[:translation] || :edit), quick[:link].nil? ? '#' : eval("#{quick[:link]}('#{object.id}')"), class: quick[:class], data: eval("#{quick[:data]}"))
      end
    when 'delete'
      if show_delete_link(object)
        @delete_options = {title: quick[:title], warning: quick[:warning], cancel: quick[:cancel], submit: quick[:submit]}
        content_tag :li, link_to(t(quick[:translation] || :delete_), '#', rel: eval("#{quick[:link]}('#{object.id}')"), class: "delete #{quick[:class] || 'text-error'}")
      end
    when 'detail'
      content_tag :li, link_to(t(quick[:translation] || :detail), '#', class: (quick[:class] || "detail-row"))
    when 'reject_reason'
      if show_reject_reason(object)
        content_tag :li, link_to(t(quick[:translation] || :rejected_reason) + ' ' + truncate(object.not_checked_reason, :length => 10), '#', rel: "tooltip", 'data-original-title' => (wrap_string_with(object.not_checked_reason, :line_width => 24)), class: "reject_info #{quick[:class]}")
      end
    else
      content_tag :li, link_to(t(quick[:translation]), eval("#{quick[:link]}('#{object.id}')"), class: quick[:class])
    end
  end

  def get_sorted_objects(objects, field)
    if field
      objects_array = Array(objects).inject([]){ |result, value|
        result << [value.send(field), value]
      }
      objects_array.sort.map{|d| d[1]}
    else
      objects
    end
  end

  def is_approvable(object)
    current_or_guest_user.admin? || (!object.is_rejected? && !object.is_checked?)
  end

  def is_authorized(object)
    autorized = @module_app.authorizable_models.inject(false) do |autorized, klass|
      if object.is_a?(klass.constantize)
        autorized ||= object.user_can_sub_manage?(current_user)
      else
        autorized ||= object.send(klass.underscore).user_can_sub_manage?(current_user)
      end
      autorized
    end
    at_least_module_manager || autorized
  end

  def quick_edit_link(args)
    @quick_edit << args
  end

  def objects(objects)
    @objects = objects
  end

  def with_link?(field, object, value)
    if field[:link] && @approvable
      if object.class.instance_methods.include?(:is_checked?) && object.is_checked?
        link_to(value, eval("#{field[:link]}('#{object.id}')"))
        # link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
      else
        value
      end  
    elsif field[:link]
      link_to(value, eval("#{field[:link]}('#{object.id}')"))
    else
      value
    end
  end

  # ===============================================================  
  # Breadcrumbs
  # ===============================================================

  def back_end_breadcrumb
    res = ''
    divider = "<span class='divider'>/</span>"
    res << "<li><a href='#{admin_dashboards_path}'>#{t(:dashboard_)}</a>#{divider}</li>"
    case controller.controller_name
      when 'authorizations'
        res << "<li><a href='/#{params[:controller]}/#{@module_app.key}'>#{@module_app.title}</a>#{divider}</li>"
        case params[:type]
        when 'approval'
          res << "<li class='active'>#{t(:approval_)}</li>"
        when 'category'
          res << "<li class='active'>#{t(:category_auth)}</li>"
        when nil
          res << "<li class='active'>#{t(:module_authorization)}</li>"
        else
          res << "<li class='active'>#{params[:type].underscore.humanize.capitalize} #{t(:authorization_)}</li>"
        end
      when 'categories'
        res << "<li>#{@module_app.title}#{divider}</li>"
        res << "<li class='active'>#{t(:categories)}</li>"
      when 'module_tags'
        res << "<li>#{@module_app.title}#{divider}</li>"
        res << "<li class='active'>#{t(:tags)}</li>"
      else
        if params[:action].eql?('index')
          res << "<li>#{@module_app.title}</li>"
        else
          res << "<li><a href='/#{params[:controller]}'>#{@module_app.title}</a>#{divider}</li>"
          res << "<li class='active'>#{t(params[:action], scope: 'restful_actions')}</li>"
        end
    end
    res.html_safe
  end

end