module Support
  module Browser
    # Browser that runs on the local environment.
    class Local < Base
      def initialize(configs)
        @name = configs.delete(:name)
        @dimensions = configs.delete(:dimensions)
      end

      private

      def instantiate_watir_browser
        chromedriver_path = "local_chromedriver/chromedriver"
        if ENV["CUCUMBER_PROFILE"] != "grid" && File.file?("local_chromedriver/chromedriver")
          Selenium::WebDriver::Chrome.driver_path = chromedriver_path
        end
        Watir::Browser.new @name.to_sym, options
      end

      # rubocop:disable Metrics/MethodLength
      def options
        case @name
        when "chrome"
          # Add chrome capabilities then add list of chrome switches to args key
          if ENV["CUCUMBER_PROFILE"] == "docker_compose"
            OptionsDocker::CHROME_CAPABILITIES[:args] = OptionsDocker.chrome_switches
            OptionsDocker::CHROME_CAPABILITIES
          else
            Options::CHROME_CAPABILITIES[:args] = Options.chrome_switches
            Options::CHROME_CAPABILITIES
          end
        when "firefox"
          {
            profile: Options::FirefoxProfile.new.default
          }
        else
          fail "Unknown browser! #{@name}"
        end
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end
