# frozen_string_literal: true

module UxToolkitMyWorkHelper
  def ux_toolkit_query_path(query)
    query.project ? project_issues_path(query.project, query_id: query.id) : issues_path(query_id: query.id)
  end

  def ux_toolkit_issue_due_date(issue)
    issue.due_date ? format_date(issue.due_date) : ''
  end

  def ux_toolkit_my_work_block_total(block)
    (@block_totals || {}).fetch(block, 0)
  end

  def ux_toolkit_my_work_issue_filter_path(block)
    today = RedmineUxToolkit::Compatibility.user_today
    filters = {
      'status_id' => {operator: 'o', values: []},
      'assigned_to_id' => {operator: '=', values: ['me']}
    }

    case block
    when UxToolkitUserPreference::DASHBOARD_BLOCK_DUE_TODAY
      filters['due_date'] = {operator: '=', values: [today.to_s]}
    when UxToolkitUserPreference::DASHBOARD_BLOCK_OVERDUE
      filters['due_date'] = {operator: '<', values: [today.to_s]}
    when UxToolkitUserPreference::DASHBOARD_BLOCK_DUE_SOON
      filters['due_date'] = {operator: '><', values: [(today + 1).to_s, (today + 7).to_s]}
    end

    issues_path(
      set_filter: 1,
      f: filters.keys,
      op: filters.transform_values { |filter| filter[:operator] },
      v: filters.transform_values { |filter| filter[:values] }
    )
  end

  def ux_toolkit_my_work_view_all_path(block)
    case block
    when UxToolkitUserPreference::DASHBOARD_BLOCK_ASSIGNED_TO_ME,
         UxToolkitUserPreference::DASHBOARD_BLOCK_DUE_TODAY,
         UxToolkitUserPreference::DASHBOARD_BLOCK_OVERDUE,
         UxToolkitUserPreference::DASHBOARD_BLOCK_DUE_SOON
      ux_toolkit_my_work_issue_filter_path(block)
    when UxToolkitUserPreference::DASHBOARD_BLOCK_FAVORITE_PROJECTS
      ux_toolkit_my_favorite_projects_path
    when UxToolkitUserPreference::DASHBOARD_BLOCK_FAVORITE_QUERIES
      ux_toolkit_my_favorite_queries_path
    when UxToolkitUserPreference::DASHBOARD_BLOCK_RECENT_ISSUES
      ux_toolkit_my_recent_issues_path
    when UxToolkitUserPreference::DASHBOARD_BLOCK_RECENT_PROJECTS
      ux_toolkit_my_recent_projects_path
    end
  end
end
