require './core/utils/post_build_utils.rb'
require './core/utils/stats_utils.rb'
require './core/utils/summary_writer_utils.rb'
require 'smoca_common/utils/twitch_utils'
require 'headless'

include PostBuildUtils
include SummaryWriterUtils
include StatUtils::Tracking

task :default => [:test]

task :test do
  if ENV['PROPERTIES'] != nil
    require './core/utils/properties_config_utils'
    PropertiesConfigUtils.configure_environment
  end

  num_of_processes = determine_cores()

  if ENV['CAPYBARA_HEADLESS'] == 'true'
    $headless_browser = Headless.new(display: 100, reuse: true, dimensions: '1920x1080x24', destroy_at_exit: false) # Start up a Headless Browser
    $headless_browser.start
  end

  # Determine test_path.
  # Smoca currently has 3 main testing services: smoca-web, smoca-video, & smoca-player-core
  case ENV['TEST_SUITE']
    when 'player_ui'
      test_path = config_player_test
    when 'player_core'
      test_path = 'spec/video/player_core/'
    when 'passport'
      test_path = 'spec/web/passport'
    when 'channel_page_integration'
      test_path = 'spec/video/channel_page_integration/'
    else
      test_path = 'spec/web'
  end

  if ENV['PARALLEL'] == 'true'
    location = find_runtime_log
    cmd = "parallel_rspec -n #{num_of_processes} --runtime-log #{location} #{test_path}"
  else
    cmd = "rspec #{test_path}"
  end

  delete_file(get_summary_file_path()) # Delete any preexisting failure file

  #################
  # Run the Suite #
  #################
  result = system(cmd) # Execute the command

  ####################
  # Determine Result #
  ####################
  if result == true
    $exit_code = 0
  else
    $exit_code = 1
  end
end

at_exit { Rake::Task[:cleanup].invoke if $!.nil? } # This is what calls :cleanup, just before Rake exits

# When all Parallel Processes complete, run this task
task :cleanup do
  # Kill Headless Browser
  $headless_browser.destroy if ENV['CAPYBARA_HEADLESS'] == 'true'

  exit($exit_code) if ENV['PARALLEL'] != 'true' # Exit immediately if Parallel isn't enabled

  output_summary # Print out the failures file
  summary_log_events($exit_code) # Send overall summary stats to statsd

  delete_file(get_summary_file_path()) # Delete the file to cleanup
  exit($exit_code) # Exit with the test's exit code
end

# @param filepath - type String. Should be the path to the file
# Delete the file that exists at the passed filepath
def delete_file(filepath)
  if File.exists?(filepath)
    File.delete(filepath)
  end
end

def determine_cores
  if ENV['TIER2'] == 'true'
    return 2 # More scenarios to run in Tier 2
  else
    return 1
  end
end

# TIER1 and TIER2 have different test runtimes. This copies over the run times to be picked up by Parallel
# Purpose is so that Parallel can end around the same time
def find_runtime_log
  if ENV['TIER2'] != 'true'
    return 'resources/logs/tier1_runtime.log'
  else
    return 'resources/logs/tier2_runtime.log'
  end
end

# As of SMOC-318, TEST_SUITE = player_ui, consist of several broken down video player related services.
# If ENV['BUFFER_EVENT'] is found a specific test path is set to execute a Tier 1 buffer event.
def config_player_test
  case ENV['BUFFER_EVENT']
    when 'buffer_empty'
      return 'spec/video/tier1_buffer_events/buffer_empty_spec.rb' # Temp for SMOC-318
    when 'buffer_refill'
      return 'spec/video/tier1_buffer_events/buffer_refill_spec.rb' # Temp for SMOC-318
    else
      test_path = 'spec/video/player_ui'
      return test_path
  end
end
