# frozen_string_literal: true

class Vulnerability::VersionChart
  include ChartHelper

  SEVERITY_LEGENDS = { high: [I18n.t('projects.vulnerabilities.severity.high'), '#4586BE'],
                       medium: [I18n.t('projects.vulnerabilities.severity.medium'), '#64AAF1'],
                       low: [I18n.t('projects.vulnerabilities.severity.low'), '#9DC7F1'],
                       unknown_severity: [I18n.t('projects.vulnerabilities.unknown_severity'), '#95C8D8'] }.freeze

  def initialize(release_history)
    @release_history = release_history
    @default_options = VULNERABILITY_VERSION_CHART_DEFAULTS.deep_dup
  end

  def data
    set_xaxis
    series.deep_merge(chart_watermark)
  end

  private

  def set_xaxis
    @default_options[:xAxis][:categories] = @release_history.map(&:version)
  end

  def series
    SEVERITY_LEGENDS.keys.each_with_index do |level, index|
      legend = SEVERITY_LEGENDS[level]
      @default_options[:series][index] = { name: legend[0], color: legend[1],
                                           data: vulnerabilities_count_by_severity(level.to_s) }
    end
    @default_options
  end

  def vulnerabilities_count_by_severity(level)
    @release_history.map { |r| r.attributes[level] }
  end
end
