# frozen_string_literal: true

module PdfDocuments
  class CustomerReport < Base
    private

    def generate
      logo
      @prawn_document.formatted_text_box [ {
        text: "Dieses Dokument wurd am #{Time.current.strftime('%d %B %Y')} um #{Time.current.strftime('%H:%M:%S')} erstellt.", size: 8, align: :right
      } ], align: :right

      heading1 params.report.name
      rich_text params.report.comment

      params.report.export[:elements].each.with_index(1) do |(element, success_criteria), element_index|
        heading2 "#{element_index} #{element.title}"
        move_down(5)
        bold("Pfad: #{element.page.path}")
        move_down(5)
        rich_text element.description
        safe_display(element.screenshot) { image(_1, height: 160) }

        success_criteria.each.with_index(1) do |success_criterion, sc_index|
          success_criterion_row(success_criterion, [element_index, sc_index])
        end
      end
    end

    private

    def success_criterion_row(success_criterion, index)
      heading3 "#{index.join(".")} #{success_criterion.title}"
      safe_display(success_criterion.test_comment) do
        heading4 "Kommentar"
        rich_text success_criterion.test_comment
      end
      safe_display(success_criterion.quick_criterion) do
        heading4 "Kriterium"
        rich_text success_criterion.quick_criterion
      end
      safe_display(success_criterion.quick_fail) do
        heading4 "Fail"
        rich_text success_criterion.quick_fail
      end
      safe_display(success_criterion.quick_fix) do
        heading4 "Fix"
        rich_text success_criterion.quick_fix
      end
    end
  end
end
