# frozen_string_literal: true

require_relative 'test_helper'
require 'review/htmlbuilder'
require 'review/img_math'
require_relative 'image_comparator'

class ImgMathTest < Test::Unit::TestCase
  def setup
    @config = ReVIEW::Configure.values
    @tmpdir = Dir.mktmpdir
    @config.merge!(
      'math_format' => 'imgmath',
      'texcommand' => 'uplatex',
      'imagedir' => @tmpdir
    )
    @config['imgmath_options']['fontsize'] = 24
    @img_math = ReVIEW::ImgMath.new(@config)
  end

  def teardown
    @img_math.cleanup_mathimg
    FileUtils.rm_rf(@tmpdir)
  end

  def test_defer_math_image_pathname
    str1 = '$A > B \\gg C$'
    key1 = Digest::SHA256.hexdigest(str1)
    img_path1 = @img_math.defer_math_image(str1, key1)

    assert_equal "_gen_#{key1}.png", File.basename(img_path1)
  end

  def test_defer_math_image
    unless support_latex_in_tests? && support_pdftocairo_in_tests?
      $stderr.puts 'skip test_defer_math_image'
      return true
    end

    str1 = '$\\sum_{i=1}^nf_n(x) \\in \\mathbb{R}$'
    key1 = Digest::SHA256.hexdigest(str1)
    img_path1 = @img_math.defer_math_image(str1, key1)
    str2 = '$\\sum_{i=1}^nf_n(X) \\in \\mathbb{R}$'
    key2 = Digest::SHA256.hexdigest(str2)
    img_path2 = @img_math.defer_math_image(str2, key2)
    @img_math.make_math_images

    assert File.exist?(img_path1)
    assert File.exist?(img_path2)

    val1 = compare_images(img_path1, File.join(assets_dir, 'img_math/img1.png'))
    assert val1 >= 0.9

    val2 = compare_images(img_path2, File.join(assets_dir, 'img_math/img2.png'))
    assert val2 >= 0.9

    val3 = compare_images(img_path1, img_path2)

    assert val3 < 0.9
  end

  def test_make_math_image_pathname
    unless support_latex_in_tests?
      $stderr.puts 'skip test_make_math_image_pathname'
      return true
    end

    str1 = '$A > B \\gg C$'
    key1 = Digest::SHA256.hexdigest(str1)
    img_path1 = @img_math.make_math_image(str1, key1)

    assert_equal "_gen_#{key1}.png", File.basename(img_path1)
  end

  def test_make_math_image
    unless support_latex_in_tests?
      $stderr.puts 'skip test_make_math_image'
      return true
    end
    str1 = '$A > B \\gg C$'
    key1 = Digest::SHA256.hexdigest(str1)
    img_path1 = @img_math.make_math_image(str1, key1)

    assert File.exist?(img_path1)

    val1 = compare_images(img_path1, File.join(assets_dir, 'img_math/img3.png'))
    assert val1 >= 0.9
  end

  private

  def compare_images(image1, image2)
    comparator = ImageComparator.new
    comparator.compare(image1, image2)
  end

  def support_latex_in_tests?
    begin
      `uplatex -v`
      true
    rescue StandardError
      false
    end
  end

  def support_pdftocairo_in_tests?
    begin
      `pdftocairo -v`
      true
    rescue StandardError
      false
    end
  end
end
