# frozen_string_literal: true

module MiBridges
  class Driver
    class JobIncomeMoreAboutPage < BasePage
      TITLE = /More About .*'s Job/

      delegate :members, to: :snap_application

      def setup; end

      def fill_in_required_fields
        fill_in "Name of Employer", with: current_member.employed_employer_name
        select current_member.employed_pay_interval,
          from: "How often does Complete (40) get paid? "\
                "This is Complete (40)'s pay period"

        if hourly?
          fill_in_hourly
        else
          fill_in_salary
        end
      end

      def continue
        click_on "Next"
      end

      private

      def current_member
        title = find("h1").text
        name = title.scan(/More About (.*)'s Job/).flatten.first
        members.select do |member|
          member.mi_bridges_formatted_name == name
        end.first
      end

      def hourly?
        current_member.employed_pay_interval == "Hourly"
      end

      def fill_in_hourly
        fill_in "If Complete (40) gets paid by the hour, ",
          with: current_member.employed_pay_quantity
        fill_in "Please tell us how many hours Complete (40) works each week",
          with: current_member.employed_hours_per_week
      end

      def fill_in_salary
        fill_in "If Complete (40) earns a salary instead",
          with: current_member.employed_pay_quantity
      end
    end
  end
end

# frozen_string_literal: true

module MiBridges
  class Driver
    class JobIncomeMoreAboutPage < BasePage
      TITLE = /More About .*'s Job/

      delegate :members, to: :snap_application

      def setup; end

      def fill_in_required_fields
        fill_in "Name of Employer", with: current_member.employed_employer_name
        select current_member.employed_pay_interval,
          from: "How often does #{name} get paid? This is #{name}'s pay period"

        if hourly?
          fill_in_hourly
        else
          fill_in_salary
        end
      end

      def continue
        click_on "Next"
      end

      private

      def hourly?
        current_member.employed_pay_interval == "Hourly"
      end

      def fill_in_hourly
        fill_in "If #{name} gets paid by the hour, ",
          with: current_member.employed_pay_quantity
        fill_in "Please tell us how many hours #{name} works",
          with: current_member.employed_hours_per_week
      end

      def fill_in_salary
        fill_in "If #{name} earns a salary instead",
          with: current_member.employed_pay_quantity
      end

      def current_member
        members.select do |member|
          member.mi_bridges_formatted_name == name
        end.first
      end

      def name
        @_name ||= begin
                     title = find("h1").text
                     title.scan(/More About (.*)'s Job/).flatten.first
                   end
      end
    end
  end
end

Use `name` instead of "Complete (40)"
FIX: Use `name` instead of "Complete (40)"

Also move `current_member` down in order of appearance to read more like
newspaper articles[1].

[1]: http://monami555.github.io/2016/01/13/clean-code-robert-c-martin-notes.html#4-formatting
