Given(/^I have (single product territory split|multiple product territory splits) to apply$/) do |splits|
  case splits
  when "single product territory split"
    @splits = [{ "upc" => "889845790624", "split_rate" => "8.00", "country" => "Ukraine" }]
  when "multiple product territory splits"
    @splits = [{ "upc" => "889845790624", "split_rate" => "8.00", "country" => "Ukraine" },
               { "upc" => "886788064409", "split_rate" => "9.00", "country" => "Poland" }]
  end
end

When(
  /^I am on the Contract::ProductByTerritoryArrangement for contract (.*) with (.*)$/
) do |int, split_presence|
  if @url_queries
    @url_queries[:id] = int
  else
    @url_queries = { "id" => int }
  end
  visit(Contract::ProductByTerritoryArrangement,
        using_params: { queries: @url_queries }) do |page|
    @browser.alert.ok if @browser.alert.exists?
    page.clickable_table_element.when_present
    case split_presence
    when "no splits"
      step "I should be able to delete all product territory splits"
    when "a split"
      step "I should be able to delete all product territory splits"
      step "I have single product territory split to apply"
      step "I should be able to add product territory splits"
    end
  end
end

Then(/^I should be able to delete all product territory splits$/) do
  on(Contract::ProductByTerritoryArrangement) do |page|
    Retriable.retriable(on: Watir::Wait::TimeoutError, tries: 3) do
      page.delete_all_territory_splits
    end
    page.no_products_message_element.when_present
    expect(page.no_products_message?).to be true
  end
end

Then(/^I should be able to add product territory splits$/) do
  # splits = JSON.parse(@splits)

  on(Contract::ProductByTerritoryArrangement) do |page|
    @splits.each do |split|
      page.add_territory_split(split["upc"], split["split_rate"], split["country"])
      expect(page.success_message?).to be true
      expect(page.success_message_element.text).to eq(
        "Product territory arrangement data saved Successfully"
      ).or(
        eq("Product territory arrangement data updated Successfully")
      )
      page.refresh
      expect(page.clickable_table_element[1].text).to include split["upc"]
      expect(page.split_element.value).to eq split["split_rate"]
    end
  end
end

Then("I should be able to update a product territory split") do
  split_value_for_update = "15.00"

  on(Contract::ProductByTerritoryArrangement) do |page|
    page.split_element.clear
    page.split_element.send_keys split_value_for_update
    page.submit
    expect(page.success_message_element.text).to eq(
      "Product territory arrangement data updated Successfully"
    )
    page.refresh
    expect(page.split_element.value).to eq split_value_for_update
  end
end

When("I add a territory split for same UPC and country twice") do
  step "I have single product territory split to apply"
  # 51643 is the contract_id of the vendor 7123
  step "I am on the Contract::ProductByTerritoryArrangement for contract 51643 with a split"
  on(Contract::ProductByTerritoryArrangement) do |page|
    page.add_territory_split(@splits[0]["upc"], @splits[0]["split_rate"], @splits[0]["country"])
  end
end

Then("a validation error should appear") do
  on(Contract::ProductByTerritoryArrangement) do |page|
    expect(page.alert_element.text).to include "Duplicate entry for upc and country id"
    page.refresh
    page.clickable_table_element.when_present
    expect(page.delete_btn_elements.count).to eq 1
  end
end

And("I add a split with no split rate and a country") do
  step "I have single product territory split to apply"
  on(Contract::ProductByTerritoryArrangement) do |page|
    page.search_input_element.send_keys @splits[0]["upc"]
    page.search_results_element.when_present.click
    page.submit
  end
end

Then("an validation error should appear") do
  on(Contract::ProductByTerritoryArrangement) do |page|
    expect(page.alert_element.text).to include "Please enter valid split values"
    expect(page.alert_element.text).to include "Please select territory"
  end
end
