module Hourglass
  module ApplicationHelper
    def hourglass_asset_paths(type, sources)
      options = sources.extract_options!
      if options[:plugin] == Hourglass::PLUGIN_NAME
        plugin = options.delete(:plugin)
        if Rails.env.production?
          sources.map! do |source|
            extname = compute_asset_extname source, options.merge(type: type)
            source = "#{source}#{extname}" if extname.present?
            source = File.join Hourglass::Assets.asset_directory_map[type], source
            "/plugin_assets/#{plugin}/#{Hourglass::Assets.manifest.assets[source] || source}"
          end
        else
          sources = sources.map do |source|
            "/plugin_assets/#{plugin}/#{source}"
          end
        end
      end
      sources.push options
    end

    def javascript_include_tag(*sources)
      super(*hourglass_asset_paths(:javascript, sources))
    end

    def stylesheet_link_tag(*sources)
      super(*hourglass_asset_paths(:stylesheet, sources))
    end

    def redmine_javascript_include_tag(*sources)
      self.method(:javascript_include_tag).super_method.call(*sources)
    end

    def redmine_stylesheet_link_tag(*sources)
      self.method(:stylesheet_link_tag).super_method.call(*sources)
    end

    def form_field(field, form, object, options = {})
      render partial: "hourglass_ui/forms/fields/#{field}", locals: {form: form, entry: object}.merge(options)
    end

    def issue_label_for(issue)
      "##{issue.id} #{issue.subject}" if issue
    end

    def projects_for_project_select(selected = nil)
      projects = User.current.projects.allowed_to_one_of(*(Hourglass::AccessControl.permissions_from_action(controller: 'hourglass/time_logs', action: 'book') + Hourglass::AccessControl.permissions_from_action(controller: 'hourglass/time_bookings', action: 'change')).flatten)
      project_tree_options_for_select projects, selected: selected do |project|
        {data: {
            round_default: Hourglass::SettingsStorage[:round_default, project: project],
            round_sums_only: Hourglass::SettingsStorage[:round_sums_only, project: project]
        }}
      end
    end

    def user_collection(project = nil)
      project.present? ? project.users : User.active
    end

    def localized_hours_in_units(hours)
      h, min = Hourglass::DateTimeCalculations.hours_in_units hours || 0
      "#{h}#{t('hourglass.ui.chart.hour_sign')} #{min}#{t('hourglass.ui.chart.minute_sign')}"
    end

    def in_user_time_zone(time)
      zone = User.current.time_zone
      if zone
        time.in_time_zone zone
      else
        time.utc? ? time.localtime : time
      end
    end

    def css_classes(*args)
      args.compact.join(' ')
    end

    def format_identifier_to_js(format)
      {
          '%b' => 'MMM',
          '%B' => 'MMMM',
          '%d' => 'DD',
          '%m' => 'MM',
          '%M' => 'mm',
          '%H' => 'HH',
          '%I' => 'hh',
          '%p' => 'A',
          '%P' => 'a',
          '%Y' => 'YYYY'
      }.inject(format) { |str, (k, v)| str.gsub(k, v) }
    end

    def class_icon(icon, label)
      content_tag(:span, '', class: "icon icon-#{icon}") + content_tag(:span, label, class: "icon-label")
    end
  end
end
