# frozen_string_literal: true

require 'kramdown'

module ApplicationHelper
  def suppress_main_layout_flash?
    true if flash && ['User successfully added', 'User successfully removed'].include?(flash.notice)
  end

  def to_markdown(text)
    return nil if text.blank?

    raw(sanitize(Kramdown::Document.new(text).to_html))
  end

  def organization_dropdown_options
    Organization.all.order(:name).map { |org| ["#{org.abbreviation} - #{org.name}", org.id] }
  end

  def organization_abbreviation_dropdown_options
    Organization.all.order(:name).map { |org| ["#{org.abbreviation} - #{org.name}", org.abbreviation] }
  end

  def us_timezones
    [
      ActiveSupport::TimeZone['Eastern Time (US & Canada)'],
      ActiveSupport::TimeZone['Central Time (US & Canada)'],
      ActiveSupport::TimeZone['Mountain Time (US & Canada)'],
      ActiveSupport::TimeZone['Pacific Time (US & Canada)'],
      ActiveSupport::TimeZone['Hawaii'],
    ]
  end

  def hisp_questions_key
    {
      '1' => 'satisfaction',
      '2' => 'trust',
      '3' => 'effectiveness',
      '4' => 'ease',
      '5' => 'efficiency',
      '6' => 'transparency',
      '7' => 'employee',
    }
  end

  def collection_rating_label(rating:)
    case rating
    when 'TRUE'
      '🟢'
    when 'FALSE'
      '🔴'
    when 'PARTIAL'
      '🟡'
    end
  end

  # key = a Collection.rating
  # value = sort order
  def collection_rating_sort_values
    {
      'FALSE' => '1',
      'PARTIAL' => '2',
      'TRUE' => '3',
    }
  end

  # key = a Service.aasm_state
  # value = sort order
  def service_status_sort_values
    {
      'created' => '1',
      'submitted' => '2',
      'approved' => '3',
      'verified' => '4',
      'archived' => '5',
    }
  end

  # key = a Website.production_status
  # value = sort order
  def website_status_sort_values
    {
      'in_development' => '1',
      'production' => '2',
      'being_decommissioned' => '3',
      'redirect' => '4',
      'archived' => '5',
      'decommissioned' => '6',
    }
  end

  def website_status_label_tags(status)
    {
      'in_development' => 'bg-blue',
      'production' => 'bg-mint',
      'being_decommissioned' => 'bg-accent-warm-dark',
      'redirect' => 'bg-accent-warm-light',
      'archived' => 'bg-gray-30',
      'decommissioned' => 'bg-black',
    }[status]
  end

  def cx_collections_filters_applied?(quarter, year, status)
    [quarter, year, status].any? { |param| param.present? && param.downcase != 'all' }
  end

  def cx_collections_filter_message(quarter, year, status)
    parts = []

    parts << "Q#{quarter}" if quarter.present? && quarter.downcase != 'all'

    parts << "FY#{year}" if year.present? && year.downcase != 'all'

    parts << status if status.present? && status.downcase != 'all'

    return '' if parts.empty?

    'for ' + parts.join(' ')
  end

  def form_edit_component_path(question_type)
    case question_type
    when 'radio_buttons', 'combobox'
      'components/forms/edit/question_types/radio_button_option'
    when 'dropdown'
      'components/forms/edit/question_types/dropdown_option'
    when 'checkbox'
      'components/forms/edit/question_types/checkbox_option'
    end
  end

  # Returns javascript to capture form input for one Form Question
  def question_type_javascript_params(question)
    if question.question_type == 'text_field'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'hidden_field'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'date_select'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'text_email_field'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'text_phone_field'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").dataset.rawValue"
    elsif question.question_type == 'textarea'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'rich_textarea'
      "form.querySelector(\"#hidden-#{question.ui_selector}\") && form.querySelector(\"#hidden-#{question.ui_selector}\").value" # Quill stores rich input text in hidden fields
    elsif question.question_type == 'radio_buttons'
      "form.querySelector(\"input[name=#{question.ui_selector}]:checked\") && form.querySelector(\"input[name=#{question.ui_selector}]:checked\").value"
    elsif question.question_type == 'star_radio_buttons'
      "form.querySelector(\"input[name=#{question.ui_selector}]:checked\") && form.querySelector(\"input[name=#{question.ui_selector}]:checked\").value"
    elsif question.question_type == 'big_thumbs_up_down_buttons'
      "form.querySelector(\"input[name=#{question.ui_selector}]:checked\") && form.querySelector(\"input[name=#{question.ui_selector}]:checked\").value"
    elsif question.question_type == 'yes_no_buttons'
      "form.querySelector(\"input[name=#{question.ui_selector}]\") && form.querySelector(\"input[name=#{question.ui_selector}]\").value"
    elsif question.question_type == 'checkbox'
      "form.querySelector(\"input[name=#{question.ui_selector}]:checked\") && Array.apply(null,form.querySelectorAll(\"input[name=#{question.ui_selector}]:checked\")).map(function(x) {return x.value;}).join(',')"
    elsif question.question_type == 'combobox'
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif %w[dropdown states_dropdown].include?(question.question_type)
      "form.querySelector(\"##{question.ui_selector}\") && form.querySelector(\"##{question.ui_selector}\").value"
    elsif question.question_type == 'text_display'
      'null'
    elsif question.question_type == 'custom_text_display'
      "form.querySelector(\"input[name=#{question.ui_selector}]:checked\") && form.querySelector(\"input[name=#{question.ui_selector}]:checked\").value"
    end
  end

  def is_at_least_form_manager?(user:, form:)
    user.admin? ||
      (user.organizational_form_approver && user.organization_id == form.organization_id) ||
      form.user_role?(user:) == UserRole::Role::FormManager
  end

  def current_path
    request.fullpath
  end

  def answer_fields
    %w[
      answer_01
      answer_02
      answer_03
      answer_04
      answer_05
      answer_06
      answer_07
      answer_08
      answer_09
      answer_10
      answer_11
      answer_12
      answer_13
      answer_14
      answer_15
      answer_16
      answer_17
      answer_18
      answer_19
      answer_20
      answer_21
      answer_22
      answer_23
      answer_24
      answer_25
      answer_26
      answer_27
      answer_28
      answer_29
      answer_30
    ]
  end

  # Legacy route from before the Form model was merged with the Touchpoint model
  def submit_touchpoint_uuid_url(form)
    "#{root_url}touchpoints/#{form.short_uuid}/submit"
  end

  def format_time(time, timezone)
    I18n.l time.to_time.in_time_zone(timezone), format: :with_timezone
  end

  def format_submission_time(datetime, time_zone)
    created_at = datetime.in_time_zone(time_zone)
    today = Date.today.in_time_zone(time_zone).beginning_of_day
    start_of_year = today.beginning_of_year

    if created_at >= today
      created_at.strftime('%-I:%M %p') # Today: 1:23 PM
    elsif created_at >= start_of_year
      created_at.strftime('%b %e') # Current year: Jan 5
    else
      created_at.strftime('%m/%d/%Y') # Last year: 01/05/2024
    end
  end

  def timezone_abbreviation(timezone)
    zone = ActiveSupport::TimeZone.new(timezone)
    zone.now.strftime('%Z')
  end

  def form_integrity_checksum(form:)
    data_to_encode = render(partial: 'components/widget/fba', formats: :js, locals: { form: })
    Digest::SHA256.base64digest(data_to_encode)
  end

  delegate :fiscal_year_and_quarter, to: :FiscalYear
end
