class PlanContext < CmsContext

  def initialize
    super
    create_plans
    create_plan_features
    create_pricing_page
  end

  def given_plan(plan)
    @plan_id = Plan.find_by(name: plan).id
  end

  def given_plan_feature(text)
    @plan_feature_text = text
  end

  def change_attribute(attribute, new_value)
    within("#plan-#{@plan_id}") do
      field = find_by_data_field(make_lazy_camel_case(attribute))
      @old_value, @new_value = field.value, new_value
      field.set(new_value, {clear: :backspace})
      click_and_wait('Save')
    end
  end

  def delete_feature(text)
    @plan_feature_text = text
    @old_order_and_ids = get_order_and_id_hash(PlanFeature.where(plan_id: @plan_id))
    plan_feature = get_plan_feature_by_text
    @deleted_order = plan_feature.order
    within("#plan-#{@plan_id}") do
      delete_and_wait("div[data-id='#{plan_feature.id}']")
    end
  end

  def change_tooltip(text)
    @tooltip_text = text
    plan_feature = get_plan_feature_by_text
    @plan_feature_id = plan_feature.id
    sleep(5)
    within("#plan-#{@plan_id}") do
      within("div[data-id='#{plan_feature.id}']") do
        select_option(find('.nice-select'), "#{text[0...12]}...")
        wait_for_ajax
      end
    end
  end

  def rearrange(text, position)
    @plan_feature_id = get_plan_feature_by_text(text).id
    feature_to_move = "div[data-id='#{@plan_feature_id}']"
    within("#plan-#{@plan_id}") do
      drag_handle = within(feature_to_move) {find('img.ui-draggable-handle')}
      perform_rearrange(drag_handle, feature_to_move, position)
    end
  end

  def add_feature(text)
    @plan_feature_text = text
    within("#plan-#{@plan_id}") do
      click_a_tag('Add Feature')
    end
    select(text)
  end

  def expect_all_sections
    Plan.all.each do |plan|
      expect(find("#plan-#{plan.id}")).to be_truthy
    end
  end

  def expect_for_all_plans(type)
    Plan.all.each do |plan|
      within("#plan-#{plan.id}") do
        self.send("expect_correct_#{type}", plan)
      end
    end
  end

  def expect_feature_to_exist
    expect(get_plan_feature_by_text).to be_truthy
  end

  def expect_feature_text_to_exist_in_plan_features
    expect(find("#plan-#{@plan_id}")).to have_text(@plan_feature_text)
  end

  def expect_feature_to_not_exist
    expect(get_plan_feature_by_text).to be_falsey
    expect(order_rearranged?(PlanFeature.where(plan_id: @plan_id))).to be_truthy
  end

  def expect_feature_text_no_in_plan_features
    expect(find("#plan-#{@plan_id}")).to have_no_text(@plan_feature_text)
  end

  def expect_correct_backend_order
    expect(PlanFeature.find(@plan_feature_id).order).to eq(@new_data_index)
  end

  def expect_correct_data_index
    within("#plan-#{@plan_id}") do
      data_index = find("div[data-id='#{@plan_feature_id}']")['data-index']
      expect(data_index).to eq(@new_data_index.to_s)
    end
  end

  def expect_correct_position_on_screen
    within("#plan-#{@plan_id}") do
      all_plan_features = all('.plan-features-index-item')
      plan_feature = find("div[data-id='#{@plan_feature_id}']")
      expect(all_plan_features[@new_data_index]).to eq(plan_feature)
    end
  end

  def expect_correct_tooltip_for_plan_feature
    plan_feature = PlanFeature.find(@plan_feature_id)
    expect(plan_feature.tooltip.text).to eq(@tooltip_text)
  end

  def expect_user_to_see_tooltip
    within("#plan-#{@plan_id}") do
      plan_feature = find("div[data-id='#{@plan_feature_id}']")
      expect(plan_feature).to have_text("#{@tooltip_text[0...12]}...")
    end
  end

  private

  def get_plan_feature_by_text(feature_text = @plan_feature_text)
    plan_features = Plan.find(@plan_id).plan_features.select do |plan_feature|
      plan_feature.feature.text == feature_text
    end
    plan_features.first
  end

  def expect_correct_information(plan)
    return if plan.name == 'All Plans'
    ['name', 'tagline', 'includesText']. each do |attribute|
      table_column = attribute.split(/(?=[A-Z])/).join('_').downcase
      expect(find_value_by_data_field(attribute)).to eq plan[table_column]
    end
  end

  def expect_correct_price(plan)
    return if plan.name == 'All Plans'
    nice_select = find('div.type-select')
    select_option(nice_select, 'Monthly/Annual')
    expect(find_value_by_data_field('monthlyPrice')).to eq plan['monthly_price'].to_s
    unless (['Free', 'Business'].include?(plan['name']))
      expect(find_value_by_data_field('annualPrice')).to eq plan['annual_price'].to_s
    end
  end

  def expect_correct_features(plan)
    plan.plan_features.each do |plan_feature|
      feature = Feature.find(plan_feature.feature_id)
      plan_feature_item = find("div[data-id='#{plan_feature.id}']")
      expect(plan_feature_item.text).to include(feature.text)
      expect_correct_plan_feature_tooltip(plan_feature_item, plan_feature.tooltip_id, plan)
    end
  end

  def expect_correct_plan_feature_tooltip(plan_feature_item, tooltip_id, plan)
    within(plan_feature_item) do
      tooltip = find('span').text
      if tooltip == 'No Tooltip'
        expect(tooltip_id).to be_nil
      else
        database_tooltip = Tooltip.find(tooltip_id).text
        expect(tooltip[0...12]).to eq(database_tooltip[0...12])
      end
    end
  end
end
