# #requires
require 'watir'
# #email stuff
require 'net/smtp'

# #used to handle javascript popups
require 'watir/contrib/enabled_popup'
require 'watir/WindowHelper'
# 
# Method to handle javascript popups.
# 
require 'thread'
# 
require 'win32ole'
# 
def startClicker( button , waitTime = 3)
  w = WinClicker.new
  longName = @@ie.dir.gsub("/" , "\\" )
  shortName = w.getShortFileName(longName)
  c = "start ruby #{shortName}\\watir\\clickJSDialog.rb #{button} #{waitTime} "   
  puts "Starting #{c}"
  w.winsystem(c)   
  w=nil
end




# #includes
include Watir
@@ie = IE.new
@@ie.visible= true
# #set the speed of watir
@@ie.speed = :fast
# #backup 2 directories
Dir.chdir('..')
Dir.chdir('..')
# #we're in windows, so replace the forwards with the backs
workingdir = Dir.getwd.to_s.gsub!('/','\\')
# #Set constants for import
ORIGINAL_APP_ID = '155'
NEW_APP_ID = '102'
SCHEMA = ARGV[0].upcase if ARGV[0] 
SCHEMA = 'MIP_TEST' if !ARGV[0]
APEX_EXPORT_SCRIPTS_DIR =  workingdir +'\\Screens\\'
APPLICATION_SCRIPT = 'f155.sql'
CSS_SCRIPT = 'css.sql'
IMAGES_SCRIPT = 'f155_img.sql'
THEME_SCRIPT = 'f155_theme_101.sql'
ADID = '4550' #APEX admin identifier
WORKSPACE = ARGV[1].upcase if ARGV[1]
WORKSPACE = 'MIP_TEST' if !ARGV[1]
USER = 'ADMIN'
PASSWORD  = 'password'
DELETE_PREVIOUS_IMPORTS = false
# 
def login(p_workspace, p_user, p_password)
  @@ie.goto("http://loordv01/pls/apex/f?p=#{ADID}:1")
  @@ie.text_field(:id, "F#{ADID}_P1_COMPANY").set(p_workspace)
  @@ie.text_field(:id, "F#{ADID}_P1_USERNAME").set(p_user)
  @@ie.text_field(:id, "F#{ADID}_P1_PASSWORD").set(p_password)
  @@ie.button(:value, 'Login').click
  @@ie.wait
end
  
def importpageone (p_file, p_file_type, p_charset)
  @@ie.link(:title, 'Application Builder').click
  @@ie.wait
  @@ie.button(:value, 'Import >').click
  @@ie.wait
  @@ie.file_field(:id, "P460_FILE").set(p_file)
  @@ie.select_list(:id, "P460_FILE_TYPE").select p_file_type
  @@ie.select_list(:id, "P460_FILE_CHARSET").select p_charset
  @@ie.button(:value, 'Next >').click
  @@ie.wait
  @@ie.button(:value, 'Next >').click
  @@ie.wait
end

#
# this doesn't work as the P107_COMPANY is a javascript screen pop up thingy
def associateschema(p_workspace, p_schema)
  login('INTERNAL','ADMIN','password')
  @@ie.link(:title,'Manage Workspaces').click
  @@ie.wait
  @@ie.link(:title,'Manage Workspace to Schema Assignments').click
  @@ie.button(:value, 'Create >').click
  @@ie.wait
  @@ie.radio(:id, 'P105_NEW_0' ).set
  @@ie.wait
  @@ie.button(:value, 'Next >').click
  @@ie.wait
  @@ie.text_field(:id, "P107_COMPANY" ).set(p_workspace)
  @@ie.button(:value, 'Next >').click
  @@ie.wait
  @@ie.select_list(:id, "P106_SCHEMA").select p_schema
  @@ie.wait
  @@ie.button(:value, "Add Schema").click
  @@ie.wait
  @@ie.link('Logout').click
end

begin 
  # #Firstly, associate the schema with the workspace
  # associateschema(WORKSPACE, SCHEMA)
  # #Login
  login(WORKSPACE, USER, PASSWORD)
  # #Import Application export file
  importpageone(APEX_EXPORT_SCRIPTS_DIR+APPLICATION_SCRIPT,
    'Application, Page or Component Export',
    'Western European Windows 1252')
  # #parse page
  @@ie.select_list(:id, "P49_PARSE_AS" ).select(SCHEMA)
  @@ie.select_list(:id, "P49_BUILD_STATUS" ).select 'Run and Build Application'
  @@ie.radio(:id, "P49_NEW_FLOW_OPT_2" ).set
  @@ie.wait
  @@ie.text_field(:id, "P49_NEW_FLOW_ID").set(NEW_APP_ID)
  @@ie.button(:value, 'Install').click
  @@ie.wait
  # #This button won't appear the first time the import occurs
  if @@ie.button(:value, 'Replace Existing Application '+NEW_APP_ID).exists?
    @@ie.button(:value, 'Replace Existing Application '+NEW_APP_ID).click
    @@ie.wait
  end
  # #Install Supporting objects
  #~ @@ie.radio(:id, "P2100_ACTION_0" ).set
  #~ @@ie.button(:value, 'Next >').click
  #~ @@ie.wait
  #~ @@ie.button(:value, 'Install').click
  #~ @@ie.wait
  # #Install CSS

  # #Need to delete the existing CSS file before installing a new one #have
  # decided to just install the application for the moment. #back to install
  # page start

  # #importpageone(APEX_EXPORT_SCRIPTS_DIR+CSS_SCRIPT,
  #                  'CSS Export',
  #                  'Western European Windows 1252')

  if DELETE_PREVIOUS_IMPORTS
    # #er this code does not work from the Loordv001 box #it will run on JP's
    # machine if this code is run from a #dos prompt - it's an issue with ruby
    # trying to spawn a #new thead to look for the open dialogue box
    @@ie.link(:title, 'Manage Export Repository').click
    @@ie.wait
    @@ie.checkboxes.each { |c| c.set }
    # #Click the annoying are you sure button that pops up
    startClicker("OK", 1)   
    @@ie.button(:value, 'Delete Checked').click
    @@ie.wait
  end

  @@ie.close

rescue StandardError => anyerror
  # #Hopefully we never get here but, we'll try and send an email if the #script
  # errors so we can fix it #don't leave memory hungry instances of ie running
  # if we get an error
  @@ie.close

  # #build the email
  from_addr = 'jamie.priest@advantica.com'
  to_addr = 'jamie.priest@advanticagroup.com'
  project = 'webMIP test build process'
  emailtext = <<END_EMAIL
From: webMIP <#{from_addr}>
To: Jamie Priest <#{to_addr}>
Subject: #{project} automated test failure

An automated assertion failed for the project
#{project}

#{anyerror}

END_EMAIL
  Net::SMTP.start('LOMAIL01') do |smtp|
    smtp.sendmail emailtext, from_addr, to_addr
  end

end