
require 'win32ole'
require 'watir'   # the controller
include Watir
require 'test/unit'

TobagoFlightDest = ['Hong Kong', 'Johannesburg', 'Lagos', 'London', 'Shanghai', 'Singapore', 'Tokyo']
LondonFlightDest = ['Albany', 'Antigua', 'Barbados', 'Boston', 'Buffalo', 'Burlington', 'Cape Town', 'Cleveland', 'Dayton', 'Delhi', 'Detroit', 'Dubai', 'Fort Lauderdale', 'Grenada', 'Halifax', 'Hartford', 'Havana', 'Hong Kong', 'Houston', 'Indianapolis', 'Johannesburg', 'Lagos', 'Las Vegas', 'Los Angeles', 'Manchester,US', 'Miami', 'Montego Bay', 'Montreal', 'Mumbai', 'Nassau', 'New York', 'Norfolk', 'Orlando', 'Ottawa', 'Philadelphia', 'Portland,ME', 'Providence', 'Quebec', 'Richmond', 'Rochester', 'San Francisco', 'Shanghai', 'Singapore', 'St Lucia', 'Sydney', 'Syracuse', 'Tampa', 'Tobago', 'Tokyo', 'Washington']
SydneyFlightDest = ['Hong Kong', 'London']
		
	$depArrive = {'London'=> LondonFlightDest, 'Sydney'=> SydneyFlightDest, 'Tobago'=> TobagoFlightDest}
$keys = ["B", "C", "D", "E"]

class Malclass
@@workbook = nil
	@@worksheet = nil
	@@sheet = nil
	@@testSite = nil
	    # The above class vars are declared so when an instance of this class is created they are all 'nil' not <null>
	puts 'class vars declared' # MB Test Code
	#~ #constants equal values in Column A of the Excel spreadsheet
		#~ #these seem redundant? the hash will always have these fields  so far? maybe good for multi pages with different fields?
		#~ # Could declare them as normal Vars and re initialise per iteration?
	#~ DATE_FIRST = "Date_First"
	#~ DATE_SECOND = "Date_Second"
	#~ ANOTHER_DATE = "Another_Date"
	#~ LOOKING_DATE = "Looking_Date"
	#~ ENTER_DATE = "Enter_Date"
	#~ HOW_DATE = "How_Date"
	#~ EXPECTED_RESULT = "Expected_Result"
	    
	    #~ puts 'constants declared' #MB Test Code
	    
	def setup
		    puts 'entering setup' #MB Test Code
		    # this is why the files need to be in the same folder in this case, They could be in a sub folder 
		    # with /folder/filename or somewhere else with /absolute/path/to/filename.
		    # File.expand path is calling file.self.expandpath (the absolute path to me) and concatinating the file name on
		#htmlpage = File.expand_path("demopage.html")
		testdata = File.expand_path("FieldControlDem.xls")
		
		#@testSite = "#{htmlpage}"
		
		@excel = WIN32OLE::new('excel.Application')
		@workbook = @excel.Workbooks.Open("#{testdata}")
		@excel['Visible'] = true 
		@sheet = @excel.activesheet  
		
		@worksheet = @workbook.Worksheets(1) 
		@worksheet.Select 
		@sheet = @excel.activesheet  
		
		#find the right-most column on the worksheet in question
		@column_number = @sheet.range("b2").end(-4161).Column
		puts @column_number
		
		puts 'leaving setup'#MB Test Code
		puts '------'#MB Test Code
	end
	
	def buildArrayKeys
		$keys.each do |x|
			puts x
			mine = "#{x}:#{x}"
			$currCol = "#{x}"
			buildTestAttempt(mine)
		end
		#~ keys = ["B:B", "C:C", "D:D", "E:E"]
		
	end
	
	def buildTestAttempt(key)
		#puts 'enter BuildTest'
		$details = []
		@sheet.range("#{key}").each do |cell|
			puts cell
			if cell.value
				$details << cell.value
				puts cell.value
			else
				puts 'no more cells of value for the hash keys'#MB Test Code
				puts 'array is'
				puts $details
				puts '-----------------------'
				puts 'Going to test'
				runTestFromExcel
				setResult
				break
			end #if
			
		end # do

	end
	
	def runTestFromExcel
		fieldType = $details[0]
		testID = $details[1]
		val = $details[2]
		valid = $details[3]
		res = $details[4]
		
		if fieldType == 'select_list'then
			$ie.select_list(:id, testID).select(val)
			check(res, valid)
			setResult
		elsif fieldType == 'text_field'then
			$ie.text_field(:id, testID).set(val)
		end
	end
	
	def check(var1, var2)
		$output = 0
		$matchArray = $ie.selectBox(:id, var2).getAllContents
		$tempArray = $depArrive[var1]
		#~ puts '---------------test code------------------'
		#~ puts $depArrive['Tobago']
		#~ puts var1
		#~ puts $tempArray
		#~ puts '-------------End test code----------------'
		if ($tempArray == $matchArray) then
			#assert($tempArray == $matchArray) 
			puts "	Drop down matches expected"
			puts " "
			$output = 1
		else
			puts " "
			puts "WARNING Drop down and Expected may be different"
			puts " "
			puts "Key from Dep_Arrive Array"
			puts $tempArray
			puts " "
			puts "all contents form Destination Drop Down"
			puts $matchArray
			puts " "
			$output = 2
		end
	end
	
	def setResult
		if $output == 1 then
			@worksheet.Range("#{$currCol}11").Interior['ColorIndex'] = 4 #green
			@worksheet.Range("#{$currCol}11")['Value'] = ['PASS']	    
		elsif $output == 2 then
			#puts("FAIL Didn't find July")
			@worksheet.Range("#{$currCol}11").Interior['ColorIndex'] = 3 #red
			@worksheet.Range("#{$currCol}11")['Value'] = ['FAIL']
		end	 
	end
	
end #Malclass

	$ie = IE.new
	$ie.set_fast_speed
	#assert(n == 1) 
	
	temp = Malclass.new
	temp.setup
	
	$ie.goto("http://www.virgin-atlantic.com/en/gb/index.jsp")
	
	
		temp.buildArrayKeys
	
	
	#temp.Test_01_buildTestAttempt
	$ie.close