# frozen_string_literal: true

module ProjectVulnerabilityReportsHelper
  def pointer(pvr, pvs: false)
    if pvs
      score = pvr.vulnerability_score.to_f
      percentage = ((score / max_score) * 100)
    else
      percentage = pvr.security_score.to_f
    end
    number_to_percentage(percentage, precision: 2)
  end

  def pointer_from_right(pvr, pvs: false)
    percentage = pointer(pvr, pvs: pvs).to_f
    percentage_off = [0, 100 - percentage].max
    number_to_percentage(percentage_off, precision: 2)
  end

  def pss_content(pvr)
    number_to_percentage(pvr.security_score.to_f, precision: 2, strip_insignificant_zeros: true)
  end

  def pvs_content(pvr)
    @max_rank ||= ProjectVulnerabilityReport.max_rank
    score = pvr.vulnerability_score.to_f.round(2)
    rank = pvr.current_rank.ordinalize
    percentage_number = ((score / max_score) * 100)
    percentage = number_to_percentage(percentage_number, precision: 5)
    I18n.t('projects.show.vulnerability_report.pvs_tooltip', score: score,
                                                             percentage: percentage,
                                                             rank: rank,
                                                             max_rank: @max_rank)
  end

  def max_score
    @max_score ||= ProjectVulnerabilityReport.max_score
  end
end
