# frozen_string_literal: true

require 'tc_helper'

class TestPageSetup < Minitest::Test
  def setup
    @p = Axlsx::Package.new
    ws = @p.workbook.add_worksheet name: "hmmm"
    @ps = ws.page_setup
  end

  def test_initialize
    assert_nil(@ps.fit_to_height)
    assert_nil(@ps.fit_to_width)
    assert_nil(@ps.orientation)
    assert_nil(@ps.paper_height)
    assert_nil(@ps.paper_width)
    assert_nil(@ps.scale)
  end

  def test_initialize_with_options
    page_setup = { fit_to_height: 1,
                   fit_to_width: 2,
                   orientation: :landscape,
                   paper_height: "297mm",
                   paper_width: "210mm",
                   scale: 50 }

    optioned = @p.workbook.add_worksheet(name: 'optioned', page_setup: page_setup).page_setup

    assert_equal(1, optioned.fit_to_height)
    assert_equal(2, optioned.fit_to_width)
    assert_equal(:landscape, optioned.orientation)
    assert_equal("297mm", optioned.paper_height)
    assert_equal("210mm", optioned.paper_width)
    assert_equal(50, optioned.scale)
  end

  def test_set_all_values
    @ps.set(fit_to_height: 1, fit_to_width: 2, orientation: :landscape, paper_height: "297mm", paper_width: "210mm", scale: 50)

    assert_equal(1, @ps.fit_to_height)
    assert_equal(2, @ps.fit_to_width)
    assert_equal(:landscape, @ps.orientation)
    assert_equal("297mm", @ps.paper_height)
    assert_equal("210mm", @ps.paper_width)
    assert_equal(50, @ps.scale)
  end

  def test_paper_size
    assert_raises(ArgumentError) { @ps.paper_size = 119 }
    refute_raises { @ps.paper_size = 10 }
  end

  def test_set_some_values
    @ps.set(fit_to_width: 2, orientation: :portrait)

    assert_equal(2, @ps.fit_to_width)
    assert_equal(:portrait, @ps.orientation)
    assert_nil(@ps.fit_to_height)
    assert_nil(@ps.paper_height)
    assert_nil(@ps.paper_width)
    assert_nil(@ps.scale)
  end

  def test_default_fit_to_page?
    assert(@ps.fit_to_width.nil? && @ps.fit_to_height.nil?)
    refute_predicate(@ps, :fit_to_page?)
  end

  def test_with_height_fit_to_page?
    assert(@ps.fit_to_width.nil? && @ps.fit_to_height.nil?)
    @ps.set(fit_to_height: 1)

    assert_predicate(@ps, :fit_to_page?)
  end

  def test_with_width_fit_to_page?
    assert(@ps.fit_to_width.nil? && @ps.fit_to_height.nil?)
    @ps.set(fit_to_width: 1)

    assert_predicate(@ps, :fit_to_page?)
  end

  def test_to_xml_all_values
    @ps.set(fit_to_height: 1, fit_to_width: 2, orientation: :landscape, paper_height: "297mm", paper_width: "210mm", scale: 50)
    doc = Nokogiri::XML.parse(@ps.to_xml_string)

    assert_equal(1, doc.xpath(".//pageSetup[@fitToHeight='1'][@fitToWidth='2'][@orientation='landscape'][@paperHeight='297mm'][@paperWidth='210mm'][@scale='50']").size)
  end

  def test_to_xml_some_values
    @ps.set(orientation: :portrait)
    doc = Nokogiri::XML.parse(@ps.to_xml_string)

    assert_equal(1, doc.xpath(".//pageSetup[@orientation='portrait']").size)
    assert_equal(0, doc.xpath(".//pageSetup[@fitToHeight]").size)
    assert_equal(0, doc.xpath(".//pageSetup[@fitToWidth]").size)
    assert_equal(0, doc.xpath(".//pageSetup[@paperHeight]").size)
    assert_equal(0, doc.xpath(".//pageSetup[@paperWidth]").size)
    assert_equal(0, doc.xpath(".//pageSetup[@scale]").size)
  end

  def test_fit_to_height
    assert_raises(ArgumentError) { @ps.fit_to_height = 1.5 }
    refute_raises { @ps.fit_to_height = 2 }
    assert_equal(2, @ps.fit_to_height)
  end

  def test_fit_to_width
    assert_raises(ArgumentError) { @ps.fit_to_width = false }
    refute_raises { @ps.fit_to_width = 1 }
    assert_equal(1, @ps.fit_to_width)
  end

  def test_orientation
    assert_raises(ArgumentError) { @ps.orientation = "" }
    refute_raises { @ps.orientation = :default }
    assert_equal(:default, @ps.orientation)
  end

  def test_paper_height
    assert_raises(ArgumentError) { @ps.paper_height = 99 }
    refute_raises { @ps.paper_height = "11in" }
    assert_equal("11in", @ps.paper_height)
  end

  def test_paper_width
    assert_raises(ArgumentError) { @ps.paper_width = "22" }
    refute_raises { @ps.paper_width = "29.7cm" }
    assert_equal("29.7cm", @ps.paper_width)
  end

  def test_scale
    assert_raises(ArgumentError) { @ps.scale = 50.5 }
    refute_raises { @ps.scale = 99 }
    assert_equal(99, @ps.scale)
  end

  def test_fit_to
    fits = @ps.fit_to(width: 1)

    assert_equal([1, 999], fits)
    fits = @ps.fit_to height: 1

    assert_equal([999, 1], fits)
    fits = @ps.fit_to height: 7, width: 2

    assert_equal([2, 7], fits)
    assert_raises(ArgumentError) { puts @ps.fit_to(width: true) }
  end
end
