module Dunlop::StateBadgeHelper
  # Display the state badge html for a specific record.
  def state_badge(target, options = {})
    state_badge = Dunlop::StateBadge.new(self, target, options)
    return ''.html_safe unless state_badge.valid?
    state_badge.html
  end

  def workflow_instance_state_badges_for(workflow_instance_class)
    output = ""
    workflow_instance_class.scope_for(current_user, archived: session[:archived_mode]).sorted_state_count.each do |state, count|
      badge = state_badge(workflow_instance_class.new(state: state), state_append_text: " (#{count})", link_to: main_app.polymorphic_path(workflow_instance_class, q: {state_eq: state}))
      output << badge
    end
    output.html_safe
  end

  def workflow_step_state_badges_for(workflow_step_class)
    output = ""
    workflow_step_class.scope_for(current_user, archived: session[:archived_mode]).sorted_state_count.each do |state, count|
      badge = state_badge workflow_step_class.new(state: state), state_append_text: " (#{count})"
      output << badge
    end
    output.html_safe
  end

  def show_workflow_progress_of(record)
    workflow_steps = record.workflow_steps.select{|workflow_step| can? :read, workflow_step }
    workflow_steps.map{|workflow_step| state_badge(workflow_step) }.join.html_safe
  end
end
