# Encoding: utf-8

require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'rubocop/rake_task'

require File.dirname(__FILE__) + '/../../../infrastructure/vagrant/aws/VagrantfileUtil.rb'
require File.dirname(__FILE__) + '/../../../infrastructure/vagrant/Servers.rb'

$LOAD_PATH.push(File.expand_path(File.dirname(__FILE__) + '/features/lib'))

# Check environment sanity and fail fast if not correct
fail 'WORKSPACE environment variable not set. Did you "source infrastructure/set.env.sh"?' unless ENV.keys.include?('WORKSPACE')
fail 'GEM_HOME environment variable not set. Did you "source infrastructure/set.env.sh"?' unless ENV.keys.include?('GEM_HOME')

task default: [:endToEndTests]

task endToEndTests: [:codequality]

task codequality: [
  :info,
  :rubocop
]

task :endToEndTests, [:env] => [:environment_setup]

def browser_setup(btype)
  case btype
  when "PHANTOMJS"
    puts "PHANTOMJS selected"
  when "FIREFOX"
    puts "FIREFOX selected"
  when "SAFARI"
    puts "SAFARI selected"
  when "IE"
    puts "IE selected"
  when "Chrome"
    puts "Chrome selected"
  else
    fail "Unrecognized browser type: #{btype}. Allowable types are: 'PHANTOMJS', 'FIREFOX', 'CHROME', 'SAFARI', and 'IE'"
  end
end

## Attempting to run a sample acceptance test in the features directory
Cucumber::Rake::Task.new(:endToEndTests) do |t|
  btype = ENV['BTYPE'] || 'PHANTOMJS'
  browser_setup(btype)
  t.cucumber_opts = 'features --format pretty --tags ~@future  --tags ~@debug --tags ~@UI -f json_pretty -o cucumber.json -f html -o cucumber.html BROWSERTYPE=' << btype
end

## Placeholder task for executing all Acceptance Tests for all Mobile Applications
Cucumber::Rake::Task.new(:allAcceptanceTests) do |t|
  btype = ENV['BTYPE'] || 'PHANTOMJS'
  t.cucumber_opts = 'features --format pretty BROWSERTYPE=' << btype
end

task :info do
  puts '=========================================================================='
  puts '#     Running codequality checks.                                        #'
  puts '#     For details, see https://wiki.vistacore.us/display/VACORE/Ruby     #'
  puts '=========================================================================='
end

desc 'Rubocop code quality checks configured in .rubocop.yml file'
Rubocop::RakeTask.new(:rubocop) do |rubocop|
  # Specify config file in non-standard location.
  rubocop.options = ['-c.rubocop.yml']

  # Specify Ruby file locations
  rubocop.patterns = ['**/*.rb', 'Rakefile']

  # Show emacs style output, and offense counts
  # See https://github.com/bbatsov/rubocop#formatters for other output options
  rubocop.formatters = ['emacs', 'o']

  # Abort rake on failure
  rubocop.fail_on_error = true
end

task :environment_setup, :env do |task, args|
  env = args[:env] || 'virtualbox'
  environment_endpoints(env)
end

def environment_endpoints(env)
  case env
  when "virtualbox"
    ENV["ADK_IP"] = Servers::ADK.localIP
  when "aws"
    ENV["ADK_IP"] = VagrantfileUtil::AWS.get_private_ip("adk")
  else
    fail "Unrecognized environment type: #{env}. Allowable types are: 'virtualbox' and 'aws'"
  end
end


