require 'dcell'
require 'pry'

module Hypertest
  class Master
    include Celluloid
    
    attr_reader :build, :completed_tests

    def initialize(remote, branch, sha, build_number)
      @build = Build.new(remote, branch, sha, build_number)
      @build.clone_repo
      @queued_tests   = load_tests
      @running_tests  = {}
      @completed_tests = []
    end

    def summarize_results
      build_result = BuildResult.new
      @completed_tests.each do |completed_test|
        build_result << completed_test
      end
      puts '*' * 25
      puts 'BUILD COMPLETE'
      puts build_result
    end

    # Should load tests and not execute them. 
    def load_tests
      puts "Loading test cases..."
      20.times.map do |n|
        TestCase.new('sample_test.rb', "test_#{n}")
      end
    end

    def next_test
      if test_to_run = @queued_tests.shift
        @running_tests[test_to_run.test_case_name] = Time.now
        test_to_run
      else
        false
      end
    end

    def complete_test(test_case_response)
      puts "completed test with response: #{test_case_response.inspect}"
      @running_tests.delete(test_case_response.test_case_name) #TODO:needs to be compound name: test_file_path + test_case_name -Colin
      @completed_tests << test_case_response
    end
  end
end

