class IndexedPropertyPage
  include PageObject

  indexed_property :table, [[:text_field, :text_table, {:id => 'table[%s].text'}],
                            [:text_field, :text_name, {:name => 'tableName[%s].text'}],
                            [:radio_button, :radio, {:id => 'table[%s].radio'}],
                            [:checkbox, :check, {:id => 'table[%s].check'}],
                            [:text_area, :area, {:id => 'table[%s].area'}],
                            [:button, :button, {:id => 'table[%s].button'}],
                            [:div, :content, {:id => 'table[%s].content'}],
                            [:div, :nil?, {:id => 'table[%s].bad_name'}],
  ]

  indexed_property :index_table, [[:radio_button, :radio, {:index => '%d'}]]

  indexed_property :index_table_by_regex, [
      [:radio_button, :radio, {:id => /^table.*radio$/, :index => '%d'}]
  ]

  indexed_property :nottable, [[:text_field, :text_nottable, {:id => 'nottable[%s].text'}]]

  indexed_property :other_table, [
      [:text_field, :text_table, {:id => 'other_table[%s].text'}],
      [:div, :content, {:id => 'other_table[%s].content'}]
  ]

end

def page
  @page ||= IndexedPropertyPage.new(@browser)
end

Given /^I am on the indexed property page$/ do
#  @page = IndexedPropertyPage.new(@browser)
  page.navigate_to(UrlHelper.indexed)
end

When /^I search for the elements for index "([^\"]*)"$/ do |index|
  @index = index
end

Then /^I type "([^\"]*)" into the table's indexed text field by id$/ do |val|
  page.table[@index].text_table = val
end

Then /^The table's indexed text field by id should contain "([^\"]*)"$/ do |val|
  expect(page.table[@index].text_table).to eql val
end

Then /^I type "([^\"]*)" into the table's indexed text field by name$/ do |val|
  page.table[@index].text_name = val
end

Then /^The table's indexed text field by name should contain "([^\"]*)"$/ do |val|
  expect(page.table[@index].text_name).to eql val
end

Then /^I select the indexed radio button$/ do
  page.table[@index].select_radio
end

Then /^The indexed radio button should be selected$/ do
  expect(page.table[@index].radio_selected?).to be true
end

Then /^I check the indexed checkbox$/ do
  page.table[@index].check_check
end

Then /^The indexed checkbox should be checked$/ do
  expect(page.table[@index].check_checked?).to be true
end

Then /^I type "([^\"]*)" into the table's indexed text area$/ do |val|
  page.table[@index].area = val
end

Then /^The table's indexed text area should contain "([^\"]*)"$/ do |val|
  expect(page.table[@index].area).to eql val
end

Then /^I should see that the indexed button exists$/ do
  expect(page.table[@index].button?).to be true
end

Then /^I should be able to click the indexed button$/ do
  page.table[@index].button
end

Then /^I type "([^\"]*)" into the regular indexed text field by id$/ do |val|
  page.nottable[@index].text_nottable = val
end

Then /^The regular indexed text field by id should contain "([^\"]*)"$/ do |val|
  expect(page.nottable[@index].text_nottable).to eql val
end

When(/^I search for an element not on my indexed property but on another$/) do
  @index = 'foo'
end

Then(/^I should see that the element doesn't exist$/) do
  expect { page.other_table[@index].radio_element.value }.to raise_error(NoMethodError)
end

When(/^I search for an element that is on an indexed property$/) do
  page.table['foo'].radio_element
end

And(/^I search for the element on another indexed property it is not on$/) do
  @index = 'foo'
end

When(/^I search using the index which is not on another indexed property$/) do
  @index = '123'
end

Then(/^I should see that the element doesn't exist for that index/) do
  expect { page.other_table[@index].text_table_element.text }.to raise_error Watir::Exception::UnknownObjectException
end

When(/^I search for an element by an index on an indexed property$/) do
  page.table['123'].text_table_element.html
end

And(/^I search using an index which is on another indexed property$/) do
  @index = 'bar'
end

Then(/^I should see the content of the element on the second indexed property$/) do
  expect(page.other_table['bar'].content).to eq 'bar!'
end

When(/^I search for an element with text by an index on an indexed property$/) do
  @indexed_property = page.table
  @contents = [@indexed_property['123'].content]
  expect(@contents[0]).to eq '123!'
end

And(/^I search for a different element with text by index on the indexed property$/) do
  @contents << @indexed_property['12test'].content
end

Then(/^I should see the contents of both elements$/) do
  expect(@contents).to eq ['123!', '12test!']
end

When(/^I use a name that collides with a ruby method on an indexed property$/) do
  #noop element is already defined on indexed property
end

Then(/^the original method remains$/) do
  expect(page.table['foo'].nil?).to be false
end

When(/^I search for the elements with index (.*)$/) do |index|
  @index = index.to_i
end

And(/^I select the table's indexed radio button$/) do
  page.index_table[@index].select_radio
end

Then(/^The table's indexed radio button should be selected$/) do
  expect(page.index_table[@index]).to be_radio_selected
end

And(/^I select the table's indexed radio button by regex$/) do
  page.index_table_by_regex[@index].select_radio
end