module Vulgata
  module ApplicationHelper
    def context_path_for(object)
      # use the provided context path helper to link to appropriate context
      if object.vulgata_context_path && object.vulgata_context_path_param
        main_app.send(object.vulgata_context_path, object.send(object.vulgata_context_path_param))
      else # if not provided, fallback to current object's path
        begin
          main_app.url_for(object)
        rescue StandardError
          nil
        end
      end
    end

    def status_tabs(path, _klass = nil, items_count = nil)
      status_counts = items_count || Vulgata::TranslationState.group(:status).count
      all_count = status_counts.except(Vulgata::TranslationState.statuses[:source], nil).values.sum

      content_tag :ul, class: 'nav nav-tabs' do
        # 'All' tab
        concat(content_tag(:li, class: status_tab_class(:all)) do
          link_to(send(path, current_query_params.except(:status))) do
            concat("All (#{all_count})")
          end
        end)
        # all other tabs
        Vulgata::TranslationState.statuses.except(:source, :rejected).each do |status, status_value|
          opts = current_query_params
          opts[:status] = status.to_s
          concat(content_tag(:li, class: status_tab_class(status)) do
            link_to(send(path, opts)) do
              concat(status.to_s.humanize)
              concat(" (#{status_counts[status_value] || 0})")
            end
          end)
        end
      end
    end

    def sorting_dropdown(path)
      opts = current_query_params

      updated_class, updated_sort_param = case params[:sort]
                                          when 'updated_at_desc' then ['active sorted-desc', 'updated_at_asc']
                                          when 'updated_at_asc' then ['active sorted-asc', 'updated_at_desc']
                                          else [nil, 'updated_at_desc']
      end

      priority_class, priority_sort_param = case params[:sort]
                                            when 'priority_desc' then ['active sorted-desc', 'priority_asc']
                                            when 'priority_asc' then ['active sorted-asc', 'priority_desc']
                                            else [nil, 'priority_desc']
      end

      content_tag(:ul, class: 'nav navbar-nav navbar-right') do
        concat(content_tag(:li, class: (params[:sort].blank? ? 'active' : nil)) do
          content_tag(:p, class: 'navbar-text') do
            'Sort by:'
          end
        end)
        concat(content_tag(:li, class: priority_class) do
          link_to('Priority', send(path, opts.merge(sort: priority_sort_param)))
        end)
        concat(content_tag(:li, class: updated_class) do
          link_to('Date', send(path, opts.merge(sort: updated_sort_param)))
        end)
      end
    end

    def current_query_params
      opts = {}
      opts[:resource] = params[:resource] if params[:resource].present?
      opts[:status] = params[:status] if params[:status].present?
      opts[:q] = params[:q] if params[:q].present?
      opts[:sort] = params[:sort] if params[:sort].present?
      opts
    end

    def status_tab_class(status)
      if params[:status] == status ||
         ((params[:status].blank? || params[:status] == 'all') && status == :all)
        'active'
      else
        ''
      end
    end

    def translation_status_label(status)
      label_class = nil

      label_class = case status
                    when :source
                      'label label-default'
                    when :pending
                      'label label-primary'
                    when :in_progress
                      'label label-warning'
                    when :done
                      'label label-info'
                    when :proofreading
                      'label label-warning'
                    when :rejected
                      'label label-danger'
                    when :approved
                      'label label-success'
                    else
                      'label label-danger'
                    end

      content_tag(:span, status.to_s.humanize, class: label_class)
    end

    def language_flag_and_label(locale)
      content_tag :span do
        concat flag_icon(Vulgata.configuration.locale_flags[locale.to_sym], class: 'flag-icon-vlg') if Vulgata.configuration.locale_flags[locale.to_sym]
        concat Vulgata::LanguageTools.language_name(locale)
      end
    end

    def language_flag_or_label(locale)
      flag_code = Vulgata.configuration.locale_flags[locale.to_sym]
      flag_code.present? ? flag_icon(flag_code, class: 'flag-icon-vlg') : locale
    end

    def translator_name(translation_state)
      translation_state.user.present? ? translation_state.user.name : nil
    end

    def bootstrap_class_for(flash_type)
      { success: 'alert-success', error: 'alert-danger', alert: 'alert-warning', notice: 'alert-info' }[flash_type.to_sym] || flash_type.to_s
    end

    def flash_messages(_opts = {})
      flash.each do |msg_type, message|
        concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do
          concat content_tag(:button, 'x', class: 'close', data: { dismiss: 'alert' })
          concat message
        end)
      end
      nil
    end
  end
end
